diff --git a/lib/datasource.js b/lib/datasource.js index 113f7b37..d59e2d65 100644 --- a/lib/datasource.js +++ b/lib/datasource.js @@ -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; }; diff --git a/lib/model-builder.js b/lib/model-builder.js index 3519e943..7e6980a6 100644 --- a/lib/model-builder.js +++ b/lib/model-builder.js @@ -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 * diff --git a/lib/model-definition.js b/lib/model-definition.js index 6fae9191..fb16dc2a 100644 --- a/lib/model-definition.js +++ b/lib/model-definition.js @@ -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, diff --git a/test/basic-querying.test.js b/test/basic-querying.test.js index 2c617a03..11267b0a 100644 --- a/test/basic-querying.test.js +++ b/test/basic-querying.test.js @@ -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}