Integrate remoting and jugglingdb fork
This commit is contained in:
parent
279ce5b929
commit
787a6bcf43
|
@ -3,11 +3,10 @@ var app = asteroid();
|
|||
|
||||
app.use(asteroid.rest());
|
||||
|
||||
var Color = app.define('color');
|
||||
var Color = app.model('color');
|
||||
|
||||
Color.defineSchema({
|
||||
'id': 'Number',
|
||||
'name': 'String'
|
||||
'name': String
|
||||
});
|
||||
|
||||
Color
|
||||
|
|
|
@ -0,0 +1,23 @@
|
|||
var asteroid = require('../../');
|
||||
var app = asteroid();
|
||||
|
||||
app.use(asteroid.rest());
|
||||
|
||||
|
||||
var dataSource = app.dataSource('db', {adapter: 'memory'});
|
||||
|
||||
var Color = dataSource.define('color', {
|
||||
'name': String
|
||||
});
|
||||
|
||||
Color.create({name: 'red'});
|
||||
Color.create({name: 'green'});
|
||||
Color.create({name: 'blue'});
|
||||
|
||||
Color.all(function () {
|
||||
console.log(arguments);
|
||||
});
|
||||
|
||||
app.listen(3000);
|
||||
|
||||
console.log('a list of colors is available at http://localhost:300/colors');
|
|
@ -3,9 +3,11 @@
|
|||
*/
|
||||
|
||||
var Model = require('../node_modules/model/lib/model')
|
||||
, DataSource = require('../node_modules/data-source')
|
||||
, DataSource = require('jugglingdb').DataSource
|
||||
, ADL = require('jugglingdb').ADL
|
||||
, assert = require('assert')
|
||||
, RemoteObjects = require('sl-remoting');
|
||||
, RemoteObjects = require('sl-remoting')
|
||||
, i8n = require('inflection');
|
||||
|
||||
/**
|
||||
* Export the app prototype.
|
||||
|
@ -45,6 +47,14 @@ app.disuse = function (route) {
|
|||
|
||||
app.models = {};
|
||||
|
||||
/**
|
||||
* Get ADL.
|
||||
*/
|
||||
|
||||
app.adl = function () {
|
||||
return this._adl || (this._adl = new ADL())
|
||||
}
|
||||
|
||||
/**
|
||||
* Define a model.
|
||||
*
|
||||
|
@ -52,39 +62,64 @@ app.models = {};
|
|||
* @param options {Object}
|
||||
* @returns {Model}
|
||||
*/
|
||||
|
||||
|
||||
app.model =
|
||||
app.defineModel =
|
||||
app.define = function (name, options) {
|
||||
var remotes = this.remotes();
|
||||
app.define = function (name, properties, options) {
|
||||
var adl = this.adl();
|
||||
|
||||
var ModelCtor = adl.define(name, properties, options);
|
||||
|
||||
options = options || {};
|
||||
options.name = options.name || name;
|
||||
ModelCtor.dataSource = function (name) {
|
||||
var dataSource = app.dataSources[name];
|
||||
|
||||
dataSource.attach(this);
|
||||
};
|
||||
|
||||
BaseModel = options.extend || Model;
|
||||
ModelCtor.sharedCtor = function (id, fn) {
|
||||
this.find(id, fn);
|
||||
};
|
||||
ModelCtor.accepts = {arg: 'id', type: 'any'};
|
||||
|
||||
assert(options.name, 'a name is required to define a model');
|
||||
|
||||
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;
|
||||
return (app._models[ModelCtor.pluralModelName] = ModelCtor);
|
||||
}
|
||||
|
||||
/**
|
||||
* App data sources.
|
||||
* Get all models.
|
||||
*/
|
||||
|
||||
app.models = function () {
|
||||
var models = this._models;
|
||||
var dataSources = this.dataSources;
|
||||
|
||||
// add in any model from a data source
|
||||
Object.keys(this.dataSources).forEach(function (name) {
|
||||
var dataSource = dataSources[name];
|
||||
|
||||
Object.keys(dataSource.models).forEach(function (className) {
|
||||
var model = dataSource.models[className];
|
||||
models[i8n.pluralize(model.modelName)] = model;
|
||||
});
|
||||
});
|
||||
|
||||
return models;
|
||||
}
|
||||
|
||||
/**
|
||||
* App data sources and models.
|
||||
*/
|
||||
|
||||
app._models = {};
|
||||
app.dataSources = {};
|
||||
|
||||
/**
|
||||
* Get the apps set of remote objects.
|
||||
*/
|
||||
|
||||
app.remotes = function () {
|
||||
return this._remotes || (this._remotes = RemoteObjects.create());
|
||||
}
|
||||
|
||||
/**
|
||||
* Attach a remote data source.
|
||||
*
|
||||
|
@ -94,5 +129,6 @@ app.dataSources = {};
|
|||
*/
|
||||
|
||||
app.dataSource = function (name, options) {
|
||||
return (app[name] = new DataSource(options));
|
||||
var dataSources = this.dataSources || (this.dataSources = {});
|
||||
return (dataSources[name] = new DataSource(options.adapter, options));
|
||||
}
|
|
@ -6,7 +6,7 @@ var express = require('express')
|
|||
, fs = require('fs')
|
||||
, path = require('path')
|
||||
, proto = require('./application')
|
||||
, merge = require('merge');
|
||||
, utils = require('express/node_modules/connect').utils;
|
||||
|
||||
/**
|
||||
* Expose `createApplication()`.
|
||||
|
@ -35,8 +35,9 @@ asteroid.mime = express.mime;
|
|||
|
||||
function createApplication() {
|
||||
var app = express();
|
||||
merge(app, proto);
|
||||
|
||||
|
||||
utils.merge(app, proto);
|
||||
|
||||
return app;
|
||||
}
|
||||
|
||||
|
|
|
@ -84,7 +84,7 @@ Model.extend = function (options) {
|
|||
* Construct a model instance for remote use.
|
||||
*/
|
||||
|
||||
Model.remoteConstructor = function (data, fn) {
|
||||
Model.sharedCtor = function (data, fn) {
|
||||
var ModelCtor = this;
|
||||
|
||||
fn(null, new ModelCtor(data));
|
||||
|
@ -118,141 +118,4 @@ Model.dataSource = function (dataSourceName, namespace) {
|
|||
Model.defineMap = function (mapping) {
|
||||
// see: https://github.com/strongloop/asteroid/tree/master/node_modules/model#mymodeldefinemapmap
|
||||
throw new Error('not implemented');
|
||||
}
|
||||
|
||||
/**
|
||||
* Using the mapped data source, try to discover the schema of a table or other namespace (url, collection, etc).
|
||||
*/
|
||||
|
||||
Model.discoverSchema = function (fn) {
|
||||
throw new Error('not implemented');
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch a model by id.
|
||||
*/
|
||||
|
||||
Model.getById = function (id, fn) {
|
||||
var ModelCtor = this;
|
||||
var model = new ModelCtor({id: id});
|
||||
|
||||
model.fetch(fn);
|
||||
}
|
||||
Model.getById.remotable = true;
|
||||
Model.getById.returns = [
|
||||
{arg: 'model', 'type': 'this'}
|
||||
];
|
||||
|
||||
/**
|
||||
* Fetch the model data and update the instance.
|
||||
*
|
||||
* **Note:** this is a remotable instance method.
|
||||
*/
|
||||
|
||||
Model.prototype.fetch = function (fn) {
|
||||
throw new Error('not implemented');
|
||||
}
|
||||
Model.prototype.fetch.remotable = true;
|
||||
Model.prototype.fetch.returns = [
|
||||
{arg: 'model', 'type': 'this'}
|
||||
];
|
||||
|
||||
/**
|
||||
* Save the model data.
|
||||
*
|
||||
* **Note:** this is a remotable instance method.
|
||||
*/
|
||||
|
||||
Model.prototype.save = function (fn) {
|
||||
throw new Error('not implemented');
|
||||
}
|
||||
Model.prototype.save.remotable = true;
|
||||
Model.prototype.save.returns = [
|
||||
{arg: 'model', 'type': 'this'}
|
||||
];
|
||||
|
||||
/**
|
||||
* Helper method for creating and saving models.
|
||||
*/
|
||||
|
||||
Model.create = function (data, fn) {
|
||||
var ModelCtor = this;
|
||||
var model = new ModelCtor(data);
|
||||
|
||||
model.save(fn);
|
||||
|
||||
// for chaining
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the model.
|
||||
*
|
||||
* **Note:** this is a remotable instance method.
|
||||
*/
|
||||
|
||||
Model.prototype.destroy = function (fn) {
|
||||
throw new Error('not implemented');
|
||||
}
|
||||
Model.prototype.destroy.remotable = true;
|
||||
|
||||
/**
|
||||
* Find by id.
|
||||
*
|
||||
* **Note:** this is a remotable static method.
|
||||
*/
|
||||
|
||||
Model.findById = function (id, fn) {
|
||||
throw new Error('not implemented');
|
||||
}
|
||||
Model.findById.remotable = true;
|
||||
Model.findById.accepts = [
|
||||
{arg: 'id', type: 'Number'}
|
||||
];
|
||||
Model.findById.returns = [
|
||||
{arg: 'model', type: 'this'}
|
||||
];
|
||||
|
||||
/**
|
||||
* Find all models that match the given query.
|
||||
*
|
||||
* **Note:** this is a remotable static method.
|
||||
*/
|
||||
|
||||
Model.all = function (query, fn) {
|
||||
if(typeof query === 'function') {
|
||||
fn = query;
|
||||
query = undefined;
|
||||
}
|
||||
|
||||
throw new Error('not implemented');
|
||||
}
|
||||
Model.all.remotable = true;
|
||||
Model.all.accepts = [
|
||||
{arg: 'query', type: 'Object', optional: true}
|
||||
];
|
||||
Model.all.returns = [
|
||||
{arg: 'models', type: ['this']}
|
||||
];
|
||||
|
||||
/**
|
||||
* Count all models that match the given filter.
|
||||
*
|
||||
* **Note:** this is a remotable static method.
|
||||
*/
|
||||
|
||||
Model.count = function (query, fn) {
|
||||
if(typeof query === 'function') {
|
||||
fn = query;
|
||||
query = undefined;
|
||||
}
|
||||
|
||||
throw new Error('not implemented');
|
||||
}
|
||||
Model.all.remotable = true;
|
||||
Model.all.accepts = [
|
||||
{arg: 'query', type: 'Object', optional: true}
|
||||
];
|
||||
Model.all.returns = [
|
||||
{arg: 'count', type: 'Number'}
|
||||
];
|
||||
}
|
|
@ -8,10 +8,11 @@
|
|||
"dependencies": {
|
||||
"debug": "latest",
|
||||
"express": "~3.1.1",
|
||||
"jugglingdb": "git@github.com:strongloop/jugglingdb.git",
|
||||
"jugglingdb": "git+ssh://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"
|
||||
"sl-module-loader": "git+ssh://git@github.com:strongloop/sl-module-loader.git",
|
||||
"sl-remoting": "git+ssh://git@github.com:strongloop/sl-remoting.git",
|
||||
"inflection": "~1.2.5"
|
||||
},
|
||||
"devDependencies": {
|
||||
"mocha": "latest"
|
||||
|
|
Loading…
Reference in New Issue