Merge pull request #39 from strongloop/model-builder

Separate the modelBuilder ref from dataSource
This commit is contained in:
Raymond Feng 2013-11-15 13:31:39 -08:00
commit e51c8971f9
2 changed files with 20 additions and 21 deletions

View File

@ -551,7 +551,7 @@ DataSource.prototype.attach = function (modelClass) {
return; return;
} }
var className = modelClass.modelName; var className = modelClass.modelName;
var modelDef = modelClass.dataSource.getModelDefinition(className); var modelDef = modelClass.modelBuilder.getModelDefinition(className);
var properties = modelDef.properties; var properties = modelDef.properties;
var settings = modelDef.settings; var settings = modelDef.settings;
@ -1444,7 +1444,7 @@ DataSource.prototype.disconnect = function disconnect(cb) {
DataSource.prototype.copyModel = function copyModel(Master) { DataSource.prototype.copyModel = function copyModel(Master) {
var dataSource = this; var dataSource = this;
var className = Master.modelName; var className = Master.modelName;
var md = Master.dataSource.getModelDefinition(className); var md = Master.modelBuilder.getModelDefinition(className);
var Slave = function SlaveModel() { var Slave = function SlaveModel() {
Master.apply(this, [].slice.call(arguments)); Master.apply(this, [].slice.call(arguments));
}; };

View File

@ -76,7 +76,7 @@ ModelBuilder.prototype.getModelDefinition = function(name) {
* *
* @example simple case * @example simple case
* ``` * ```
* var User = dataSource.define('User', { * var User = modelBuilder.define('User', {
* email: String, * email: String,
* password: String, * password: String,
* birthDate: Date, * birthDate: Date,
@ -85,7 +85,7 @@ ModelBuilder.prototype.getModelDefinition = function(name) {
* ``` * ```
* @example more advanced case * @example more advanced case
* ``` * ```
* var User = dataSource.define('User', { * var User = modelBuilder.define('User', {
* email: { type: String, limit: 150, index: true }, * email: { type: String, limit: 150, index: true },
* password: { type: String, limit: 50 }, * password: { type: String, limit: 50 },
* birthDate: Date, * birthDate: Date,
@ -95,7 +95,7 @@ ModelBuilder.prototype.getModelDefinition = function(name) {
* ``` * ```
*/ */
ModelBuilder.prototype.define = function defineClass(className, properties, settings, parent) { ModelBuilder.prototype.define = function defineClass(className, properties, settings, parent) {
var dataSource = this; var modelBuilder = this;
var args = slice.call(arguments); var args = slice.call(arguments);
var pluralName = settings && settings.plural; var pluralName = settings && settings.plural;
@ -140,7 +140,7 @@ ModelBuilder.prototype.define = function defineClass(className, properties, sett
// every class can receive hash of data as optional param // every class can receive hash of data as optional param
ModelClass = function ModelConstructor(data, dataSource) { ModelClass = function ModelConstructor(data, dataSource) {
if(!(this instanceof ModelConstructor)) { if(!(this instanceof ModelConstructor)) {
return new ModelConstructor(data, dataSource); return new ModelConstructor(data, modelBuilder);
} }
if(ModelClass.settings.unresolved) { if(ModelClass.settings.unresolved) {
throw new Error('Model ' + ModelClass.modelName + ' is not defined.'); throw new Error('Model ' + ModelClass.modelName + ' is not defined.');
@ -171,8 +171,7 @@ ModelBuilder.prototype.define = function defineClass(className, properties, sett
} }
// Add metadata to the ModelClass // Add metadata to the ModelClass
hiddenProperty(ModelClass, 'dataSource', dataSource); hiddenProperty(ModelClass, 'modelBuilder', modelBuilder);
hiddenProperty(ModelClass, 'schema', dataSource); // For backward compatibility
hiddenProperty(ModelClass, 'pluralModelName', pluralName || inflection.pluralize(className)); hiddenProperty(ModelClass, 'pluralModelName', pluralName || inflection.pluralize(className));
hiddenProperty(ModelClass, 'relations', {}); hiddenProperty(ModelClass, 'relations', {});
@ -290,7 +289,7 @@ ModelBuilder.prototype.define = function defineClass(className, properties, sett
}); });
// Define the subclass // Define the subclass
var subClass = dataSource.define(className, subclassProperties, subclassSettings, ModelClass); var subClass = modelBuilder.define(className, subclassProperties, subclassSettings, ModelClass);
// Calling the setup function // Calling the setup function
if(typeof subClass.setup === 'function') { if(typeof subClass.setup === 'function') {
@ -319,7 +318,7 @@ ModelBuilder.prototype.define = function defineClass(className, properties, sett
return new OrigDate(arg); return new OrigDate(arg);
}; };
} else if(typeof DataType === 'string') { } else if(typeof DataType === 'string') {
DataType = dataSource.resolveType(DataType); DataType = modelBuilder.resolveType(DataType);
} }
if(prop.required) { if(prop.required) {
@ -420,7 +419,7 @@ ModelBuilder.prototype.defineProperty = function (model, propertyName, propertyD
* db.defineProperty('Content', 'expiryDate', { type: Date, index: true }); * db.defineProperty('Content', 'expiryDate', { type: Date, index: true });
* db.defineProperty('Content', 'isExpired', { type: Boolean, index: true }); * db.defineProperty('Content', 'isExpired', { type: Boolean, index: true });
* *
* // dataSource.extend allows to * // modelBuilder.extend allows to
* // extend the content model with competition attributes * // extend the content model with competition attributes
* db.extendModel('Content', { * db.extendModel('Content', {
* competitionType: String, * competitionType: String,
@ -438,9 +437,9 @@ ModelBuilder.prototype.extendModel = function (model, props) {
ModelBuilder.prototype.copyModel = function copyModel(Master) { ModelBuilder.prototype.copyModel = function copyModel(Master) {
var dataSource = this; var modelBuilder = this;
var className = Master.modelName; var className = Master.modelName;
var md = Master.dataSource.definitions[className]; var md = Master.modelBuilder.definitions[className];
var Slave = function SlaveModel() { var Slave = function SlaveModel() {
Master.apply(this, [].slice.call(arguments)); Master.apply(this, [].slice.call(arguments));
}; };
@ -449,15 +448,15 @@ ModelBuilder.prototype.copyModel = function copyModel(Master) {
Slave.__proto__ = Master; Slave.__proto__ = Master;
hiddenProperty(Slave, 'dataSource', dataSource); hiddenProperty(Slave, 'modelBuilder', modelBuilder);
hiddenProperty(Slave, 'modelName', className); hiddenProperty(Slave, 'modelName', className);
hiddenProperty(Slave, 'relations', Master.relations); hiddenProperty(Slave, 'relations', Master.relations);
if (!(className in dataSource.models)) { if (!(className in modelBuilder.models)) {
// store class in model pool // store class in model pool
dataSource.models[className] = Slave; modelBuilder.models[className] = Slave;
dataSource.definitions[className] = { modelBuilder.definitions[className] = {
properties: md.properties, properties: md.properties,
settings: md.settings settings: md.settings
}; };
@ -537,12 +536,12 @@ ModelBuilder.prototype.resolveType = function(type) {
}; };
/** /**
* Build models from dataSource definitions * Build models from schema definitions
* *
* `schemas` can be one of the following: * `schemas` can be one of the following:
* *
* 1. An array of named dataSource definition JSON objects * 1. An array of named schema definition JSON objects
* 2. A dataSource definition JSON object * 2. A schema definition JSON object
* 3. A list of property definitions (anonymous) * 3. A list of property definitions (anonymous)
* *
* @param {*} schemas The schemas * @param {*} schemas The schemas
@ -557,7 +556,7 @@ ModelBuilder.prototype.buildModels = function (schemas) {
// Only one item // Only one item
schemas = [schemas]; schemas = [schemas];
} else { } else {
// Anonymous dataSource // Anonymous schema
schemas = [ schemas = [
{ {
name: this.getSchemaName(), name: this.getSchemaName(),