Skip to content

Commit

Permalink
Add simple service demo using sequelize.
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul Selden committed Jan 1, 2014
1 parent 08894c1 commit e5579fe
Show file tree
Hide file tree
Showing 14 changed files with 159 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ results

npm-debug.log
node_modules
.idea
23 changes: 23 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
var app = require('koa')();
var middleware = require('./lib/middleware');
var db = require('./platform/db');
var router = require('./lib/router');

app.use(middleware.favicon()); // TODO - right now this actually bounces a favicon with a 404
app.use(middleware.logger());
app.use(middleware.responseTime());
app.use(middleware.compress());

app.use(router.middleware());

db
.sequelize.sync()
.complete(function (err) { // TODO - figure out how to yield instead of using the promise
if (err) {
throw err;
} else {

app.listen(3000);

}
});
1 change: 1 addition & 0 deletions controllers/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
exports.users = require('./users');
17 changes: 17 additions & 0 deletions controllers/users.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
var platform = require('../platform'),
parse = require('co-body');

exports.show = function *(next){
var user = yield platform.users.getUser(this.params.user);
if(user){
this.body = user;
} else {
this.throw(404, 'No user found');
}
};

exports.create = function *(next){
var body = yield parse.json(this);
var user = yield platform.users.createUser(body.name);
this.body = user;
};
5 changes: 5 additions & 0 deletions lib/middleware.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
exports.responseTime = require('koa-response-time');
exports.logger = require('koa-logger');
exports.compress = require('koa-compress');
exports.mount = require('koa-mount');
exports.favicon = require('koa-favicon');
8 changes: 8 additions & 0 deletions lib/router.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
var Router = require('koa-router');
var controllers = require('../controllers');

var router = new Router();

router.resource('users', controllers.users);

module.exports = router;
26 changes: 26 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"name": "koa-demo",
"version": "0.0.1",
"description": "Demo for a simple koa app.",
"main": "app.js",
"dependencies": {
"co": "~3.0.1",
"koa": "~0.2.0",
"pg": "~2.10.0",
"sequelize": "~2.0.0-beta.5",
"thunkify": "0.0.1",
"koa-router": "~2.2.0",
"co-body": "0.0.1",
"koa-logger": "~1.1.0",
"koa-response-time": "~1.0.1",
"koa-favicon": "~1.0.1",
"koa-mount": "~1.2.2",
"koa-compress": "~1.0.0"
},
"devDependencies": {},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "pselden",
"license": "MIT"
}
28 changes: 28 additions & 0 deletions platform/db/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
var fs = require('fs'),
path = require('path'),
Sequelize = require('sequelize'),
sequelize = new Sequelize('blogs_db', null, null, { // TODO - configuration
dialect: "postgres",
port: 5432
}),
db = {};

// read all models and import them into the "db" object
fs
.readdirSync(__dirname + '/models')
.filter(function (file) {
return (file.indexOf('.') !== 0) && (file !== 'index.js');
})
.forEach(function (file) {
var model = sequelize.import(path.join(__dirname + '/models', file));
db[model.name] = model;
});

Object.keys(db).forEach(function (modelName) {
if (db[modelName].options.hasOwnProperty('associate')) {
db[modelName].options.associate(db);
}
});

db.sequelize = sequelize;
module.exports = db;
11 changes: 11 additions & 0 deletions platform/db/models/post.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module.exports = function(sequelize, DataTypes) {
var Post = sequelize.define('Post', {
title: DataTypes.STRING
}, {
associate: function(models) {
Post.belongsTo(models.User);
}
});

return Post;
}
11 changes: 11 additions & 0 deletions platform/db/models/user.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module.exports = function(sequelize, DataTypes) {
var User = sequelize.define('User', {
name: DataTypes.STRING
}, {
associate: function(models) {
User.hasMany(models.Post);
}
});

return User;
}
1 change: 1 addition & 0 deletions platform/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
exports.users = require('./users');
1 change: 1 addition & 0 deletions platform/users/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('./usersProvider');
9 changes: 9 additions & 0 deletions platform/users/usersPersistence.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
var db = require('../db');

exports.getUser = function *(id){
return yield db.User.find(Number(id));
};

exports.createUser = function *(name){
return yield db.User.create({ name: name });
}
17 changes: 17 additions & 0 deletions platform/users/usersProvider.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
var usersPersistence = require('./usersPersistence');

exports.getUser = function *(id){
if(!id){
throw new Error("id must be specified");
}

return yield usersPersistence.getUser(id);
};

exports.createUser = function *(name){
if(!name){
throw new Error("name must be specified");
}

return yield usersPersistence.createUser(name);
}

0 comments on commit e5579fe

Please sign in to comment.