diff --git a/lib/connector.js b/lib/connector.js index efb3843d..9372ef6d 100644 --- a/lib/connector.js +++ b/lib/connector.js @@ -17,6 +17,22 @@ function Connector(name, settings) { */ Connector.prototype.relational = false; +/*! + * Get the connector type/subtype + * @returns {String} The type for the connector + */ +Connector.prototype.getType = function() { + return 'db/nosql'; +}; + +/** + * Get the default data type for ID + * @returns {Function} The default type for ID + */ +Connector.prototype.getDefaultIdType = function() { + return String; +}; + /** * Execute a command with given parameters * @param {String} command The command such as SQL diff --git a/lib/connectors/memory.js b/lib/connectors/memory.js index 7c337d43..29e6dd8e 100644 --- a/lib/connectors/memory.js +++ b/lib/connectors/memory.js @@ -35,6 +35,14 @@ function Memory(m, settings) { util.inherits(Memory, Connector); +Memory.prototype.getDefaultIdType = function() { + return Number; +}; + +Memory.prototype.getType = function() { + return 'db/nosql/memory'; +}; + Memory.prototype.connect = function (callback) { if (this.isTransaction) { this.onTransactionExec = callback; diff --git a/lib/datasource.js b/lib/datasource.js index 8f11368c..c035b23b 100644 --- a/lib/datasource.js +++ b/lib/datasource.js @@ -424,17 +424,27 @@ DataSource.prototype.defineRelations = function (modelClass, relations) { /*! * Set up the data access functions from the data source - * @param modelClass - * @param settings + * @param {Model} modelClass The model class + * @param {Object} settings The settings object */ DataSource.prototype.setupDataAccess = function (modelClass, settings) { - if (this.connector && this.connector.define) { - // pass control to connector - this.connector.define({ - model: modelClass, - properties: modelClass.definition.properties, - settings: settings - }); + if (this.connector) { + // Check if the id property should be generated + var idName = modelClass.definition.idName(); + var idProp = modelClass.definition.properties[idName]; + if(idProp && idProp.generated && this.connector.getDefaultIdType) { + // Set the default id type from connector's ability + var idType = this.connector.getDefaultIdType() || String; + idProp.type = idType; + } + if (this.connector.define) { + // pass control to connector + this.connector.define({ + model: modelClass, + properties: modelClass.definition.properties, + settings: settings + }); + } } // add data access objects @@ -567,6 +577,14 @@ DataSource.prototype.getModelDefinition = function (name) { return this.modelBuilder.getModelDefinition(name); }; +/** + * Get the data source type + * @returns {String} The data source type, such as db/relational, db/nosql, rest + */ +DataSource.prototype.getType = function() { + return this.connector && this.connector.getType(); +}; + /** * Attach an existing model to a data source. * diff --git a/lib/sql.js b/lib/sql.js index b3b5c664..06e33dc5 100644 --- a/lib/sql.js +++ b/lib/sql.js @@ -19,6 +19,22 @@ util.inherits(BaseSQL, Connector); */ BaseSQL.prototype.relational = true; +/*! + * Get the connector type/subtype + * @returns {string} + */ +BaseSQL.prototype.getType = function() { + return 'db/relational'; +}; + +/*! + * Get the default data type for ID + * @returns {Function} + */ +BaseSQL.prototype.getDefaultIdType = function() { + return Number; +}; + BaseSQL.prototype.query = function () { throw new Error('query method should be declared in connector'); }; diff --git a/test/loopback-dl.test.js b/test/loopback-dl.test.js index 205ee4e1..49ac88ab 100644 --- a/test/loopback-dl.test.js +++ b/test/loopback-dl.test.js @@ -416,6 +416,40 @@ describe('DataSource define model', function () { }); }); + it('injects id by default', function (done) { + var ds = new ModelBuilder(); + + var User = ds.define('User', {}); + assert.deepEqual(User.definition.properties.id, + {type: Number, id: 1, generated: true}); + + done(); + }); + + it('disables idInjection if the value is false', function (done) { + var ds = new ModelBuilder(); + + var User1 = ds.define('User', {}, {idInjection: false}); + assert(!User1.definition.properties.id); + done(); + }); + + it('updates generated id type by the connector', function (done) { + var builder = new ModelBuilder(); + + var User = builder.define('User', {id: {type: String, generated: true, id: true}}); + assert.deepEqual(User.definition.properties.id, + {type: String, id: 1, generated: true}); + + var ds = new DataSource('memory');// define models + User.attachTo(ds); + + assert.deepEqual(User.definition.properties.id, + {type: Number, id: 1, generated: true}); + + done(); + }); + }); describe('Load models with base', function () {