-
Notifications
You must be signed in to change notification settings - Fork 2
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
etai
committed
May 1, 2013
1 parent
bd15522
commit 74d8595
Showing
9 changed files
with
141 additions
and
78 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 |
---|---|---|
@@ -1,4 +1,5 @@ | ||
Resors | ||
===== | ||
Simply write resource. | ||
|
||
Resources for Mongoose. Check `\example\models\users.js` for most options. |
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,23 @@ | ||
var mongoose = require('mongoose'), | ||
Types = mongoose.Schema.Types; | ||
|
||
var schema = new mongoose.Schema({ | ||
title: { type: String }, | ||
content: { type: String }, | ||
user: { type: Types.ObjectId, ref: 'users' } | ||
}); | ||
schema.methods.toString = function(){ | ||
return this.title; | ||
}; | ||
var posts = module.exports = mongoose.model('posts', schema); | ||
|
||
|
||
/* | ||
Resors | ||
*/ | ||
posts.resors = { | ||
query: function(req, res, next) { | ||
res.query = res.query.populate('user'); | ||
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
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,13 @@ | ||
var Resors = require('../'), | ||
models = require('./models'); | ||
|
||
var hi = new Resors('hi'); | ||
hi.index = function(req, res) { | ||
res.json('hi'); | ||
}; | ||
|
||
module.exports = { | ||
hi: hi, | ||
users: models.users, | ||
posts: models.posts | ||
}; |
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 |
---|---|---|
@@ -1,19 +1,29 @@ | ||
var Resors = require('./resors'); | ||
var Resors = require('./resors'), | ||
MongooseResors = require('./mongoose'); | ||
|
||
var resors = module.exports = function(express, models) { | ||
module.exports = Resors; | ||
Resors.MongooseResors = MongooseResors; | ||
|
||
Resors.middleware = function(express, resources) { | ||
var app = express(); | ||
|
||
app.get('/', function(req, res) { | ||
res.json({ | ||
models: Object.keys(models) | ||
resources: Object.keys(resources) | ||
}); | ||
}); | ||
|
||
Object.keys(models).forEach(function(name) { | ||
var resource = Resors.fetch(models[name]); | ||
resource.routes(app); | ||
Object.keys(resources).forEach(function(path) { | ||
var r = resources[path]; | ||
|
||
if (r.modelName) | ||
r = new MongooseResors(r, r.resors || {}); | ||
|
||
else if (!r.routes) | ||
r = new Resors(path, r); | ||
|
||
r.routes(app); | ||
}); | ||
|
||
return app; | ||
}; | ||
resors.Resors = Resors; | ||
}; |
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,61 @@ | ||
var Resors = require('./resors'), | ||
extend = require('xtend'); | ||
|
||
/* | ||
Mongoose Resors Class | ||
*/ | ||
var MongooseResors = module.exports = function(model, options) { | ||
this.model = model; | ||
this.path = model.modelName; | ||
this.options = extend(Resors.defaults, options); | ||
}; | ||
MongooseResors.fn = MongooseResors.prototype = new Resors; | ||
MongooseResors.fn.constructor = MongooseResors; | ||
MongooseResors.fn.query = function(q) { | ||
var o = this.options; | ||
|
||
if (o.fields) | ||
q = q.select(o.fields); | ||
|
||
return q; | ||
}; | ||
MongooseResors.fn.index = function(req, res, next) { | ||
res.query = this.query(this.model.find()); | ||
|
||
/* | ||
TODO: | ||
offset, limit | ||
filter | ||
sorting | ||
*/ | ||
|
||
next(); | ||
}; | ||
MongooseResors.fn.show = function(req, res, next) { | ||
res.query = this.query(this.model.findById(req.params.id)); | ||
next(); | ||
}; | ||
MongooseResors.fn.create = function(req, res, next) { | ||
this.model.create(req.body, function(err, result) { | ||
res.err = err; | ||
res.result = result; | ||
next(); | ||
}); | ||
}; | ||
MongooseResors.fn.update = function(req, res, next) { | ||
console.log(req.body); | ||
res.query = this.query(this.model.findByIdAndUpdate(req.params.id, req.body)); | ||
next(); | ||
}; | ||
MongooseResors.fn.destroy = function(req, res, next) { | ||
res.query = this.model.findByIdAndRemove(req.params.id); | ||
next(); | ||
}; | ||
MongooseResors.fn.exec = function(req, res, next) { | ||
res.query.exec(function(err, result) { | ||
delete res.query; | ||
res.err = err; | ||
res.result = result; | ||
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
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