Skip to content

Commit cf200a2

Browse files
author
Raymond Feng
committed
Add getType/getDefaultIdType from connectors
1 parent e65d21d commit cf200a2

5 files changed

Lines changed: 101 additions & 9 deletions

File tree

‎lib/connector.js‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,22 @@ function Connector(name, settings) {
1717
*/
1818
Connector.prototype.relational = false;
1919

20+
/*!
21+
* Get the connector type/subtype
22+
* @returns {String} The type for the connector
23+
*/
24+
Connector.prototype.getType = function() {
25+
return 'db/nosql';
26+
};
27+
28+
/**
29+
* Get the default data type for ID
30+
* @returns {Function} The default type for ID
31+
*/
32+
Connector.prototype.getDefaultIdType = function() {
33+
return String;
34+
};
35+
2036
/**
2137
* Execute a command with given parameters
2238
* @param {String} command The command such as SQL

‎lib/connectors/memory.js‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,14 @@ function Memory(m, settings) {
3535

3636
util.inherits(Memory, Connector);
3737

38+
Memory.prototype.getDefaultIdType = function() {
39+
return Number;
40+
};
41+
42+
Memory.prototype.getType = function() {
43+
return 'db/nosql/memory';
44+
};
45+
3846
Memory.prototype.connect = function (callback) {
3947
if (this.isTransaction) {
4048
this.onTransactionExec = callback;

‎lib/datasource.js‎

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -424,17 +424,27 @@ DataSource.prototype.defineRelations = function (modelClass, relations) {
424424

425425
/*!
426426
* Set up the data access functions from the data source
427-
* @param modelClass
428-
* @param settings
427+
* @param {Model} modelClass The model class
428+
* @param {Object} settings The settings object
429429
*/
430430
DataSource.prototype.setupDataAccess = function (modelClass, settings) {
431-
if (this.connector && this.connector.define) {
432-
// pass control to connector
433-
this.connector.define({
434-
model: modelClass,
435-
properties: modelClass.definition.properties,
436-
settings: settings
437-
});
431+
if (this.connector) {
432+
// Check if the id property should be generated
433+
var idName = modelClass.definition.idName();
434+
var idProp = modelClass.definition.properties[idName];
435+
if(idProp && idProp.generated && this.connector.getDefaultIdType) {
436+
// Set the default id type from connector's ability
437+
var idType = this.connector.getDefaultIdType() || String;
438+
idProp.type = idType;
439+
}
440+
if (this.connector.define) {
441+
// pass control to connector
442+
this.connector.define({
443+
model: modelClass,
444+
properties: modelClass.definition.properties,
445+
settings: settings
446+
});
447+
}
438448
}
439449

440450
// add data access objects
@@ -567,6 +577,14 @@ DataSource.prototype.getModelDefinition = function (name) {
567577
return this.modelBuilder.getModelDefinition(name);
568578
};
569579

580+
/**
581+
* Get the data source type
582+
* @returns {String} The data source type, such as db/relational, db/nosql, rest
583+
*/
584+
DataSource.prototype.getType = function() {
585+
return this.connector && this.connector.getType();
586+
};
587+
570588
/**
571589
* Attach an existing model to a data source.
572590
*

‎lib/sql.js‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,22 @@ util.inherits(BaseSQL, Connector);
1919
*/
2020
BaseSQL.prototype.relational = true;
2121

22+
/*!
23+
* Get the connector type/subtype
24+
* @returns {string}
25+
*/
26+
BaseSQL.prototype.getType = function() {
27+
return 'db/relational';
28+
};
29+
30+
/*!
31+
* Get the default data type for ID
32+
* @returns {Function}
33+
*/
34+
BaseSQL.prototype.getDefaultIdType = function() {
35+
return Number;
36+
};
37+
2238
BaseSQL.prototype.query = function () {
2339
throw new Error('query method should be declared in connector');
2440
};

‎test/loopback-dl.test.js‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,40 @@ describe('DataSource define model', function () {
416416
});
417417
});
418418

419+
it('injects id by default', function (done) {
420+
var ds = new ModelBuilder();
421+
422+
var User = ds.define('User', {});
423+
assert.deepEqual(User.definition.properties.id,
424+
{type: Number, id: 1, generated: true});
425+
426+
done();
427+
});
428+
429+
it('disables idInjection if the value is false', function (done) {
430+
var ds = new ModelBuilder();
431+
432+
var User1 = ds.define('User', {}, {idInjection: false});
433+
assert(!User1.definition.properties.id);
434+
done();
435+
});
436+
437+
it('updates generated id type by the connector', function (done) {
438+
var builder = new ModelBuilder();
439+
440+
var User = builder.define('User', {id: {type: String, generated: true, id: true}});
441+
assert.deepEqual(User.definition.properties.id,
442+
{type: String, id: 1, generated: true});
443+
444+
var ds = new DataSource('memory');// define models
445+
User.attachTo(ds);
446+
447+
assert.deepEqual(User.definition.properties.id,
448+
{type: Number, id: 1, generated: true});
449+
450+
done();
451+
});
452+
419453
});
420454

421455
describe('Load models with base', function () {

0 commit comments

Comments
 (0)