From 787a6bcf4322214d58876ead226d877a49bc106d Mon Sep 17 00:00:00 2001 From: Ritchie Date: Fri, 24 May 2013 07:59:23 -0700 Subject: [PATCH] Integrate remoting and jugglingdb fork --- example/colors/app.js | 5 +- example/hello-world/app.js | 0 example/simple-data-source/app.js | 23 +++++ lib/application.js | 86 ++++++++++++------ lib/asteroid.js | 7 +- node_modules/model/lib/model.js | 141 +----------------------------- package.json | 7 +- 7 files changed, 96 insertions(+), 173 deletions(-) delete mode 100644 example/hello-world/app.js create mode 100644 example/simple-data-source/app.js diff --git a/example/colors/app.js b/example/colors/app.js index 83aea117..1e8dd9a2 100644 --- a/example/colors/app.js +++ b/example/colors/app.js @@ -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 diff --git a/example/hello-world/app.js b/example/hello-world/app.js deleted file mode 100644 index e69de29b..00000000 diff --git a/example/simple-data-source/app.js b/example/simple-data-source/app.js new file mode 100644 index 00000000..9b4f0569 --- /dev/null +++ b/example/simple-data-source/app.js @@ -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'); \ No newline at end of file diff --git a/lib/application.js b/lib/application.js index eb228127..0f3819b6 100644 --- a/lib/application.js +++ b/lib/application.js @@ -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)); } \ No newline at end of file diff --git a/lib/asteroid.js b/lib/asteroid.js index daeb052f..c5b90746 100644 --- a/lib/asteroid.js +++ b/lib/asteroid.js @@ -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; } diff --git a/node_modules/model/lib/model.js b/node_modules/model/lib/model.js index 615b8907..cf687a96 100644 --- a/node_modules/model/lib/model.js +++ b/node_modules/model/lib/model.js @@ -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'} -]; \ No newline at end of file +} \ No newline at end of file diff --git a/package.json b/package.json index 779e4c72..5e4304ed 100644 --- a/package.json +++ b/package.json @@ -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"