From fad6ee5e1da8090cbec50a9810c946c2b709bbc1 Mon Sep 17 00:00:00 2001 From: Raymond Feng Date: Tue, 1 Oct 2013 22:14:21 -0700 Subject: [PATCH] Refactor/cleanup the data source juggler implementation - Add a ModelDefinition class to encapsulate the model schema handling - Add a Connector class as the base class for connector implementations - Optimize attachTo and mixin - Rename some properties/methods --- examples/datasource-app.js | 5 + lib/connector.js | 129 ++++++++++++++++++++++ lib/connectors/memory.js | 54 +++------- lib/dao.js | 26 ++--- lib/datasource.js | 48 +++++++-- lib/jutil.js | 35 +++--- lib/model-builder.js | 124 ++++++++++----------- lib/model-definition.js | 198 +++++++++++++++++++++++++++++----- lib/model.js | 92 +++++++--------- lib/scope.js | 13 --- lib/sql.js | 69 ++---------- test/common_test.js | 8 +- test/manipulation.test.js | 2 +- test/model-definition.test.js | 83 ++++++++++++++ 14 files changed, 589 insertions(+), 297 deletions(-) create mode 100644 lib/connector.js create mode 100644 test/model-definition.test.js diff --git a/examples/datasource-app.js b/examples/datasource-app.js index b61b9a53..d22b0691 100644 --- a/examples/datasource-app.js +++ b/examples/datasource-app.js @@ -78,6 +78,11 @@ User.create({name: 'Ray'}, function (err, data) { console.log(data); }); +User.scope('minors', {age: {le: 16}}); +User.minors(function(err, kids) { + console.log('Kids: ', kids); +}); + var Article = ds.define('Article', {title: String}); var Tag = ds.define('Tag', {name: String}); Article.hasAndBelongsToMany('tags'); diff --git a/lib/connector.js b/lib/connector.js new file mode 100644 index 00000000..561bee63 --- /dev/null +++ b/lib/connector.js @@ -0,0 +1,129 @@ +module.exports = Connector; + +/** + * Base class for LooopBack connector. This is more a collection of useful + * methods for connectors than a super class + * @constructor + */ +function Connector() { + this._models = {}; +} + +/** + * Set the relational property to indicate the backend is a relational DB + * @type {boolean} + */ +Connector.prototype.relational = false; + +/** + * Execute a command with given parameters + * @param {String} command The command such as SQL + * @param {Object[]} [params] An array of parameters + * @param {Function} [callback] The callback function + */ +Connector.prototype.execute = function (command, params, callback) { + throw new Error('query method should be declared in connector'); +}; + + +/** + * Look up the data source by model name + * @param {String} model The model name + * @returns {DataSource} The data source + */ +Connector.prototype.getDataSource = function(model) { + var m = this._models[model]; + if(!m) { + console.trace('Model not found: ' + model); + } + return m && m.model.dataSource; +}; + +/** + * Get the id property name + * @param {String} model The model name + * @returns {String} The id property name + */ +Connector.prototype.idName = function (model) { + return this.getDataSource(model).idName(model); +}; + +/** + * Get the id property names + * @param {String} model The model name + * @returns {[String]} The id property names + */ +Connector.prototype.idNames = function (model) { + return this.getDataSource(model).idNames(model); +}; + + +/** + * Get the id index (sequence number, starting from 1) + * @param {String} model The model name + * @param {String} prop The property name + * @returns {Number} The id index, undefined if the property is not part of the primary key + */ +Connector.prototype.id = function (model, prop) { + var p = this._models[model].properties[prop]; + if(!p) { + console.trace('Property not found: ' + model +'.' + prop); + } + return p.id; +}; + +/** + * Hook to be called by DataSource for defining a model + * @param {Object} modelDefinition The model definition + */ +Connector.prototype.define = function (modelDefinition) { + if (!modelDefinition.settings) { + modelDefinition.settings = {}; + } + this._models[modelDefinition.model.modelName] = modelDefinition; +}; + +/** + * Hook to be called by DataSource for defining a model property + * @param {String} model The model name + * @param {String} prop The property name + * @param {Object} params The object for property metadata + */ +Connector.prototype.defineProperty = function (model, prop, params) { + this._models[model].properties[prop] = params; +}; + +/** + * Disconnect from the connector + */ +Connector.prototype.disconnect = function disconnect(cb) { + // NO-OP +}; + +/** + * Get the id value for the given model + * @param {String} model The model name + * @param {Object} data The model instance data + * @returns {*} The id value + * + */ +Connector.prototype.getIdValue = function(model, data) { + return data && data[this.idName(model)]; +}; + +/** + * Set the id value for the given model + * @param {String} model The model name + * @param {Object} data The model instance data + * @param {*} value The id value + * + */ +Connector.prototype.setIdValue = function(model, data, value) { + if(data) { + data[this.idName(model)] = value; + } +}; + + + + diff --git a/lib/connectors/memory.js b/lib/connectors/memory.js index 2749c040..eefd8721 100644 --- a/lib/connectors/memory.js +++ b/lib/connectors/memory.js @@ -1,7 +1,15 @@ +var util = require('util'); +var Connector = require('../connector'); var geo = require('../geo'); var utils = require('../utils'); -exports.initialize = function initializeSchema(dataSource, callback) { +/** + * Initialize the Oracle connector against the given data source + * + * @param {DataSource} dataSource The loopback-datasource-juggler dataSource + * @param {Function} [callback] The callback function + */ +exports.initialize = function initializeDataSource(dataSource, callback) { dataSource.connector = new Memory(); dataSource.connector.connect(callback); }; @@ -13,15 +21,18 @@ function Memory(m) { this.isTransaction = true; this.cache = m.cache; this.ids = m.ids; + Connector.apply(this, [].slice.call(arguments)); this._models = m._models; } else { this.isTransaction = false; this.cache = {}; this.ids = {}; - this._models = {}; + Connector.apply(this, [].slice.call(arguments)); } } +util.inherits(Memory, Connector); + Memory.prototype.connect = function(callback) { if (this.isTransaction) { this.onTransactionExec = callback; @@ -32,48 +43,11 @@ Memory.prototype.connect = function(callback) { Memory.prototype.define = function defineModel(descr) { var m = descr.model.modelName; - this._models[m] = descr; + Connector.prototype.define.apply(this, [].slice.call(arguments)); this.cache[m] = {}; this.ids[m] = 1; }; -/** - * Get the id property name for the given model - * @param {String} model The model name - * @returns {String} The id property name - * - * @private - */ -Memory.prototype.idName = function(model) { - return (this.dataSource && this.dataSource.idName && this.dataSource.idName(model)) || 'id'; -}; - -/** - * Get the id value for the given model - * @param {String} model The model name - * @param {Object} data The model instance data - * @returns {*} The id value - * - * @private - */ -Memory.prototype.getIdValue = function(model, data) { - return data && data[this.idName(model)]; -}; - -/** - * Set the id value for the given model - * @param {String} model The model name - * @param {Object} data The model instance data - * @param {*} value The id value - * - * @private - */ -Memory.prototype.setIdValue = function(model, data, value) { - if(data) { - data[this.idName(model)] = value; - } -}; - Memory.prototype.create = function create(model, data, callback) { // FIXME: [rfeng] We need to generate unique ids based on the id type // FIXME: [rfeng] We don't support composite ids yet diff --git a/lib/dao.js b/lib/dao.js index b172298c..4dec58e8 100644 --- a/lib/dao.js +++ b/lib/dao.js @@ -59,7 +59,7 @@ DataAccessObject._forDB = function (data) { } var res = {}; Object.keys(data).forEach(function (propName) { - var type = this.whatTypeName(propName); + var type = this.getPropertyType(propName); if (type === 'JSON' || type === 'Any' || type === 'Object' || data[propName] instanceof Array) { res[propName] = JSON.stringify(data[propName]); } else { @@ -202,17 +202,7 @@ setRemoting(DataAccessObject.create, { }); function stillConnecting(dataSource, obj, args) { - if (dataSource.connected) return false; // Connected - - var method = args.callee; - // Set up a callback after the connection is established to continue the method call - dataSource.once('connected', function () { - method.apply(obj, [].slice.call(args)); - }); - if (!dataSource.connecting) { - dataSource.connect(); - } - return true; + return dataSource.ready(obj, args); } /** @@ -729,7 +719,7 @@ DataAccessObject.prototype.updateAttributes = function updateAttributes(data, cb inst._adapter().updateAttributes(model, getIdValue(inst.constructor, inst), inst.constructor._forDB(data), function (err) { if (!err) { - // update _was attrs + // update $was attrs Object.keys(data).forEach(function (key) { inst.__dataWas[key] = inst.__data[key]; }); @@ -790,6 +780,16 @@ function defineReadonlyProp(obj, key, value) { }); } +var defineScope = require('./scope.js').defineScope; + +/** + * Define scope + */ +DataAccessObject.scope = function (name, filter) { + defineScope(this, this, name, filter); +}; + + // jutil.mixin(DataAccessObject, validations.Validatable); jutil.mixin(DataAccessObject, Inclusion); jutil.mixin(DataAccessObject, Relation); diff --git a/lib/datasource.js b/lib/datasource.js index 027eb866..7196d4f0 100644 --- a/lib/datasource.js +++ b/lib/datasource.js @@ -357,7 +357,6 @@ DataSource.prototype.createModel = DataSource.prototype.define = function define DataSource.prototype.mixin = function (ModelCtor) { var ops = this.operations(); - var self = this; var DAO = this.DataAccessObject; // mixin DAO @@ -366,7 +365,6 @@ DataSource.prototype.mixin = function (ModelCtor) { // decorate operations as alias functions Object.keys(ops).forEach(function (name) { var op = ops[name]; - var fn = op.scope[op.fnName]; var scope; if(op.enabled) { @@ -381,7 +379,7 @@ DataSource.prototype.mixin = function (ModelCtor) { 'scope', 'fnName', 'prototype' - ].indexOf(key) + ].indexOf(key); }) .forEach(function (key) { if(typeof op[key] !== 'undefined') { @@ -390,7 +388,7 @@ DataSource.prototype.mixin = function (ModelCtor) { }); } }); -} +}; /** * Attach an existing model to a data source. @@ -399,6 +397,10 @@ DataSource.prototype.mixin = function (ModelCtor) { */ DataSource.prototype.attach = function (ModelCtor) { + if(ModelCtor.dataSource === this) { + // 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; @@ -1358,6 +1360,7 @@ DataSource.prototype.copyModel = function copyModel(Master) { util.inherits(Slave, Master); + // Delegating static properties Slave.__proto__ = Master; hiddenProperty(Slave, 'dataSource', dataSource); @@ -1393,7 +1396,12 @@ DataSource.prototype.copyModel = function copyModel(Master) { */ DataSource.prototype.transaction = function() { var dataSource = this; - var transaction = new EventEmitter; + var transaction = new EventEmitter(); + + for (var p in dataSource) { + transaction[p] = dataSource[p]; + } + transaction.isTransaction = true; transaction.origin = dataSource; transaction.name = dataSource.name; @@ -1410,8 +1418,6 @@ DataSource.prototype.transaction = function() { dataSource.copyModel.call(transaction, dataSource.models[i]); } - transaction.connect = dataSource.connect; - transaction.exec = function(cb) { transaction.connector.exec(cb); }; @@ -1482,7 +1488,7 @@ DataSource.prototype.defineOperation = function (name, options, fn) { options.fn = fn; options.name = name; this._operations[name] = options; -} +}; /** * Check if the backend is a relational DB @@ -1490,7 +1496,31 @@ DataSource.prototype.defineOperation = function (name, options, fn) { */ DataSource.prototype.isRelational = function() { return this.connector && this.connector.relational; -} +}; + +/** + * Check if the data source is ready + * @param obj + * @param args + * @returns {boolean} + */ +DataSource.prototype.ready = function(obj, args) { + if (this.connected) { + // Connected + return false; + } + + var method = args.callee; + // Set up a callback after the connection is established to continue the method call + this.once('connected', function () { + method.apply(obj, [].slice.call(args)); + }); + if (!this.connecting) { + this.connect(); + } + return true; +}; + /** * Define a hidden property diff --git a/lib/jutil.js b/lib/jutil.js index e9cf7c26..d5b9e59b 100644 --- a/lib/jutil.js +++ b/lib/jutil.js @@ -24,12 +24,21 @@ exports.inherits = function (newClass, baseClass, options) { /** - * Mix in the base class into the new class - * @param newClass - * @param baseClass + * Mix in the a class into the new class + * @param newClass The target class to receive the mixin + * @param mixinClass The class to be mixed in * @param options */ -exports.mixin = function (newClass, baseClass, options) { +exports.mixin = function (newClass, mixinClass, options) { + if (Array.isArray(newClass._mixins)) { + if(newClass._mixins.indexOf(mixinClass) !== -1) { + return; + } + newClass._mixins.push(mixinClass); + } else { + newClass._mixins = [mixinClass]; + } + options = options || { staticProperties: true, instanceProperties: true, @@ -37,31 +46,25 @@ exports.mixin = function (newClass, baseClass, options) { }; if (options.staticProperties) { - Object.keys(baseClass).forEach(function (classProp) { - if (classProp !== 'super_' && (!newClass.hasOwnProperty(classProp) || options.override)) { - var pd = Object.getOwnPropertyDescriptor(baseClass, classProp); + Object.keys(mixinClass).forEach(function (classProp) { + if (classProp !== 'super_' && classProp !== '_mixins' && (!newClass.hasOwnProperty(classProp) || options.override)) { + var pd = Object.getOwnPropertyDescriptor(mixinClass, classProp); Object.defineProperty(newClass, classProp, pd); } }); } if (options.instanceProperties) { - if (baseClass.prototype) { - Object.keys(baseClass.prototype).forEach(function (instanceProp) { + if (mixinClass.prototype) { + Object.keys(mixinClass.prototype).forEach(function (instanceProp) { if (!newClass.hasOwnProperty(instanceProp) || options.override) { - var pd = Object.getOwnPropertyDescriptor(baseClass.prototype, instanceProp); + var pd = Object.getOwnPropertyDescriptor(mixinClass.prototype, instanceProp); Object.defineProperty(newClass.prototype, instanceProp, pd); } }); } } - if (Array.isArray(newClass._mixins)) { - newClass._mixins.push(baseClass); - } else { - newClass._mixins = [baseClass]; - } - return newClass; }; diff --git a/lib/model-builder.js b/lib/model-builder.js index a449bebf..c51d3d92 100644 --- a/lib/model-builder.js +++ b/lib/model-builder.js @@ -2,11 +2,12 @@ * Module dependencies */ -var i8n = require('inflection'); -var DefaultModelBaseClass = require('./model.js'); -var List = require('./list.js'); +var inflection = require('inflection'); var EventEmitter = require('events').EventEmitter; var util = require('util'); +var DefaultModelBaseClass = require('./model.js'); +var List = require('./list.js'); +var ModelDefinition = require('./model-definition.js'); // Set up types require('./types')(ModelBuilder); @@ -40,8 +41,12 @@ function ModelBuilder() { this.definitions = {}; } +// Inherit from EventEmitter util.inherits(ModelBuilder, EventEmitter); +// Create a default instance +ModelBuilder.defaultInstance = new ModelBuilder(); + /** * Define a model class * @@ -122,30 +127,35 @@ ModelBuilder.prototype.define = function defineClass(className, properties, sett hiddenProperty(ModelClass, 'dataSource', dataSource); hiddenProperty(ModelClass, 'schema', dataSource); // For backward compatibility hiddenProperty(ModelClass, 'modelName', className); - hiddenProperty(ModelClass, 'pluralModelName', pluralName || i8n.pluralize(className)); + hiddenProperty(ModelClass, 'pluralModelName', pluralName || inflection.pluralize(className)); hiddenProperty(ModelClass, 'relations', {}); util.inherits(ModelClass, ModelBaseClass); // inherit ModelBaseClass static methods for (var i in ModelBaseClass) { + if(i !== '_mixins') { ModelClass[i] = ModelBaseClass[i]; + } } ModelClass.getter = {}; ModelClass.setter = {}; - standartize(properties, settings); + normalize(properties, settings); + var modelDefinition = new ModelDefinition(this, className, properties, settings); // store class in model pool this.models[className] = ModelClass; this.definitions[className] = { properties: properties, - settings: settings + settings: settings, + schema: modelDefinition }; // expose properties on the ModelClass ModelClass.properties = properties; ModelClass.settings = settings; + ModelClass.definition = modelDefinition; var idInjection = settings.idInjection; if(idInjection !== false) { @@ -182,25 +192,25 @@ ModelBuilder.prototype.define = function defineClass(className, properties, sett dataSource.attach(this); }; - ModelClass.extend = function (className, p, s) { - p = p || {}; - s = s || {}; + ModelClass.extend = function (className, subclassProperties, subclassSettings) { + subclassProperties = subclassProperties || {}; + subclassSettings = subclassSettings || {}; Object.keys(properties).forEach(function (key) { // dont inherit the id property - if(key !== 'id' && typeof p[key] === 'undefined') { - p[key] = properties[key]; + if(key !== 'id' && typeof subclassProperties[key] === 'undefined') { + subclassProperties[key] = properties[key]; } }); Object.keys(settings).forEach(function (key) { - if(typeof s[key] === 'undefined') { - s[key] = settings[key]; + if(typeof subclassSettings[key] === 'undefined') { + subclassSettings[key] = settings[key]; } }); - var c = dataSource.define(className, p, s, ModelClass); + var c = dataSource.define(className, subclassProperties, subclassSettings, ModelClass); if(typeof c.setup === 'function') { c.setup.call(c); @@ -209,11 +219,15 @@ ModelBuilder.prototype.define = function defineClass(className, properties, sett return c; }; - ModelClass.registerProperty = function (attr) { - var prop = properties[attr]; + /** + * Register a property for the model class + * @param propertyName + */ + ModelClass.registerProperty = function (propertyName) { + var prop = properties[propertyName]; var DataType = prop.type; if(!DataType) { - throw new Error('Invalid type for property ' + attr); + throw new Error('Invalid type for property ' + propertyName); } if (Array.isArray(DataType) || DataType === Array) { DataType = List; @@ -228,33 +242,33 @@ ModelBuilder.prototype.define = function defineClass(className, properties, sett if(prop.required) { var requiredOptions = typeof prop.required === 'object' ? prop.required : undefined; - ModelClass.validatesPresenceOf(attr, requiredOptions); + ModelClass.validatesPresenceOf(propertyName, requiredOptions); } - Object.defineProperty(ModelClass.prototype, attr, { + Object.defineProperty(ModelClass.prototype, propertyName, { get: function () { - if (ModelClass.getter[attr]) { - return ModelClass.getter[attr].call(this); + if (ModelClass.getter[propertyName]) { + return ModelClass.getter[propertyName].call(this); // Try getter first } else { - return this.__data && this.__data[attr]; + return this.__data && this.__data[propertyName]; // Try __data } }, set: function (value) { - if (ModelClass.setter[attr]) { - ModelClass.setter[attr].call(this, value); + if (ModelClass.setter[propertyName]) { + ModelClass.setter[propertyName].call(this, value); // Try setter first } else { if (!this.__data) { this.__data = {}; } if (value === null || value === undefined) { - this.__data[attr] = value; + this.__data[propertyName] = value; } else { if(DataType === List) { - this.__data[attr] = DataType(value, properties[attr].type, this.__data); + this.__data[propertyName] = DataType(value, properties[propertyName].type, this.__data); } else { // Assume the type constructor handles Constructor() call // If not, we should call new DataType(value).valueOf(); - this.__data[attr] = DataType(value); + this.__data[propertyName] = DataType(value); } } } @@ -263,21 +277,26 @@ ModelBuilder.prototype.define = function defineClass(className, properties, sett enumerable: true }); - ModelClass.prototype.__defineGetter__(attr + '_was', function () { - return this.__dataWas && this.__dataWas[attr]; + // $was --> __dataWas. + Object.defineProperty(ModelClass.prototype, propertyName + '$was', { + get: function () { + return this.__dataWas && this.__dataWas[propertyName]; + }, + configurable: true, + enumerable: false }); // FIXME: [rfeng] Do we need to keep the raw data? // Use $ as the prefix to avoid conflicts with properties such as _id - Object.defineProperty(ModelClass.prototype, '$' + attr, { + Object.defineProperty(ModelClass.prototype, '$' + propertyName, { get: function () { - return this.__data && this.__data[attr]; + return this.__data && this.__data[propertyName]; }, set: function (value) { if (!this.__data) { this.__data = {}; } - this.__data[attr] = value; + this.__data[propertyName] = value; }, configurable: true, enumerable: false @@ -290,29 +309,30 @@ ModelBuilder.prototype.define = function defineClass(className, properties, sett }; -function standartize(properties, settings) { +/*! + * Normalize the property definitions + * @param properties + * @param settings + */ +function normalize(properties, settings) { Object.keys(properties).forEach(function (key) { var v = properties[key]; if (typeof v === 'function' || Array.isArray(v)) { properties[key] = { type: v }; } }); - // TODO: add timestamps fields - // when present in settings: {timestamps: true} - // or {timestamps: {created: 'created_at', updated: false}} - // by default property names: createdAt, updatedAt } /** - * Define single property named `prop` on `model` + * Define single property named `propertyName` on `model` * * @param {String} model - name of model - * @param {String} prop - name of property - * @param {Object} params - property settings + * @param {String} propertyName - name of property + * @param {Object} propertyDefinition - property settings */ -ModelBuilder.prototype.defineProperty = function (model, prop, params) { - this.definitions[model].properties[prop] = params; - this.models[model].registerProperty(prop); +ModelBuilder.prototype.defineProperty = function (model, propertyName, propertyDefinition) { + this.definitions[model].properties[propertyName] = propertyDefinition; + this.models[model].registerProperty(propertyName); }; /** @@ -340,7 +360,7 @@ ModelBuilder.prototype.defineProperty = function (model, prop, params) { */ ModelBuilder.prototype.extendModel = function (model, props) { var t = this; - standartize(props, {}); + normalize(props, {}); Object.keys(props).forEach(function (propName) { var definition = props[propName]; t.defineProperty(model, propName, definition); @@ -391,22 +411,6 @@ function hiddenProperty(where, property, value) { }); } -/*! - * Define readonly property on object - * - * @param {Object} obj - * @param {String} key - * @param {Mixed} value - */ -function defineReadonlyProp(obj, key, value) { - Object.defineProperty(obj, key, { - writable: false, - enumerable: true, - configurable: true, - value: value - }); -} - /** * 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 diff --git a/lib/model-definition.js b/lib/model-definition.js index 7a336de8..a981407a 100644 --- a/lib/model-definition.js +++ b/lib/model-definition.js @@ -1,6 +1,9 @@ var assert = require('assert'); var util = require('util'); var EventEmitter = require('events').EventEmitter; +var traverse = require('traverse'); +var ModelBaseClass = require('./model'); +var ModelBuilder = require('./model-builder'); /** * Model definition @@ -9,20 +12,22 @@ module.exports = ModelDefinition; /** * Constructor for ModelDefinition - * @param name - * @param properties - * @param settings + * @param {ModelBuilder} modelBuilder A model builder instance + * @param {String} name The model name + * @param {Object} properties The model properties, optional + * @param {Object} settings The model settings, optional * @returns {ModelDefinition} * @constructor * */ -function ModelDefinition(name, properties, settings) { +function ModelDefinition(modelBuilder, name, properties, settings) { if (!(this instanceof ModelDefinition)) { - return new ModelDefinition(name, properties, settings); + return new ModelDefinition(modelBuilder, name, properties, settings); } + this.modelBuilder = modelBuilder || ModelBuilder.defaultInstance; assert(name, 'name is missing'); - if (arguments.length === 1 && typeof name === 'object') { + if (arguments.length === 2 && typeof name === 'object') { this.name = name.name; this.properties = name.properties || {}; this.settings = name.settings || {}; @@ -32,28 +37,14 @@ function ModelDefinition(name, properties, settings) { this.properties = properties || {}; this.settings = settings || {}; } + this.associations = []; } util.inherits(ModelDefinition, EventEmitter); -/** - * Convert to a plain JSON Object - * @returns {Object} - */ -ModelDefinition.prototype.toJSON = function () { - return this; -}; +// Set up types +require('./types')(ModelDefinition); -/** - * - * @returns {*} - */ -ModelDefinition.prototype.build = function () { - if(this.model) { - return this.model; - } - return this; -}; /** * Return table name for specified `modelName` @@ -127,8 +118,8 @@ ModelDefinition.prototype.columnNames = function (connectorType) { * @returns {Object[]} property name/index for IDs */ ModelDefinition.prototype.ids = function () { - if(this.ids) { - return this.ids; + if(this._ids) { + return this._ids; } var ids = []; var props = this.properties; @@ -145,6 +136,7 @@ ModelDefinition.prototype.ids = function () { ids.sort(function (a, b) { return a.key - b.key; }); + this._ids = ids; return ids; }; @@ -162,7 +154,7 @@ ModelDefinition.prototype.idColumnName = function(connectorType) { * @returns {String} property name for ID */ ModelDefinition.prototype.idName = function() { - var id = this.ids[0]; + var id = this.ids()[0]; return id && id.name; }; @@ -171,9 +163,161 @@ ModelDefinition.prototype.idName = function() { * @returns {String[]} property names for IDs */ ModelDefinition.prototype.idNames = function () { - var ids = this.ids; + var ids = this.ids(); var names = ids.map(function (id) { return id.name; }); return names; }; + +/** + * + * @returns {{}} + */ +ModelDefinition.prototype.indexes = function () { + var indexes = {}; + if (this.settings.indexes) { + for (var i in this.settings.indexes) { + indexes[i] = this.settings.indexes[i]; + } + } + for (var p in this.properties) { + if (this.properties[p].index) { + indexes[p + '_index'] = this.properties[p].index; + } + } + 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()]; + 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 { + if(!this.anonymousTypesCount) { + this.anonymousTypesCount = 0; + } + this.anonymousTypesCount++; + return this.modelBuilder.define('AnonymousType' + this.anonymousTypesCount, + type, {anonymous: true, idInjection: false}); + /* + console.error(type); + throw new Error('Missing type property'); + */ + } + } else if('function' === typeof type ) { + return type; + } + return type; +}; + +/** + * Build a model definition + */ +ModelDefinition.prototype.build = function () { + if (this.resolvedProperties) { + return this.resolvedProperties; + } + this.resolvedProperties = {}; + for (var p in this.properties) { + var type = this.resolveType(this.properties[p]); + if (typeof type === 'string') { + if (Array.isArray(this.associations)) { + this.associations.push({ + source: this.name, + target: type, + relation: Array.isArray(this.properties[p]) ? 'hasMany' : 'belongsTo', + as: p + }); + // delete this.properties[p]; + } + } else { + var typeDef = { + type: type + }; + for (var a in this.properties[p]) { + // Skip the type property but don't delete it Model.extend() shares same instances of the properties from the base class + if (a !== 'type') { + typeDef[a] = this.properties[p][a]; + } + } + this.resolvedProperties[p] = typeDef; + } + } + return this.resolvedProperties; +}; + + +function isModelClass(cls) { + while (true) { + if (!cls) { + return false; + } + if (ModelBaseClass === cls) { + return true; + } + cls = cls.prototype; + } +} + +ModelDefinition.prototype.toJSON = function() { + + var json = { + name: this.name, + properties: {}, + settings: this.settings + }; + this.build(); + + var mapper = function(val) { + if(val === undefined || val === null) { + return val; + } + if('function' === typeof val.toJSON) { + // The value has its own toJSON() object + return val.toJSON(); + } + if('function' === typeof val) { + if(isModelClass(val)) { + if(val.settings && val.settings.anonymous) { + return val.definition && val.definition.toJSON(); + } else { + return val.modelName; + } + } + return val.name; + } else { + return val; + } + }; + for(var p in this.resolvedProperties) { + json.properties[p] = traverse(this.resolvedProperties[p]).map(mapper); + } + return json; +}; diff --git a/lib/model.js b/lib/model.js index 9c7f48f3..dbac8cd2 100644 --- a/lib/model.js +++ b/lib/model.js @@ -99,56 +99,56 @@ ModelBaseClass.prototype._initProperties = function (data, applySetters) { } if (applySetters === true) { - Object.keys(data).forEach(function (attr) { - if((attr in properties) || (attr in ctor.relations)) { - self[attr] = self.__data[attr] || data[attr]; + Object.keys(data).forEach(function (propertyName) { + if((propertyName in properties) || (propertyName in ctor.relations)) { + self[propertyName] = self.__data[propertyName] || data[propertyName]; } }); } // Set the unknown properties as properties to the object if(strict === false) { - Object.keys(data).forEach(function (attr) { - if(!(attr in properties)) { - self[attr] = self.__data[attr] || data[attr]; + Object.keys(data).forEach(function (propertyName) { + if(!(propertyName in properties)) { + self[propertyName] = self.__data[propertyName] || data[propertyName]; } }); } - ctor.forEachProperty(function (attr) { + ctor.forEachProperty(function (propertyName) { - if ('undefined' === typeof self.__data[attr]) { - self.__data[attr] = self.__dataWas[attr] = getDefault(attr); + if ('undefined' === typeof self.__data[propertyName]) { + self.__data[propertyName] = self.__dataWas[propertyName] = getDefault(propertyName); } else { - self.__dataWas[attr] = self.__data[attr]; + self.__dataWas[propertyName] = self.__data[propertyName]; } }); - ctor.forEachProperty(function (attr) { + ctor.forEachProperty(function (propertyName) { - var type = properties[attr].type; + var type = properties[propertyName].type; if (BASE_TYPES.indexOf(type.name) === -1) { - if (typeof self.__data[attr] !== 'object' && self.__data[attr]) { + if (typeof self.__data[propertyName] !== 'object' && self.__data[propertyName]) { try { - self.__data[attr] = JSON.parse(self.__data[attr] + ''); + self.__data[propertyName] = JSON.parse(self.__data[propertyName] + ''); } catch (e) { - self.__data[attr] = String(self.__data[attr]); + self.__data[propertyName] = String(self.__data[propertyName]); } } if (type.name === 'Array' || Array.isArray(type)) { - if(!(self.__data[attr] instanceof List)) { - self.__data[attr] = new List(self.__data[attr], type, self); + if(!(self.__data[propertyName] instanceof List)) { + self.__data[propertyName] = new List(self.__data[propertyName], type, self); } } } }); - function getDefault(attr) { - var def = properties[attr]['default']; - if (isdef(def)) { + function getDefault(propertyName) { + var def = properties[propertyName]['default']; + if (def !== undefined) { if (typeof def === 'function') { return def(); } else { @@ -170,7 +170,7 @@ ModelBaseClass.defineProperty = function (prop, params) { this.dataSource.defineProperty(this.modelName, prop, params); }; -ModelBaseClass.whatTypeName = function (propName) { +ModelBaseClass.getPropertyType = function (propName) { var prop = this.properties[propName]; if(!prop) { // The property is not part of the definition @@ -183,8 +183,8 @@ ModelBaseClass.whatTypeName = function (propName) { return prop.type.name; }; -ModelBaseClass.prototype.whatTypeName = function (propName) { - return this.constructor.whatTypeName(propName); +ModelBaseClass.prototype.getPropertyType = function (propName) { + return this.constructor.getPropertyType(propName); }; /** @@ -209,28 +209,28 @@ ModelBaseClass.prototype.toObject = function (onlySchema) { var self = this; var schemaLess = this.constructor.settings.strict === false || !onlySchema; - this.constructor.forEachProperty(function (attr) { - if (self[attr] instanceof List) { - data[attr] = self[attr].toObject(!schemaLess); - } else if (self.__data.hasOwnProperty(attr)) { - if(self[attr] !== undefined && self[attr]!== null && self[attr].toObject) { - data[attr] = self[attr].toObject(!schemaLess); + this.constructor.forEachProperty(function (propertyName) { + if (self[propertyName] instanceof List) { + data[propertyName] = self[propertyName].toObject(!schemaLess); + } else if (self.__data.hasOwnProperty(propertyName)) { + if(self[propertyName] !== undefined && self[propertyName]!== null && self[propertyName].toObject) { + data[propertyName] = self[propertyName].toObject(!schemaLess); } else { - data[attr] = self[attr]; + data[propertyName] = self[propertyName]; } } else { - data[attr] = null; + data[propertyName] = null; } }); if (schemaLess) { - Object.keys(self.__data).forEach(function (attr) { - if (!data.hasOwnProperty(attr)) { - var val = self.__data[attr]; + Object.keys(self.__data).forEach(function (propertyName) { + if (!data.hasOwnProperty(propertyName)) { + var val = self.__data[propertyName]; if(val !== undefined && val!== null && val.toObject) { - data[attr] = val.toObject(!schemaLess); + data[propertyName] = val.toObject(!schemaLess); } else { - data[attr] = val; + data[propertyName] = val; } } }); @@ -256,11 +256,11 @@ ModelBaseClass.prototype.fromObject = function (obj) { /** * Checks is property changed based on current property and initial value * - * @param {String} attr - property name + * @param {String} propertyName - property name * @return Boolean */ -ModelBaseClass.prototype.propertyChanged = function propertyChanged(attr) { - return this.__data[attr] !== this.__dataWas[attr]; +ModelBaseClass.prototype.propertyChanged = function propertyChanged(propertyName) { + return this.__data[propertyName] !== this.__dataWas[propertyName]; }; /** @@ -276,7 +276,7 @@ ModelBaseClass.prototype.reset = function () { delete obj[k]; } if (obj.propertyChanged(k)) { - obj[k] = obj[k + '_was']; + obj[k] = obj[k + '$was']; } }); }; @@ -285,19 +285,9 @@ ModelBaseClass.prototype.inspect = function () { return util.inspect(this.__data, false, 4, true); }; -/** - * Check whether `s` is not undefined - * @param {Mixed} s - * @return {Boolean} s is undefined - */ -function isdef(s) { - var undef; - return s !== undef; -} - ModelBaseClass.mixin = function(anotherClass, options) { return jutil.mixin(this, anotherClass, options); -} +}; jutil.mixin(ModelBaseClass, Hookable); jutil.mixin(ModelBaseClass, validations.Validatable); \ No newline at end of file diff --git a/lib/scope.js b/lib/scope.js index ddefdedd..50d4b481 100644 --- a/lib/scope.js +++ b/lib/scope.js @@ -3,19 +3,6 @@ */ exports.defineScope = defineScope; -/** - * Scope mixin for ./model.js - */ -var DataAccessObject = require('./dao.js'); - -/** - * Define scope - * TODO: describe behavior and usage examples - */ -DataAccessObject.scope = function (name, params) { - defineScope(this, this, name, params); -}; - function defineScope(cls, targetClass, name, params, methods) { // collect meta info about scope diff --git a/lib/sql.js b/lib/sql.js index a4d32dd1..281212c5 100644 --- a/lib/sql.js +++ b/lib/sql.js @@ -1,3 +1,6 @@ +var util = require('util'); +var Connector = require('./connector'); + module.exports = BaseSQL; /** @@ -5,8 +8,11 @@ module.exports = BaseSQL; * @constructor */ function BaseSQL() { + Connector.apply(this, [].slice.call(arguments)); } +util.inherits(BaseSQL, Connector); + /** * Set the relational property to indicate the backend is a relational DB * @type {boolean} @@ -28,18 +34,6 @@ BaseSQL.prototype.queryOne = function (sql, callback) { }); }; -/** - * Look up the data source by model name - * @param {String} model The model name - * @returns {DataSource} The data source - */ -BaseSQL.prototype.getDataSource = function(model) { - var m = this._models[model]; - if(!m) { - console.trace('Model not found: ' + model); - } - return m.model.dataSource; -} /** * Get the table name for a given model @@ -96,24 +90,6 @@ BaseSQL.prototype.propertyName = function (model, column) { return null; }; -/** - * Get the id property name - * @param {String} model The model name - * @returns {String} The id property name - */ -BaseSQL.prototype.idName = function (model) { - return this.getDataSource(model).idName(model); -}; - -/** - * Get the id property names - * @param {String} model The model name - * @returns {[String]} The id property names - */ -BaseSQL.prototype.idNames = function (model) { - return this.getDataSource(model).idNames(model); -}; - /** * Get the id column name * @param {String} model The model name @@ -137,20 +113,6 @@ BaseSQL.prototype.idColumnEscaped = function (model) { return this.escapeName(this.getDataSource(model).idColumnName(model)); }; -/** - * Get the id index (sequence number, starting from 1) - * @param {String} model The model name - * @param {String} prop The property name - * @returns {Number} The id index, undefined if the property is not part of the primary key - */ -BaseSQL.prototype.id = function (model, prop) { - var p = this._models[model].properties[prop]; - if(!p) { - console.trace('Property not found: ' + model +'.' + prop); - } - return p.id; -}; - /** * Escape the name for the underlying database * @param {String} name The name @@ -178,25 +140,6 @@ BaseSQL.prototype.columnEscaped = function (model, property) { return this.escapeName(this.column(model, property)); }; -/** - * Hook to be called by DataSource for defining a model - * @param {Object} descr The description of a model - */ -BaseSQL.prototype.define = function (descr) { - if (!descr.settings) descr.settings = {}; - this._models[descr.model.modelName] = descr; -}; - -/** - * Hook to be called by DataSource for defining a model property - * @param {String} model The model name - * @param {String} prop The property name - * @param {Object} params The object for property metadata - */ -BaseSQL.prototype.defineProperty = function (model, prop, params) { - this._models[model].properties[prop] = params; -}; - /** * Save the model instance into the backend store * @param {String} model The model name diff --git a/test/common_test.js b/test/common_test.js index 6087743c..35d1ec72 100644 --- a/test/common_test.js +++ b/test/common_test.js @@ -579,11 +579,11 @@ function testOrm(dataSource) { }); it('should return type of property', function (test) { - test.equal(Post.whatTypeName('title'), 'String'); - test.equal(Post.whatTypeName('content'), 'Text'); + test.equal(Post.getPropertyType('title'), 'String'); + test.equal(Post.getPropertyType('content'), 'Text'); var p = new Post; - test.equal(p.whatTypeName('title'), 'String'); - test.equal(p.whatTypeName('content'), 'Text'); + test.equal(p.getPropertyType('title'), 'String'); + test.equal(p.getPropertyType('content'), 'Text'); test.done(); }); diff --git a/test/manipulation.test.js b/test/manipulation.test.js index 260b0d74..0e9563ae 100644 --- a/test/manipulation.test.js +++ b/test/manipulation.test.js @@ -241,7 +241,7 @@ describe('manipulation', function() { person.name.should.equal(hw); person.propertyChanged('name').should.be.false; person.name = 'Goodbye, Lenin'; - person.name_was.should.equal(hw); + person.name$was.should.equal(hw); person.propertyChanged('name').should.be.true; (person.createdAt >= now).should.be.true; person.isNewRecord().should.be.true; diff --git a/test/model-definition.test.js b/test/model-definition.test.js new file mode 100644 index 00000000..e907f1d9 --- /dev/null +++ b/test/model-definition.test.js @@ -0,0 +1,83 @@ +// This test written in mocha+should.js +var should = require('./init.js'); +var assert = require('assert'); + +var jdb = require('../'); +var ModelBuilder = jdb.ModelBuilder; +var DataSource = jdb.DataSource; + +var ModelDefinition = require('../lib/model-definition'); + +describe('ModelDefinition class', function () { + + it('should be able to define plain models', function (done) { + var modelBuilder = new ModelBuilder(); + + var User = new ModelDefinition(modelBuilder, 'User', { + name: String, + bio: ModelBuilder.Text, + approved: Boolean, + joinedAt: Date, + age: Number + }); + + // console.log(User.toJSON()); + + done(); + + + }); + + + it('should be able to define nesting models', function (done) { + var modelBuilder = new ModelBuilder(); + + var User = new ModelDefinition(modelBuilder, 'User', { + name: String, + bio: ModelBuilder.Text, + approved: Boolean, + joinedAt: Date, + age: Number, + address: { + street: String, + city: String, + zipCode: String, + state: String + } + }); + + // console.log(JSON.stringify(User.toJSON(), null, ' ')); + + done(); + + + }); + + + it('should be able to define referencing models', function (done) { + var modelBuilder = new ModelBuilder(); + + var Address = modelBuilder.define('Address', { + street: String, + city: String, + zipCode: String, + state: String + }); + var User = new ModelDefinition(modelBuilder, 'User', { + name: String, + bio: ModelBuilder.Text, + approved: Boolean, + joinedAt: Date, + age: Number, + address: Address + + }); + + // console.log(JSON.stringify(User.toJSON(), null, ' ')); + + done(); + + + }); +}); +