Merge pull request #25 from strongloop/ds-attach
Fixes for data source juggler
This commit is contained in:
commit
8a615324d0
|
@ -412,9 +412,17 @@ DataSource.prototype.attach = function (ModelCtor) {
|
|||
// Already attached to the data source
|
||||
return;
|
||||
}
|
||||
var properties = ModelCtor.dataSource.definitions[ModelCtor.modelName].properties;
|
||||
var settings = ModelCtor.dataSource.definitions[ModelCtor.modelName].settings;
|
||||
var className = ModelCtor.modelName;
|
||||
var properties = ModelCtor.dataSource.definitions[className].properties;
|
||||
var settings = ModelCtor.dataSource.definitions[className].settings;
|
||||
|
||||
// redefine the dataSource
|
||||
ModelCtor.dataSource = this;
|
||||
// add to def
|
||||
var def = new ModelDefinition(this, className, properties, settings);
|
||||
def.build();
|
||||
this.definitions[className] = def;
|
||||
this.models[className] = ModelCtor;
|
||||
|
||||
this.mixin(ModelCtor);
|
||||
|
||||
|
@ -426,16 +434,7 @@ DataSource.prototype.attach = function (ModelCtor) {
|
|||
settings: settings
|
||||
});
|
||||
}
|
||||
|
||||
// redefine the dataSource
|
||||
hiddenProperty(ModelCtor, 'dataSource', this);
|
||||
ModelCtor.dataSource = this;
|
||||
|
||||
// add to def
|
||||
this.definitions[className] = new ModelDefinition(this, className, properties, settings);
|
||||
|
||||
this.models[className] = ModelCtor;
|
||||
|
||||
|
||||
return this;
|
||||
};
|
||||
|
||||
|
|
|
@ -275,7 +275,7 @@ ModelBuilder.prototype.define = function defineClass(className, properties, sett
|
|||
return new OrigDate(arg);
|
||||
};
|
||||
} else if(typeof DataType === 'string') {
|
||||
DataType = dataSource.getSchemaType(DataType);
|
||||
DataType = dataSource.resolveType(DataType);
|
||||
}
|
||||
|
||||
if(prop.required) {
|
||||
|
@ -449,6 +449,46 @@ ModelBuilder.prototype.getSchemaName = function (name) {
|
|||
return 'AnonymousModel_' + this._nameCount;
|
||||
};
|
||||
|
||||
/**
|
||||
* Resolve the type string to be a function, for example, 'String' to String
|
||||
* @param {String} type The type string, such as 'number', 'Number', 'boolean', or 'String'. It's case insensitive
|
||||
* @returns {Function} if the type is resolved
|
||||
*/
|
||||
ModelBuilder.prototype.resolveType = function(type) {
|
||||
if (!type) {
|
||||
return type;
|
||||
}
|
||||
if (Array.isArray(type) && type.length > 0) {
|
||||
// For array types, the first item should be the type string
|
||||
var itemType = this.resolveType(type[0]);
|
||||
if (typeof itemType === 'function') {
|
||||
return [itemType];
|
||||
}
|
||||
else {
|
||||
return itemType; // Not resolved, return the type string
|
||||
}
|
||||
}
|
||||
if (typeof type === 'string') {
|
||||
var schemaType = ModelBuilder.schemaTypes[type.toLowerCase()] || this.models[type];
|
||||
if (schemaType) {
|
||||
return schemaType;
|
||||
} else {
|
||||
return type;
|
||||
}
|
||||
} else if (type.constructor.name === 'Object') {
|
||||
// We also support the syntax {type: 'string', ...}
|
||||
if (type.type) {
|
||||
return this.resolveType(type.type);
|
||||
} else {
|
||||
return this.define(this.getSchemaName(null),
|
||||
type, {anonymous: true, idInjection: false});
|
||||
}
|
||||
} else if('function' === typeof type ) {
|
||||
return type;
|
||||
}
|
||||
return type;
|
||||
};
|
||||
|
||||
/**
|
||||
* Build models from dataSource definitions
|
||||
*
|
||||
|
|
|
@ -41,6 +41,7 @@ function ModelDefinition(modelBuilder, name, properties, settings) {
|
|||
}
|
||||
this.associations = [];
|
||||
this.properties = null;
|
||||
this.build();
|
||||
}
|
||||
|
||||
util.inherits(ModelDefinition, EventEmitter);
|
||||
|
@ -200,46 +201,6 @@ ModelDefinition.prototype.indexes = function () {
|
|||
return indexes;
|
||||
};
|
||||
|
||||
/**
|
||||
* Resolve the type string to be a function, for example, 'String' to String
|
||||
* @param {String} type The type string, such as 'number', 'Number', 'boolean', or 'String'. It's case insensitive
|
||||
* @returns {Function} if the type is resolved
|
||||
*/
|
||||
ModelDefinition.prototype.resolveType = function(type) {
|
||||
if (!type) {
|
||||
return type;
|
||||
}
|
||||
if (Array.isArray(type) && type.length > 0) {
|
||||
// For array types, the first item should be the type string
|
||||
var itemType = this.resolveType(type[0]);
|
||||
if (typeof itemType === 'function') {
|
||||
return [itemType];
|
||||
}
|
||||
else {
|
||||
return itemType; // Not resolved, return the type string
|
||||
}
|
||||
}
|
||||
if (typeof type === 'string') {
|
||||
var schemaType = ModelDefinition.schemaTypes[type.toLowerCase()] || this.modelBuilder.models[type];
|
||||
if (schemaType) {
|
||||
return schemaType;
|
||||
} else {
|
||||
return type;
|
||||
}
|
||||
} else if (type.constructor.name === 'Object') {
|
||||
// We also support the syntax {type: 'string', ...}
|
||||
if (type.type) {
|
||||
return this.resolveType(type.type);
|
||||
} else {
|
||||
return this.modelBuilder.define(this.modelBuilder.getSchemaName(null),
|
||||
type, {anonymous: true, idInjection: false});
|
||||
}
|
||||
} else if('function' === typeof type ) {
|
||||
return type;
|
||||
}
|
||||
return type;
|
||||
};
|
||||
|
||||
/**
|
||||
* Build a model definition
|
||||
* @param {Boolean} force Forcing rebuild
|
||||
|
@ -256,7 +217,7 @@ ModelDefinition.prototype.build = function (forceRebuild) {
|
|||
this.properties = {};
|
||||
for (var p in this.rawProperties) {
|
||||
var prop = this.rawProperties[p];
|
||||
var type = this.resolveType(prop);
|
||||
var type = this.modelBuilder.resolveType(prop);
|
||||
if (typeof type === 'string') {
|
||||
this.associations.push({
|
||||
source: this.name,
|
||||
|
|
|
@ -8,7 +8,7 @@ describe('basic-querying', function() {
|
|||
db = getSchema();
|
||||
|
||||
User = db.define('User', {
|
||||
name: {type: String, sort: true},
|
||||
name: {type: String, index: true, sort: true},
|
||||
email: {type: String, index: true},
|
||||
role: {type: String, index: true},
|
||||
order: {type: Number, index: true, sort: true}
|
||||
|
|
Loading…
Reference in New Issue