diff --git a/.gitignore b/.gitignore index a72b52e..071738d 100644 --- a/.gitignore +++ b/.gitignore @@ -13,3 +13,4 @@ results npm-debug.log node_modules +.idea \ No newline at end of file diff --git a/app.js b/app.js new file mode 100644 index 0000000..575dce6 --- /dev/null +++ b/app.js @@ -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); + + } + }); \ No newline at end of file diff --git a/controllers/index.js b/controllers/index.js new file mode 100644 index 0000000..6a2fdd3 --- /dev/null +++ b/controllers/index.js @@ -0,0 +1 @@ +exports.users = require('./users'); \ No newline at end of file diff --git a/controllers/users.js b/controllers/users.js new file mode 100644 index 0000000..4f109c0 --- /dev/null +++ b/controllers/users.js @@ -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; +}; \ No newline at end of file diff --git a/lib/middleware.js b/lib/middleware.js new file mode 100644 index 0000000..1668121 --- /dev/null +++ b/lib/middleware.js @@ -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'); \ No newline at end of file diff --git a/lib/router.js b/lib/router.js new file mode 100644 index 0000000..9054c38 --- /dev/null +++ b/lib/router.js @@ -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; \ No newline at end of file diff --git a/package.json b/package.json new file mode 100644 index 0000000..f8a234c --- /dev/null +++ b/package.json @@ -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" +} diff --git a/platform/db/index.js b/platform/db/index.js new file mode 100644 index 0000000..5091fab --- /dev/null +++ b/platform/db/index.js @@ -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; \ No newline at end of file diff --git a/platform/db/models/post.js b/platform/db/models/post.js new file mode 100644 index 0000000..86aa13c --- /dev/null +++ b/platform/db/models/post.js @@ -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; +} \ No newline at end of file diff --git a/platform/db/models/user.js b/platform/db/models/user.js new file mode 100644 index 0000000..3190a49 --- /dev/null +++ b/platform/db/models/user.js @@ -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; +} \ No newline at end of file diff --git a/platform/index.js b/platform/index.js new file mode 100644 index 0000000..6a2fdd3 --- /dev/null +++ b/platform/index.js @@ -0,0 +1 @@ +exports.users = require('./users'); \ No newline at end of file diff --git a/platform/users/index.js b/platform/users/index.js new file mode 100644 index 0000000..462dd7e --- /dev/null +++ b/platform/users/index.js @@ -0,0 +1 @@ +module.exports = require('./usersProvider'); \ No newline at end of file diff --git a/platform/users/usersPersistence.js b/platform/users/usersPersistence.js new file mode 100644 index 0000000..d4028b1 --- /dev/null +++ b/platform/users/usersPersistence.js @@ -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 }); +} \ No newline at end of file diff --git a/platform/users/usersProvider.js b/platform/users/usersProvider.js new file mode 100644 index 0000000..9c55505 --- /dev/null +++ b/platform/users/usersProvider.js @@ -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); +} \ No newline at end of file