Add getType/getDefaultIdType from connectors
This commit is contained in:
parent
e65d21dcdb
commit
cf200a2e27
|
@ -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
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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.
|
||||
*
|
||||
|
|
16
lib/sql.js
16
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');
|
||||
};
|
||||
|
|
|
@ -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 () {
|
||||
|
|
Loading…
Reference in New Issue