remove model-route
This commit is contained in:
parent
5fdc7076b7
commit
279ce5b929
|
@ -4,7 +4,8 @@
|
|||
|
||||
var Model = require('../node_modules/model/lib/model')
|
||||
, DataSource = require('../node_modules/data-source')
|
||||
, assert = require('assert');
|
||||
, assert = require('assert')
|
||||
, RemoteObjects = require('sl-remoting');
|
||||
|
||||
/**
|
||||
* Export the app prototype.
|
||||
|
@ -13,7 +14,19 @@ var Model = require('../node_modules/model/lib/model')
|
|||
var app = exports = module.exports = {};
|
||||
|
||||
/**
|
||||
* Remove a route by referenve.
|
||||
* Create a set of remote objects.
|
||||
*/
|
||||
|
||||
app.remotes = function () {
|
||||
if(this._remotes) {
|
||||
return this._remotes;
|
||||
} else {
|
||||
return (this._remotes = RemoteObjects.create());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove a route by reference.
|
||||
*/
|
||||
|
||||
app.disuse = function (route) {
|
||||
|
@ -39,8 +52,11 @@ app.models = {};
|
|||
* @param options {Object}
|
||||
* @returns {Model}
|
||||
*/
|
||||
|
||||
app.defineModel =
|
||||
app.define = function (name, options) {
|
||||
var remotes = this.remotes();
|
||||
|
||||
options = options || {};
|
||||
options.name = options.name || name;
|
||||
|
||||
|
@ -48,7 +64,19 @@ app.define = function (name, options) {
|
|||
|
||||
assert(options.name, 'a name is required to define a model');
|
||||
|
||||
return (this.model[name] = BaseModel.extend(options));
|
||||
var ModelCtor = (this.model[name] = BaseModel.extend(options));
|
||||
var proto = ModelCtor.prototype;
|
||||
|
||||
remotes.exports[name] = ModelCtor;
|
||||
|
||||
// default shared methods
|
||||
if(ModelCtor.all) ModelCtor.all.shared = true;
|
||||
if(proto.save) proto.save = true;
|
||||
|
||||
// attach a remotes reference
|
||||
ModelCtor.remotes = remotes;
|
||||
|
||||
return ModelCtor;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -1,10 +0,0 @@
|
|||
.DS_Store
|
||||
*.seed
|
||||
*.log
|
||||
*.csv
|
||||
*.dat
|
||||
*.out
|
||||
*.pid
|
||||
*.swp
|
||||
*.swo
|
||||
node_modules/
|
|
@ -1,21 +0,0 @@
|
|||
# model route
|
||||
|
||||
## About
|
||||
|
||||
A `ModelRoute` inherits from the [route](../route) class. It provides HTTP access to a supplied [model](../model) instance.
|
||||
|
||||
## Config
|
||||
|
||||
Supports all configuration of a [resource](../route).
|
||||
|
||||
### Options
|
||||
|
||||
#### name (optional)
|
||||
|
||||
If defined, overrides the default name (based on the path).
|
||||
|
||||
### Dependencies
|
||||
|
||||
#### model
|
||||
|
||||
Requires a path to an existing model instance.
|
|
@ -1,5 +0,0 @@
|
|||
/**
|
||||
* collection ~ public api
|
||||
*/
|
||||
|
||||
module.exports = require('./lib/model-route');
|
|
@ -1,132 +0,0 @@
|
|||
/**
|
||||
* Expose `ModelRoute`.
|
||||
*/
|
||||
|
||||
module.exports = ModelRoute;
|
||||
|
||||
/**
|
||||
* Module dependencies.
|
||||
*/
|
||||
|
||||
var Route = require('route')
|
||||
, debug = require('debug')('collection')
|
||||
, util = require('util')
|
||||
, inherits = util.inherits
|
||||
, assert = require('assert');
|
||||
|
||||
/**
|
||||
* Create a new `ModelRoute` with the given `options`.
|
||||
*
|
||||
* @param {Object} options
|
||||
* @return {ModelRoute}
|
||||
*/
|
||||
|
||||
function ModelRoute(options) {
|
||||
Route.apply(this, arguments);
|
||||
|
||||
this.options = options;
|
||||
|
||||
// model
|
||||
this.model = this.dependencies.model.schema;
|
||||
|
||||
// setup http routes
|
||||
this.setupRoutes(this.app);
|
||||
|
||||
debug('created with options', options);
|
||||
}
|
||||
|
||||
/**
|
||||
* Inherit from `Route`.
|
||||
*/
|
||||
|
||||
inherits(ModelRoute, Route);
|
||||
|
||||
/**
|
||||
* Dependencies.
|
||||
*/
|
||||
|
||||
ModelRoute.dependencies = {
|
||||
'model': 'model'
|
||||
}
|
||||
|
||||
ModelRoute.prototype.setupRoutes = function (app) {
|
||||
var Model = this.model;
|
||||
var ctx;
|
||||
var collection = this;
|
||||
|
||||
app.use(function (req, res, next) {
|
||||
ctx = collection.createContext(req, res, next);
|
||||
|
||||
ctx.emit('request', function () {
|
||||
ctx.emit(req.method.toLowerCase(), function () {
|
||||
next();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
// create
|
||||
app.post('/', create);
|
||||
app.post('/new', create);
|
||||
|
||||
function create(req, res, next) {
|
||||
ctx.emit('create', function (done) {
|
||||
Model.create(req.body, done);
|
||||
});
|
||||
}
|
||||
|
||||
// read
|
||||
app.get('/:id', query);
|
||||
app.get('/', query);
|
||||
|
||||
function query(req, res, next) {
|
||||
var query = req.query;
|
||||
query.where = query.where || {};
|
||||
|
||||
if(req.param('id')) {
|
||||
query.where.id = req.param('id');
|
||||
}
|
||||
if(query.where.id) {
|
||||
query.limit = 1;
|
||||
}
|
||||
|
||||
if(query.limit) query.limit = Number(query.limit);
|
||||
if(query.skip) query.skip = Number(query.skip);
|
||||
|
||||
ctx.emit('query', query, function (done) {
|
||||
Model.all(query, done);
|
||||
});
|
||||
}
|
||||
|
||||
// update
|
||||
app.put('/:id', function (req, res) {
|
||||
var body = req.body || {};
|
||||
var id = req.param('id');
|
||||
|
||||
if(Number(id) == id) {
|
||||
id = Number(id);
|
||||
}
|
||||
|
||||
ctx.emit('find', function () {
|
||||
Model.find(id, function (err, m) {
|
||||
if(err) {
|
||||
ctx.done(err);
|
||||
} else {
|
||||
ctx.emit('validate', body, function () {
|
||||
ctx.emit('update', body, function (done) {
|
||||
m.updateAttributes(body, done);
|
||||
});
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
// delete
|
||||
app.del('/:id', function () {
|
||||
var id = req.param('id');
|
||||
|
||||
ctx.emit('delete', id, function (done) {
|
||||
Model.destroy(id, done);
|
||||
});
|
||||
});
|
||||
}
|
|
@ -1,14 +0,0 @@
|
|||
{
|
||||
"name": "model-route",
|
||||
"description": "model-route",
|
||||
"version": "0.0.1",
|
||||
"scripts": {
|
||||
"test": "mocha"
|
||||
},
|
||||
"dependencies": {
|
||||
"debug": "latest"
|
||||
},
|
||||
"devDependencies": {
|
||||
"mocha": "latest"
|
||||
}
|
||||
}
|
|
@ -8,9 +8,10 @@
|
|||
"dependencies": {
|
||||
"debug": "latest",
|
||||
"express": "~3.1.1",
|
||||
"jugglingdb": "~0.2.0-30",
|
||||
"jugglingdb": "git@github.com:strongloop/jugglingdb.git",
|
||||
"merge": "~1.1.0",
|
||||
"sl-module-loader": "git+ssh://git@github.com:strongloop/sl-module-loader.git"
|
||||
"sl-remoting": "git+ssh://git@github.com:strongloop/sl-remoting.git"
|
||||
},
|
||||
"devDependencies": {
|
||||
"mocha": "latest"
|
||||
|
|
Loading…
Reference in New Issue