diff --git a/examples/datasource-app.js b/examples/datasource-app.js index 615f2d5c..b61f20d1 100644 --- a/examples/datasource-app.js +++ b/examples/datasource-app.js @@ -1,4 +1,5 @@ var DataSource = require('../../jugglingdb').DataSource; +var ADL = require('../../jugglingdb').ADL; var ds = new DataSource('memory'); // define models @@ -91,5 +92,21 @@ Article.create(function(e, article) { }); }); +// should be able to attach a data source to an existing model +var adl = new ADL(); +Color = adl.define('Color', { + name: String +}); + +// attach +ds.attach(Color); + +Color.create({name: 'red'}); +Color.create({name: 'green'}); +Color.create({name: 'blue'}); + +Color.all(function (err, colors) { + console.log(colors); +}); diff --git a/lib/adl.js b/lib/adl.js index 1dff997e..decb9529 100644 --- a/lib/adl.js +++ b/lib/adl.js @@ -329,7 +329,7 @@ function hiddenProperty(where, property, value) { Object.defineProperty(where, property, { writable: false, enumerable: false, - configurable: false, + configurable: true, value: value }); } diff --git a/lib/datasource.js b/lib/datasource.js index 0e774351..4210d104 100644 --- a/lib/datasource.js +++ b/lib/datasource.js @@ -241,17 +241,29 @@ DataSource.prototype.mixin = function (ModelCtor) { */ DataSource.prototype.attach = function (ModelCtor) { + var properties = ModelCtor.schema.definitions[ModelCtor.modelName].properties; + var settings = ModelCtor.schema.definitions[ModelCtor.modelName].settings; + this.mixin(ModelCtor); if(this.adapter) { - // pass control to adapter - this.adapter.define({ - model: ModelCtor, - properties: ModelCtor.properties, - settings: ModelCtor.settings - }); + // pass control to adapter + this.adapter.define({ + model: ModelCtor, + properties: properties, + settings: settings + }); } + // redefine the schema + hiddenProperty(ModelCtor, 'schema', this); + + // add to def + this.definitions[ModelCtor.modelName] = { + properties: properties, + settings: settings + }; + return this; } diff --git a/lib/model.js b/lib/model.js index a089cd0f..7abe0259 100644 --- a/lib/model.js +++ b/lib/model.js @@ -32,6 +32,7 @@ ModelBaseClass.prototype._initProperties = function (data, applySetters) { var self = this; var ctor = this.constructor; var ds = ctor.schema.definitions[ctor.modelName]; + var properties = ds.properties; data = data || {};