diff --git a/lib/registry.js b/lib/registry.js index 8b581066..6e0289e2 100644 --- a/lib/registry.js +++ b/lib/registry.js @@ -239,6 +239,19 @@ registry.createDataSource = function (name, options) { var self = this; var ds = new DataSource(name, options, self.modelBuilder); ds.createModel = function (name, properties, settings) { + settings = settings || {}; + var BaseModel = settings.base || settings.super; + if (!BaseModel) { + // Check the connector types + var connectorTypes = ds.connector && ds.connector.getTypes(); + if (Array.isArray(connectorTypes) && connectorTypes.indexOf('db') !== -1) { + // Only set up the base model to PersistedModel if the connector is DB + BaseModel = self.PersistedModel; + } else { + BaseModel = self.Model; + } + settings.base = BaseModel; + } var ModelCtor = self.createModel(name, properties, settings); ModelCtor.attachTo(ds); return ModelCtor; diff --git a/test/data-source.test.js b/test/data-source.test.js index a1e0e126..b8354c04 100644 --- a/test/data-source.test.js +++ b/test/data-source.test.js @@ -33,6 +33,34 @@ describe('DataSource', function() { assert.isFunc(Color.prototype, 'updateAttributes'); assert.isFunc(Color.prototype, 'reload'); }); + + it("should honor settings.base", function() { + var Base = memory.createModel('base'); + var Color = memory.createModel('color', {name: String}, {base: Base}); + assert.equal(Color.super_, Base); + }); + + it("should use loopback.PersistedModel as the base for DBs", function() { + var Color = memory.createModel('color', {name: String}); + assert.equal(Color.super_, loopback.PersistedModel); + }); + + it("should use loopback.Model as the base for non DBs", function() { + // Mock up a non-DB connector + var Connector = function() { + }; + Connector.prototype.getTypes = function() { + return ['rest']; + }; + + var ds = loopback.createDataSource({ + connector: new Connector() + }); + + var Color = ds.createModel('color', {name: String}); + assert.equal(Color.super_, loopback.Model); + }); + }); describe.skip('PersistedModel Methods', function() {