forked from dresende/node-orm2
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
20 changed files
with
694 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
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,30 @@ | ||
# AnonTXT demo app | ||
|
||
## Getting started | ||
|
||
**Setup node-orm2!** | ||
|
||
```bash | ||
git clone https://github.com/dresende/node-orm2.git | ||
cd node-orm2 | ||
npm install | ||
|
||
# You may work off master, or checkout a different version if master is broken: | ||
git tag | ||
git checkout v2.1.3 | ||
``` | ||
|
||
**Setup AnonTXT** | ||
|
||
Edit `anontxt/config/settings.js` and set your database, user & password. | ||
|
||
```bash | ||
cd examples/anontxt | ||
npm install | ||
node tasks/reset | ||
nodemon server.js | ||
``` | ||
|
||
And then open up [localhost:3000](http://localhost:3000/) | ||
|
||
You can also just run it with `node server.js` however nodemon will automatically restart the server if you change any code. |
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,15 @@ | ||
|
||
module.exports = { | ||
formatErrors: function(errorsIn) { | ||
var errors = {}; | ||
var a, e; | ||
|
||
for(a = 0; a < errorsIn.length; a++) { | ||
e = errorsIn[a]; | ||
|
||
errors[e.property] = errors[e.property] || []; | ||
errors[e.property].push(e.msg); | ||
} | ||
return errors; | ||
} | ||
}; |
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,33 @@ | ||
var _ = require('lodash'); | ||
var helpers = require('./_helpers'); | ||
var orm = require('../../../../'); | ||
|
||
module.exports = { | ||
create: function (req, res, next) { | ||
var params = _.pick(req.body, 'author', 'body'); | ||
|
||
req.models.message.get(req.params.messageId, function (err, message) { | ||
if (err) { | ||
if (err.code == orm.ErrorCodes.NOT_FOUND) { | ||
res.send(404, "Message not found"); | ||
} else { | ||
return next(err); | ||
} | ||
} | ||
|
||
params.message_id = message.id; | ||
|
||
req.models.comment.create(params, function (err, message) { | ||
if(err) { | ||
if(Array.isArray(err)) { | ||
return res.send(200, { errors: helpers.formatErrors(err) }); | ||
} else { | ||
return next(err); | ||
} | ||
} | ||
|
||
return res.send(200, message.serialize()); | ||
}); | ||
}); | ||
} | ||
}; |
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 @@ | ||
var settings = require('../../config/settings'); | ||
|
||
module.exports = function (req, res, next) { | ||
res.sendfile(settings.path + '/public/index.html'); | ||
}; |
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,6 @@ | ||
|
||
module.exports = { | ||
home : require('./home_controller'), | ||
messages : require('./messages_controller'), | ||
comments : require('./comments_controller') | ||
}; |
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,35 @@ | ||
var _ = require('lodash'); | ||
var helpers = require('./_helpers'); | ||
var orm = require('../../../../'); | ||
|
||
module.exports = { | ||
list: function (req, res, next) { | ||
req.models.message.find().limit(4).order('-id').all(function (err, messages) { | ||
if (err) return next(err); | ||
|
||
var items = messages.map(function (m) { | ||
return m.serialize(); | ||
}); | ||
|
||
res.send({ items: items }); | ||
}); | ||
}, | ||
create: function (req, res, next) { | ||
var params = _.pick(req.body, 'title', 'body'); | ||
|
||
req.models.message.create(params, function (err, message) { | ||
if(err) { | ||
if(Array.isArray(err)) { | ||
return res.send(200, { errors: helpers.formatErrors(err) }); | ||
} else { | ||
return next(err); | ||
} | ||
} | ||
|
||
return res.send(200, message.serialize()); | ||
}); | ||
}, | ||
get: function (req, res, next) { | ||
|
||
} | ||
}; |
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 moment = require('moment'); | ||
|
||
module.exports = function (orm, db) { | ||
var Comment = db.define('comment', { | ||
body : { type: 'text', required: true }, | ||
createdAt : { type: 'date', required: true, time: true } | ||
}, | ||
{ | ||
hooks: { | ||
beforeValidation: function () { | ||
this.createdAt = new Date(); | ||
} | ||
}, | ||
validations: { | ||
body : orm.enforce.ranges.length(1, 1024) | ||
}, | ||
methods: { | ||
serialize: function () { | ||
return { | ||
body : this.body, | ||
createdAt : moment(this.createdAt).fromNow() | ||
} | ||
} | ||
} | ||
}); | ||
|
||
Comment.hasOne('message', db.models.message, { required: true, reverse: 'comments', autoFetch: true }); | ||
}; |
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,22 @@ | ||
var orm = require('../../../../'); | ||
var settings = require('../../config/settings'); | ||
|
||
var connection = null; | ||
|
||
function setup(db, cb) { | ||
require('./message')(orm, db); | ||
require('./comment')(orm, db); | ||
|
||
return cb(null, db); | ||
} | ||
|
||
module.exports = function (cb) { | ||
if (connection) return cb(null, connection); | ||
|
||
orm.connect(settings.database, function (err, db) { | ||
if (err) return cb(err); | ||
|
||
db.settings.set('instance.returnAllErrors', true); | ||
setup(db, cb); | ||
}); | ||
}; |
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,45 @@ | ||
var moment = require('moment'); | ||
|
||
module.exports = function (orm, db) { | ||
var Message = db.define('message', { | ||
title : { type: 'text', required: true }, | ||
body : { type: 'text', required: true, big: true }, | ||
createdAt : { type: 'date', required: true, time: true } | ||
}, | ||
{ | ||
hooks: { | ||
beforeValidation: function () { | ||
this.createdAt = new Date(); | ||
} | ||
}, | ||
validations: { | ||
title: [ | ||
orm.enforce.ranges.length(1, undefined, "must be atleast 1 letter long"), | ||
orm.enforce.ranges.length(undefined, 96, "cannot be longer than 96 letters") | ||
], | ||
body: [ | ||
orm.enforce.ranges.length(1, undefined, "must be atleast 1 letter long"), | ||
orm.enforce.ranges.length(undefined, 32768, "cannot be longer than 32768 letters") | ||
] | ||
}, | ||
methods: { | ||
serialize: function () { | ||
var comments; | ||
|
||
if (this.comments) { | ||
comments = this.comments.map(function (c) { return c.serialize(); }); | ||
} else { | ||
comments = []; | ||
} | ||
|
||
return { | ||
id : this.id, | ||
title : this.title, | ||
body : this.body, | ||
createdAt : moment(this.createdAt).fromNow(), | ||
comments : comments | ||
}; | ||
} | ||
} | ||
}); | ||
}; |
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,24 @@ | ||
var path = require('path'); | ||
var express = require('express'); | ||
var settings = require('./settings'); | ||
var models = require('../app/models/'); | ||
|
||
module.exports = function (app) { | ||
app.configure(function () { | ||
app.use(express.static(path.join(settings.path, 'public'))); | ||
app.use(express.logger({ format: 'dev' })); | ||
app.use(express.bodyParser()); | ||
app.use(express.methodOverride()); | ||
app.use(function (req, res, next) { | ||
models(function (err, db) { | ||
if (err) return next(err); | ||
|
||
req.models = db.models; | ||
req.db = db; | ||
|
||
return next(); | ||
}); | ||
}), | ||
app.use(app.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,10 @@ | ||
|
||
var controllers = require('../app/controllers') | ||
|
||
module.exports = function (app) { | ||
app.get( '/' , controllers.home); | ||
app.get( '/messages' , controllers.messages.list); | ||
app.post('/messages' , controllers.messages.create); | ||
app.get( '/message/:id' , controllers.messages.get); | ||
app.post('/message/:messageId/comments', controllers.comments.create); | ||
}; |
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,16 @@ | ||
var path = require('path'); | ||
|
||
var settings = { | ||
path : path.normalize(path.join(__dirname, '..')), | ||
port : process.env.NODE_PORT || 3000, | ||
database : { | ||
protocol : "postgresql", // or "mysql" | ||
query : { pool: true }, | ||
host : "127.0.0.1", | ||
database : "anontxt_dev", | ||
user : "anontxt", | ||
password : "apassword" | ||
} | ||
}; | ||
|
||
module.exports = settings; |
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,18 @@ | ||
{ | ||
"name": "AnonTXT", | ||
"description": "Post text snippets, quickly and easily", | ||
"version": "0.1.0", | ||
"repository" : "http://github.com/dresende/node-orm2.git", | ||
"dependencies": { | ||
"express": "3.3.*", | ||
"lodash": "2.3.0", | ||
"nodemon": "0.7.10", | ||
"moment": "2.4.0", | ||
"pg": "2.6.2", | ||
"colors": "0.6.2" | ||
}, | ||
"scripts": { | ||
"start": "script/start", | ||
"reset": "script/reset" | ||
} | ||
} |
Oops, something went wrong.