2013-08-13 16:37:09 +00:00
|
|
|
/*!
|
2013-05-17 17:54:14 +00:00
|
|
|
* Module dependencies
|
|
|
|
*/
|
2013-05-24 15:02:58 +00:00
|
|
|
|
2013-10-02 05:14:21 +00:00
|
|
|
var inflection = require('inflection');
|
2013-05-17 17:54:14 +00:00
|
|
|
var EventEmitter = require('events').EventEmitter;
|
|
|
|
var util = require('util');
|
2013-11-08 01:11:17 +00:00
|
|
|
var assert = require('assert');
|
2013-10-02 05:14:21 +00:00
|
|
|
var DefaultModelBaseClass = require('./model.js');
|
|
|
|
var List = require('./list.js');
|
|
|
|
var ModelDefinition = require('./model-definition.js');
|
2013-05-17 17:54:14 +00:00
|
|
|
|
2013-08-07 21:51:32 +00:00
|
|
|
// Set up types
|
|
|
|
require('./types')(ModelBuilder);
|
|
|
|
|
|
|
|
var introspect = require('./introspection')(ModelBuilder);
|
|
|
|
|
2013-05-17 17:54:14 +00:00
|
|
|
/**
|
|
|
|
* Export public API
|
|
|
|
*/
|
2013-08-13 16:37:09 +00:00
|
|
|
exports.ModelBuilder = exports.Schema = ModelBuilder;
|
2013-05-20 23:05:59 +00:00
|
|
|
|
2013-08-13 16:37:09 +00:00
|
|
|
/*!
|
2013-05-17 17:54:14 +00:00
|
|
|
* Helpers
|
|
|
|
*/
|
|
|
|
var slice = Array.prototype.slice;
|
|
|
|
|
|
|
|
/**
|
2013-08-13 16:37:09 +00:00
|
|
|
* ModelBuilder - A builder to define data models
|
|
|
|
*
|
|
|
|
* @constructor
|
2013-05-17 17:54:14 +00:00
|
|
|
*/
|
2013-05-24 05:20:20 +00:00
|
|
|
function ModelBuilder() {
|
2013-05-17 17:54:14 +00:00
|
|
|
// create blank models pool
|
2013-08-13 16:37:09 +00:00
|
|
|
/**
|
|
|
|
* @property {Object} models Model constructors
|
|
|
|
*/
|
2013-05-17 17:54:14 +00:00
|
|
|
this.models = {};
|
2013-08-13 16:37:09 +00:00
|
|
|
/**
|
|
|
|
* @property {Object} definitions Definitions of the models
|
|
|
|
*/
|
2013-05-17 17:54:14 +00:00
|
|
|
this.definitions = {};
|
2013-08-09 22:16:32 +00:00
|
|
|
}
|
2013-05-17 17:54:14 +00:00
|
|
|
|
2013-10-02 05:14:21 +00:00
|
|
|
// Inherit from EventEmitter
|
2013-05-24 05:20:20 +00:00
|
|
|
util.inherits(ModelBuilder, EventEmitter);
|
2013-05-17 17:54:14 +00:00
|
|
|
|
2013-10-02 05:14:21 +00:00
|
|
|
// Create a default instance
|
|
|
|
ModelBuilder.defaultInstance = new ModelBuilder();
|
|
|
|
|
2013-11-08 01:11:17 +00:00
|
|
|
function isModelClass(cls) {
|
|
|
|
if(!cls) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return cls.prototype instanceof DefaultModelBaseClass;
|
|
|
|
}
|
|
|
|
|
2013-11-09 01:13:00 +00:00
|
|
|
ModelBuilder.prototype.getModel = function(name) {
|
|
|
|
return this.models[name];
|
|
|
|
};
|
|
|
|
|
|
|
|
ModelBuilder.prototype.getModelDefinition = function(name) {
|
|
|
|
return this.definitions[name];
|
|
|
|
};
|
|
|
|
|
2013-05-17 17:54:14 +00:00
|
|
|
/**
|
2013-08-13 16:37:09 +00:00
|
|
|
* Define a model class
|
2013-05-17 17:54:14 +00:00
|
|
|
*
|
|
|
|
* @param {String} className
|
|
|
|
* @param {Object} properties - hash of class properties in format
|
|
|
|
* `{property: Type, property2: Type2, ...}`
|
|
|
|
* or
|
|
|
|
* `{property: {type: Type}, property2: {type: Type2}, ...}`
|
|
|
|
* @param {Object} settings - other configuration of class
|
|
|
|
* @return newly created class
|
|
|
|
*
|
|
|
|
* @example simple case
|
|
|
|
* ```
|
2013-07-23 18:16:43 +00:00
|
|
|
* var User = dataSource.define('User', {
|
2013-05-17 17:54:14 +00:00
|
|
|
* email: String,
|
|
|
|
* password: String,
|
|
|
|
* birthDate: Date,
|
|
|
|
* activated: Boolean
|
|
|
|
* });
|
|
|
|
* ```
|
|
|
|
* @example more advanced case
|
|
|
|
* ```
|
2013-07-23 18:16:43 +00:00
|
|
|
* var User = dataSource.define('User', {
|
2013-05-17 17:54:14 +00:00
|
|
|
* email: { type: String, limit: 150, index: true },
|
|
|
|
* password: { type: String, limit: 50 },
|
|
|
|
* birthDate: Date,
|
|
|
|
* registrationDate: {type: Date, default: function () { return new Date }},
|
|
|
|
* activated: { type: Boolean, default: false }
|
|
|
|
* });
|
|
|
|
* ```
|
|
|
|
*/
|
2013-07-01 23:49:43 +00:00
|
|
|
ModelBuilder.prototype.define = function defineClass(className, properties, settings, parent) {
|
2013-07-23 18:16:43 +00:00
|
|
|
var dataSource = this;
|
2013-05-17 17:54:14 +00:00
|
|
|
var args = slice.call(arguments);
|
2013-05-31 17:27:52 +00:00
|
|
|
var pluralName = settings && settings.plural;
|
2013-05-17 17:54:14 +00:00
|
|
|
|
2013-08-09 22:16:32 +00:00
|
|
|
if (!className) {
|
|
|
|
throw new Error('Class name required');
|
|
|
|
}
|
|
|
|
if (args.length === 1) {
|
|
|
|
properties = {};
|
|
|
|
args.push(properties);
|
|
|
|
}
|
|
|
|
if (args.length === 2) {
|
|
|
|
settings = {};
|
|
|
|
args.push(settings);
|
|
|
|
}
|
2013-05-17 17:54:14 +00:00
|
|
|
|
|
|
|
properties = properties || {};
|
|
|
|
settings = settings || {};
|
|
|
|
|
2013-08-26 20:38:24 +00:00
|
|
|
// Set the strict mode to be false by default
|
|
|
|
if(settings.strict === undefined || settings.strict === null) {
|
|
|
|
settings.strict = false;
|
|
|
|
}
|
|
|
|
|
2013-11-08 01:11:17 +00:00
|
|
|
// Set up the base model class
|
|
|
|
var ModelBaseClass = parent || DefaultModelBaseClass;
|
|
|
|
var baseClass = settings.base || settings['super'];
|
|
|
|
if(baseClass) {
|
|
|
|
if(isModelClass(baseClass)) {
|
|
|
|
ModelBaseClass = baseClass;
|
|
|
|
} else {
|
|
|
|
ModelBaseClass = this.models[baseClass];
|
|
|
|
assert(ModelBaseClass, 'Base model is not found: ' + baseClass);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-05 06:53:02 +00:00
|
|
|
// Check if there is a unresolved model with the same name
|
|
|
|
var ModelClass = this.models[className];
|
|
|
|
|
2013-11-06 22:00:22 +00:00
|
|
|
// Create the ModelClass if it doesn't exist or it's resolved (override)
|
|
|
|
// TODO: [rfeng] We need to decide what names to use for built-in models such as User.
|
|
|
|
if(!ModelClass || !ModelClass.settings.unresolved) {
|
2013-11-05 06:53:02 +00:00
|
|
|
// every class can receive hash of data as optional param
|
|
|
|
ModelClass = function ModelConstructor(data, dataSource) {
|
|
|
|
if(!(this instanceof ModelConstructor)) {
|
|
|
|
return new ModelConstructor(data, dataSource);
|
|
|
|
}
|
|
|
|
if(ModelClass.settings.unresolved) {
|
|
|
|
throw new Error('Model ' + ModelClass.modelName + ' is not defined.');
|
|
|
|
}
|
|
|
|
ModelBaseClass.apply(this, arguments);
|
|
|
|
if(dataSource) {
|
|
|
|
hiddenProperty(this, '__dataSource', dataSource);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
// mix in EventEmitter (don't inherit from)
|
|
|
|
var events = new EventEmitter();
|
|
|
|
for (var f in EventEmitter.prototype) {
|
|
|
|
if (typeof EventEmitter.prototype[f] === 'function') {
|
2013-11-12 06:05:50 +00:00
|
|
|
ModelClass[f] = EventEmitter.prototype[f].bind(events);
|
2013-11-05 06:53:02 +00:00
|
|
|
}
|
2013-07-03 03:28:00 +00:00
|
|
|
}
|
2013-11-05 06:53:02 +00:00
|
|
|
util.inherits(ModelClass, ModelBaseClass);
|
|
|
|
hiddenProperty(ModelClass, 'modelName', className);
|
|
|
|
}
|
2013-10-02 22:18:50 +00:00
|
|
|
|
2013-11-05 06:53:02 +00:00
|
|
|
// store class in model pool
|
|
|
|
this.models[className] = ModelClass;
|
|
|
|
|
|
|
|
// Return the unresolved model
|
|
|
|
if(settings.unresolved) {
|
|
|
|
ModelClass.settings = {unresolved: true};
|
|
|
|
return ModelClass;
|
2013-10-02 22:18:50 +00:00
|
|
|
}
|
2013-05-17 17:54:14 +00:00
|
|
|
|
2013-10-02 22:18:50 +00:00
|
|
|
// Add metadata to the ModelClass
|
2013-07-23 18:16:43 +00:00
|
|
|
hiddenProperty(ModelClass, 'dataSource', dataSource);
|
2013-07-23 19:16:12 +00:00
|
|
|
hiddenProperty(ModelClass, 'schema', dataSource); // For backward compatibility
|
2013-10-02 05:14:21 +00:00
|
|
|
hiddenProperty(ModelClass, 'pluralModelName', pluralName || inflection.pluralize(className));
|
2013-05-17 17:54:14 +00:00
|
|
|
hiddenProperty(ModelClass, 'relations', {});
|
|
|
|
|
2013-07-16 01:22:49 +00:00
|
|
|
// inherit ModelBaseClass static methods
|
2013-05-17 17:54:14 +00:00
|
|
|
for (var i in ModelBaseClass) {
|
2013-11-12 06:05:50 +00:00
|
|
|
// We need to skip properties that are already in the subclass, for example, the event emitter methods
|
|
|
|
if(i !== '_mixins' && !(i in ModelClass)) {
|
|
|
|
ModelClass[i] = ModelBaseClass[i];
|
|
|
|
}
|
2013-05-17 17:54:14 +00:00
|
|
|
}
|
2013-07-15 17:44:34 +00:00
|
|
|
|
2013-05-17 17:54:14 +00:00
|
|
|
ModelClass.getter = {};
|
|
|
|
ModelClass.setter = {};
|
|
|
|
|
2013-10-02 05:14:21 +00:00
|
|
|
var modelDefinition = new ModelDefinition(this, className, properties, settings);
|
2013-11-05 06:53:02 +00:00
|
|
|
|
2013-10-02 22:18:50 +00:00
|
|
|
this.definitions[className] = modelDefinition;
|
2013-06-28 01:25:42 +00:00
|
|
|
|
|
|
|
// expose properties on the ModelClass
|
2013-10-02 05:14:21 +00:00
|
|
|
ModelClass.definition = modelDefinition;
|
2013-10-04 22:48:39 +00:00
|
|
|
// keep a pointer to settings as models can use it for configuration
|
|
|
|
ModelClass.settings = modelDefinition.settings;
|
2013-05-17 17:54:14 +00:00
|
|
|
|
2013-05-22 17:41:08 +00:00
|
|
|
var idInjection = settings.idInjection;
|
2013-05-23 17:46:01 +00:00
|
|
|
if(idInjection !== false) {
|
2013-10-02 22:18:50 +00:00
|
|
|
// Default to true if undefined
|
2013-05-23 17:46:01 +00:00
|
|
|
idInjection = true;
|
|
|
|
}
|
2013-10-02 22:18:50 +00:00
|
|
|
|
|
|
|
var idNames = modelDefinition.idNames();
|
|
|
|
if(idNames.length > 0) {
|
|
|
|
// id already exists
|
|
|
|
idInjection = false;
|
2013-05-22 17:41:08 +00:00
|
|
|
}
|
2013-10-02 22:18:50 +00:00
|
|
|
|
2013-05-22 17:41:08 +00:00
|
|
|
// Add the id property
|
2013-05-23 17:46:01 +00:00
|
|
|
if (idInjection) {
|
2013-05-22 17:41:08 +00:00
|
|
|
// Set up the id property
|
2013-10-02 22:18:50 +00:00
|
|
|
ModelClass.definition.defineProperty('id', { type: Number, id: 1, generated: true });
|
|
|
|
}
|
|
|
|
|
|
|
|
idNames = modelDefinition.idNames(); // Reload it after rebuild
|
|
|
|
// Create a virtual property 'id'
|
|
|
|
if (idNames.length === 1) {
|
|
|
|
var idProp = idNames[0];
|
|
|
|
if (idProp !== 'id') {
|
|
|
|
Object.defineProperty(ModelClass.prototype, 'id', {
|
|
|
|
get: function () {
|
|
|
|
var idProp = ModelClass.definition.idNames[0];
|
|
|
|
return this.__data[idProp];
|
|
|
|
},
|
|
|
|
configurable: true,
|
|
|
|
enumerable: false
|
|
|
|
});
|
2013-05-22 17:41:08 +00:00
|
|
|
}
|
2013-10-02 22:18:50 +00:00
|
|
|
} else {
|
|
|
|
// Now the id property is an object that consists of multiple keys
|
|
|
|
Object.defineProperty(ModelClass.prototype, 'id', {
|
|
|
|
get: function () {
|
|
|
|
var compositeId = {};
|
|
|
|
var idNames = ModelClass.definition.idNames();
|
|
|
|
for (var p in idNames) {
|
|
|
|
compositeId[p] = this.__data[p];
|
|
|
|
}
|
|
|
|
return compositeId;
|
|
|
|
},
|
|
|
|
configurable: true,
|
|
|
|
enumerable: false
|
|
|
|
});
|
2013-05-22 17:41:08 +00:00
|
|
|
}
|
2013-05-17 17:54:14 +00:00
|
|
|
|
2013-10-02 22:18:50 +00:00
|
|
|
// A function to loop through the properties
|
2013-05-17 17:54:14 +00:00
|
|
|
ModelClass.forEachProperty = function (cb) {
|
2013-10-02 22:18:50 +00:00
|
|
|
Object.keys(ModelClass.definition.properties).forEach(cb);
|
2013-05-17 17:54:14 +00:00
|
|
|
};
|
|
|
|
|
2013-10-02 22:18:50 +00:00
|
|
|
// A function to attach the model class to a data source
|
2013-06-12 22:45:31 +00:00
|
|
|
ModelClass.attachTo = function (dataSource) {
|
2013-06-21 21:15:24 +00:00
|
|
|
dataSource.attach(this);
|
2013-08-09 22:16:32 +00:00
|
|
|
};
|
2013-10-02 22:18:50 +00:00
|
|
|
|
|
|
|
// A function to extend the model
|
2013-10-02 05:14:21 +00:00
|
|
|
ModelClass.extend = function (className, subclassProperties, subclassSettings) {
|
2013-10-02 22:18:50 +00:00
|
|
|
var properties = ModelClass.definition.properties;
|
|
|
|
var settings = ModelClass.definition.settings;
|
|
|
|
|
2013-10-02 05:14:21 +00:00
|
|
|
subclassProperties = subclassProperties || {};
|
|
|
|
subclassSettings = subclassSettings || {};
|
2013-10-07 04:13:52 +00:00
|
|
|
|
|
|
|
// Check if subclass redefines the ids
|
|
|
|
var idFound = false;
|
|
|
|
for(var k in subclassProperties) {
|
|
|
|
if(subclassProperties[k].id) {
|
|
|
|
idFound = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-02 22:18:50 +00:00
|
|
|
// Merging the properties
|
2013-07-01 23:49:43 +00:00
|
|
|
Object.keys(properties).forEach(function (key) {
|
2013-10-07 04:13:52 +00:00
|
|
|
if(idFound && properties[key].id) {
|
|
|
|
// don't inherit id properties
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if(subclassProperties[key] === undefined) {
|
2013-10-02 05:14:21 +00:00
|
|
|
subclassProperties[key] = properties[key];
|
2013-07-01 23:49:43 +00:00
|
|
|
}
|
|
|
|
});
|
2013-10-02 22:18:50 +00:00
|
|
|
|
|
|
|
// Merge the settings
|
2013-07-01 23:49:43 +00:00
|
|
|
Object.keys(settings).forEach(function (key) {
|
2013-10-07 04:13:52 +00:00
|
|
|
if(subclassSettings[key] === undefined) {
|
2013-10-02 05:14:21 +00:00
|
|
|
subclassSettings[key] = settings[key];
|
2013-07-01 23:49:43 +00:00
|
|
|
}
|
|
|
|
});
|
2013-10-02 22:18:50 +00:00
|
|
|
|
|
|
|
// Define the subclass
|
|
|
|
var subClass = dataSource.define(className, subclassProperties, subclassSettings, ModelClass);
|
|
|
|
|
|
|
|
// Calling the setup function
|
|
|
|
if(typeof subClass.setup === 'function') {
|
|
|
|
subClass.setup.call(subClass);
|
2013-07-03 03:28:00 +00:00
|
|
|
}
|
|
|
|
|
2013-10-02 22:18:50 +00:00
|
|
|
return subClass;
|
2013-08-09 22:16:32 +00:00
|
|
|
};
|
2013-06-12 22:45:31 +00:00
|
|
|
|
2013-10-02 05:14:21 +00:00
|
|
|
/**
|
|
|
|
* Register a property for the model class
|
|
|
|
* @param propertyName
|
|
|
|
*/
|
|
|
|
ModelClass.registerProperty = function (propertyName) {
|
2013-10-02 22:18:50 +00:00
|
|
|
var properties = modelDefinition.build();
|
2013-10-02 05:14:21 +00:00
|
|
|
var prop = properties[propertyName];
|
2013-07-28 20:17:12 +00:00
|
|
|
var DataType = prop.type;
|
2013-05-17 17:54:14 +00:00
|
|
|
if(!DataType) {
|
2013-10-02 05:14:21 +00:00
|
|
|
throw new Error('Invalid type for property ' + propertyName);
|
2013-05-17 17:54:14 +00:00
|
|
|
}
|
2013-07-26 20:06:43 +00:00
|
|
|
if (Array.isArray(DataType) || DataType === Array) {
|
2013-05-17 17:54:14 +00:00
|
|
|
DataType = List;
|
|
|
|
} else if (DataType.name === 'Date') {
|
|
|
|
var OrigDate = Date;
|
|
|
|
DataType = function Date(arg) {
|
|
|
|
return new OrigDate(arg);
|
|
|
|
};
|
2013-06-17 23:42:13 +00:00
|
|
|
} else if(typeof DataType === 'string') {
|
2013-10-17 21:23:29 +00:00
|
|
|
DataType = dataSource.resolveType(DataType);
|
2013-05-17 17:54:14 +00:00
|
|
|
}
|
2013-07-28 20:17:12 +00:00
|
|
|
|
|
|
|
if(prop.required) {
|
|
|
|
var requiredOptions = typeof prop.required === 'object' ? prop.required : undefined;
|
2013-10-02 05:14:21 +00:00
|
|
|
ModelClass.validatesPresenceOf(propertyName, requiredOptions);
|
2013-07-28 20:17:12 +00:00
|
|
|
}
|
2013-05-17 17:54:14 +00:00
|
|
|
|
2013-10-02 05:14:21 +00:00
|
|
|
Object.defineProperty(ModelClass.prototype, propertyName, {
|
2013-05-17 17:54:14 +00:00
|
|
|
get: function () {
|
2013-10-02 05:14:21 +00:00
|
|
|
if (ModelClass.getter[propertyName]) {
|
|
|
|
return ModelClass.getter[propertyName].call(this); // Try getter first
|
2013-05-17 17:54:14 +00:00
|
|
|
} else {
|
2013-10-02 05:14:21 +00:00
|
|
|
return this.__data && this.__data[propertyName]; // Try __data
|
2013-05-17 17:54:14 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
set: function (value) {
|
2013-10-02 05:14:21 +00:00
|
|
|
if (ModelClass.setter[propertyName]) {
|
|
|
|
ModelClass.setter[propertyName].call(this, value); // Try setter first
|
2013-05-17 17:54:14 +00:00
|
|
|
} else {
|
2013-05-28 20:50:59 +00:00
|
|
|
if (!this.__data) {
|
|
|
|
this.__data = {};
|
|
|
|
}
|
2013-05-17 17:54:14 +00:00
|
|
|
if (value === null || value === undefined) {
|
2013-10-02 05:14:21 +00:00
|
|
|
this.__data[propertyName] = value;
|
2013-05-17 17:54:14 +00:00
|
|
|
} else {
|
2013-07-12 19:36:14 +00:00
|
|
|
if(DataType === List) {
|
2013-10-02 05:14:21 +00:00
|
|
|
this.__data[propertyName] = DataType(value, properties[propertyName].type, this.__data);
|
2013-07-12 19:36:14 +00:00
|
|
|
} else {
|
2013-07-25 14:48:31 +00:00
|
|
|
// Assume the type constructor handles Constructor() call
|
|
|
|
// If not, we should call new DataType(value).valueOf();
|
2013-10-02 05:14:21 +00:00
|
|
|
this.__data[propertyName] = DataType(value);
|
2013-07-12 19:36:14 +00:00
|
|
|
}
|
2013-05-17 17:54:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
configurable: true,
|
|
|
|
enumerable: true
|
|
|
|
});
|
|
|
|
|
2013-10-02 05:14:21 +00:00
|
|
|
// <propertyName>$was --> __dataWas.<propertyName>
|
|
|
|
Object.defineProperty(ModelClass.prototype, propertyName + '$was', {
|
|
|
|
get: function () {
|
|
|
|
return this.__dataWas && this.__dataWas[propertyName];
|
|
|
|
},
|
|
|
|
configurable: true,
|
|
|
|
enumerable: false
|
2013-05-17 17:54:14 +00:00
|
|
|
});
|
|
|
|
|
2013-08-30 23:51:17 +00:00
|
|
|
// FIXME: [rfeng] Do we need to keep the raw data?
|
|
|
|
// Use $ as the prefix to avoid conflicts with properties such as _id
|
2013-10-02 05:14:21 +00:00
|
|
|
Object.defineProperty(ModelClass.prototype, '$' + propertyName, {
|
2013-05-17 17:54:14 +00:00
|
|
|
get: function () {
|
2013-10-02 05:14:21 +00:00
|
|
|
return this.__data && this.__data[propertyName];
|
2013-05-17 17:54:14 +00:00
|
|
|
},
|
|
|
|
set: function (value) {
|
2013-05-28 20:50:59 +00:00
|
|
|
if (!this.__data) {
|
|
|
|
this.__data = {};
|
|
|
|
}
|
2013-10-02 05:14:21 +00:00
|
|
|
this.__data[propertyName] = value;
|
2013-05-17 17:54:14 +00:00
|
|
|
},
|
|
|
|
configurable: true,
|
|
|
|
enumerable: false
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
ModelClass.forEachProperty(ModelClass.registerProperty);
|
|
|
|
|
2013-11-05 06:53:02 +00:00
|
|
|
ModelClass.emit('defined', ModelClass);
|
|
|
|
|
2013-05-17 17:54:14 +00:00
|
|
|
return ModelClass;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2013-10-02 05:14:21 +00:00
|
|
|
* Define single property named `propertyName` on `model`
|
2013-05-17 17:54:14 +00:00
|
|
|
*
|
|
|
|
* @param {String} model - name of model
|
2013-10-02 05:14:21 +00:00
|
|
|
* @param {String} propertyName - name of property
|
|
|
|
* @param {Object} propertyDefinition - property settings
|
2013-05-17 17:54:14 +00:00
|
|
|
*/
|
2013-10-02 05:14:21 +00:00
|
|
|
ModelBuilder.prototype.defineProperty = function (model, propertyName, propertyDefinition) {
|
2013-10-02 22:18:50 +00:00
|
|
|
this.definitions[model].defineProperty(propertyName, propertyDefinition);
|
2013-10-02 05:14:21 +00:00
|
|
|
this.models[model].registerProperty(propertyName);
|
2013-05-17 17:54:14 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Extend existing model with bunch of properties
|
|
|
|
*
|
|
|
|
* @param {String} model - name of model
|
|
|
|
* @param {Object} props - hash of properties
|
|
|
|
*
|
|
|
|
* Example:
|
|
|
|
*
|
|
|
|
* // Instead of doing this:
|
|
|
|
*
|
|
|
|
* // amend the content model with competition attributes
|
|
|
|
* db.defineProperty('Content', 'competitionType', { type: String });
|
|
|
|
* db.defineProperty('Content', 'expiryDate', { type: Date, index: true });
|
|
|
|
* db.defineProperty('Content', 'isExpired', { type: Boolean, index: true });
|
|
|
|
*
|
2013-07-23 18:16:43 +00:00
|
|
|
* // dataSource.extend allows to
|
2013-05-17 17:54:14 +00:00
|
|
|
* // extend the content model with competition attributes
|
|
|
|
* db.extendModel('Content', {
|
|
|
|
* competitionType: String,
|
|
|
|
* expiryDate: { type: Date, index: true },
|
|
|
|
* isExpired: { type: Boolean, index: true }
|
|
|
|
* });
|
|
|
|
*/
|
2013-05-24 05:20:20 +00:00
|
|
|
ModelBuilder.prototype.extendModel = function (model, props) {
|
2013-05-17 17:54:14 +00:00
|
|
|
var t = this;
|
|
|
|
Object.keys(props).forEach(function (propName) {
|
|
|
|
var definition = props[propName];
|
|
|
|
t.defineProperty(model, propName, definition);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2013-05-24 05:20:20 +00:00
|
|
|
ModelBuilder.prototype.copyModel = function copyModel(Master) {
|
2013-07-23 18:16:43 +00:00
|
|
|
var dataSource = this;
|
2013-05-17 17:54:14 +00:00
|
|
|
var className = Master.modelName;
|
2013-07-23 18:16:43 +00:00
|
|
|
var md = Master.dataSource.definitions[className];
|
2013-05-17 17:54:14 +00:00
|
|
|
var Slave = function SlaveModel() {
|
|
|
|
Master.apply(this, [].slice.call(arguments));
|
|
|
|
};
|
|
|
|
|
|
|
|
util.inherits(Slave, Master);
|
|
|
|
|
|
|
|
Slave.__proto__ = Master;
|
|
|
|
|
2013-07-23 18:16:43 +00:00
|
|
|
hiddenProperty(Slave, 'dataSource', dataSource);
|
2013-05-17 17:54:14 +00:00
|
|
|
hiddenProperty(Slave, 'modelName', className);
|
|
|
|
hiddenProperty(Slave, 'relations', Master.relations);
|
|
|
|
|
2013-07-23 18:16:43 +00:00
|
|
|
if (!(className in dataSource.models)) {
|
2013-05-17 17:54:14 +00:00
|
|
|
|
|
|
|
// store class in model pool
|
2013-07-23 18:16:43 +00:00
|
|
|
dataSource.models[className] = Slave;
|
|
|
|
dataSource.definitions[className] = {
|
2013-05-17 17:54:14 +00:00
|
|
|
properties: md.properties,
|
|
|
|
settings: md.settings
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return Slave;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2013-08-13 16:37:09 +00:00
|
|
|
/*!
|
2013-05-17 17:54:14 +00:00
|
|
|
* Define hidden property
|
|
|
|
*/
|
|
|
|
function hiddenProperty(where, property, value) {
|
|
|
|
Object.defineProperty(where, property, {
|
2013-07-02 21:59:24 +00:00
|
|
|
writable: true,
|
2013-05-17 17:54:14 +00:00
|
|
|
enumerable: false,
|
2013-05-24 00:29:03 +00:00
|
|
|
configurable: true,
|
2013-05-17 17:54:14 +00:00
|
|
|
value: value
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2013-05-24 05:20:20 +00:00
|
|
|
/**
|
2013-10-02 22:18:50 +00:00
|
|
|
* Get the schema name
|
2013-05-24 05:20:20 +00:00
|
|
|
*/
|
2013-10-02 22:18:50 +00:00
|
|
|
ModelBuilder.prototype.getSchemaName = function (name) {
|
|
|
|
if (name) {
|
|
|
|
return name;
|
2013-05-24 05:20:20 +00:00
|
|
|
}
|
2013-10-02 22:18:50 +00:00
|
|
|
if (typeof this._nameCount !== 'number') {
|
|
|
|
this._nameCount = 0;
|
|
|
|
} else {
|
|
|
|
this._nameCount++;
|
2013-05-24 05:20:20 +00:00
|
|
|
}
|
2013-10-02 22:18:50 +00:00
|
|
|
return 'AnonymousModel_' + this._nameCount;
|
2013-08-09 22:16:32 +00:00
|
|
|
};
|
2013-05-24 05:20:20 +00:00
|
|
|
|
2013-10-17 21:23:29 +00:00
|
|
|
/**
|
|
|
|
* 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 {
|
2013-11-05 06:53:02 +00:00
|
|
|
// The type cannot be resolved, let's create a place holder
|
|
|
|
type = this.define(type, {}, {unresolved: true});
|
2013-10-17 21:23:29 +00:00
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
2013-05-24 05:20:20 +00:00
|
|
|
/**
|
2013-07-23 18:16:43 +00:00
|
|
|
* Build models from dataSource definitions
|
2013-08-13 16:37:09 +00:00
|
|
|
*
|
|
|
|
* `schemas` can be one of the following:
|
|
|
|
*
|
2013-07-23 18:16:43 +00:00
|
|
|
* 1. An array of named dataSource definition JSON objects
|
|
|
|
* 2. A dataSource definition JSON object
|
2013-05-24 05:20:20 +00:00
|
|
|
* 3. A list of property definitions (anonymous)
|
2013-08-13 16:37:09 +00:00
|
|
|
*
|
|
|
|
* @param {*} schemas The schemas
|
2013-05-24 05:20:20 +00:00
|
|
|
* @returns {Object} A map of model constructors keyed by model name
|
|
|
|
*/
|
2013-05-31 17:25:11 +00:00
|
|
|
ModelBuilder.prototype.buildModels = function (schemas) {
|
2013-05-24 05:20:20 +00:00
|
|
|
var models = {};
|
|
|
|
|
2013-10-02 22:18:50 +00:00
|
|
|
// Normalize the schemas to be an array of the schema objects {name: <name>, properties: {}, options: {}}
|
|
|
|
if (!Array.isArray(schemas)) {
|
|
|
|
if (schemas.properties && schemas.name) {
|
|
|
|
// Only one item
|
|
|
|
schemas = [schemas];
|
|
|
|
} else {
|
|
|
|
// Anonymous dataSource
|
|
|
|
schemas = [
|
|
|
|
{
|
|
|
|
name: this.getSchemaName(),
|
|
|
|
properties: schemas,
|
|
|
|
options: {anonymous: true}
|
|
|
|
}
|
|
|
|
];
|
|
|
|
}
|
2013-05-24 05:20:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var associations = [];
|
|
|
|
for (var s in schemas) {
|
2013-10-02 22:18:50 +00:00
|
|
|
var name = this.getSchemaName(schemas[s].name);
|
|
|
|
schemas[s].name = name;
|
|
|
|
var model = this.define(schemas[s].name, schemas[s].properties, schemas[s].options);
|
2013-05-31 17:25:11 +00:00
|
|
|
models[name] = model;
|
2013-10-02 22:18:50 +00:00
|
|
|
associations = associations.concat(model.definition.associations);
|
2013-05-24 05:20:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Connect the models based on the associations
|
|
|
|
for (var i = 0; i < associations.length; i++) {
|
|
|
|
var association = associations[i];
|
2013-05-31 17:25:11 +00:00
|
|
|
var sourceModel = models[association.source];
|
|
|
|
var targetModel = models[association.target];
|
2013-05-24 05:20:20 +00:00
|
|
|
if (sourceModel && targetModel) {
|
|
|
|
if(typeof sourceModel[association.relation] === 'function') {
|
|
|
|
sourceModel[association.relation](targetModel, {as: association.as});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return models;
|
2013-08-09 22:16:32 +00:00
|
|
|
};
|
2013-05-24 05:20:20 +00:00
|
|
|
|
2013-08-07 21:51:32 +00:00
|
|
|
/**
|
|
|
|
* Introspect the json document to build a corresponding model
|
2013-08-13 16:37:09 +00:00
|
|
|
* @param {String} name The model name
|
|
|
|
* @param {Object} json The json object
|
|
|
|
* @param [Object} options The options
|
2013-08-07 21:51:32 +00:00
|
|
|
* @returns {}
|
|
|
|
*/
|
|
|
|
ModelBuilder.prototype.buildModelFromInstance = function(name, json, options) {
|
|
|
|
|
|
|
|
// Introspect the JSON document to generate a schema
|
|
|
|
var schema = introspect(json);
|
|
|
|
|
|
|
|
// Create a model for the generated schema
|
|
|
|
return this.define(name, schema, options);
|
2013-08-13 16:37:09 +00:00
|
|
|
};
|
2013-08-07 21:51:32 +00:00
|
|
|
|
2013-05-31 17:25:11 +00:00
|
|
|
|
2013-05-24 05:20:20 +00:00
|
|
|
|