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 c57ad621..fb16dc2a 100644 --- a/lib/model-definition.js +++ b/lib/model-definition.js @@ -201,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 @@ -257,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,