Skip to content

Commit

Permalink
Add getType/getDefaultIdType from connectors
Browse files Browse the repository at this point in the history
  • Loading branch information
Raymond Feng committed Jan 30, 2014
1 parent e65d21d commit cf200a2
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 9 deletions.
16 changes: 16 additions & 0 deletions lib/connector.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions lib/connectors/memory.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
36 changes: 27 additions & 9 deletions lib/datasource.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
*
Expand Down
16 changes: 16 additions & 0 deletions lib/sql.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
};
Expand Down
34 changes: 34 additions & 0 deletions test/loopback-dl.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down

0 comments on commit cf200a2

Please sign in to comment.