-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add simple service demo using sequelize.
- Loading branch information
Paul Selden
committed
Jan 1, 2014
1 parent
08894c1
commit e5579fe
Showing
14 changed files
with
159 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,3 +13,4 @@ results | |
|
||
npm-debug.log | ||
node_modules | ||
.idea |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
|
||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
exports.users = require('./users'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
exports.users = require('./users'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = require('./usersProvider'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |