2013-04-11 23:23:34 +00:00
|
|
|
/**
|
|
|
|
* Module exports class Model
|
|
|
|
*/
|
2013-05-17 15:49:57 +00:00
|
|
|
module.exports = ModelBaseClass;
|
2013-04-11 23:23:34 +00:00
|
|
|
|
2011-10-10 13:22:51 +00:00
|
|
|
/**
|
2012-03-27 14:22:24 +00:00
|
|
|
* Module dependencies
|
2011-10-10 13:22:51 +00:00
|
|
|
*/
|
|
|
|
var util = require('util');
|
2013-03-24 21:27:36 +00:00
|
|
|
var List = require('./list.js');
|
2013-05-20 23:05:59 +00:00
|
|
|
require('./hooks.js');
|
2011-10-10 13:22:51 +00:00
|
|
|
|
2013-04-11 23:23:34 +00:00
|
|
|
var BASE_TYPES = ['String', 'Boolean', 'Number', 'Date', 'Text'];
|
2011-10-10 13:22:51 +00:00
|
|
|
|
2011-10-08 17:11:26 +00:00
|
|
|
/**
|
2013-04-11 23:23:34 +00:00
|
|
|
* Model class - base class for all persist objects
|
2012-03-27 14:22:24 +00:00
|
|
|
* provides **common API** to access any database adapter.
|
|
|
|
* This class describes only abstract behavior layer, refer to `lib/adapters/*.js`
|
|
|
|
* to learn more about specific adapter implementations
|
|
|
|
*
|
2013-05-17 15:49:57 +00:00
|
|
|
* `ModelBaseClass` mixes `Validatable` and `Hookable` classes methods
|
2012-03-27 14:22:24 +00:00
|
|
|
*
|
|
|
|
* @constructor
|
|
|
|
* @param {Object} data - initial object data
|
2011-10-08 17:11:26 +00:00
|
|
|
*/
|
2013-05-17 15:49:57 +00:00
|
|
|
function ModelBaseClass(data) {
|
2012-04-19 15:01:40 +00:00
|
|
|
this._initProperties(data, true);
|
|
|
|
}
|
|
|
|
|
2013-05-17 15:49:57 +00:00
|
|
|
ModelBaseClass.prototype._initProperties = function (data, applySetters) {
|
2011-10-08 17:11:26 +00:00
|
|
|
var self = this;
|
2012-04-19 15:01:40 +00:00
|
|
|
var ctor = this.constructor;
|
|
|
|
var ds = ctor.schema.definitions[ctor.modelName];
|
2011-10-08 17:11:26 +00:00
|
|
|
var properties = ds.properties;
|
|
|
|
data = data || {};
|
|
|
|
|
2012-10-29 23:37:19 +00:00
|
|
|
Object.defineProperty(this, '__cachedRelations', {
|
|
|
|
writable: true,
|
|
|
|
enumerable: false,
|
|
|
|
configurable: true,
|
|
|
|
value: {}
|
|
|
|
});
|
|
|
|
|
2012-10-13 13:59:25 +00:00
|
|
|
Object.defineProperty(this, '__data', {
|
|
|
|
writable: true,
|
|
|
|
enumerable: false,
|
|
|
|
configurable: true,
|
|
|
|
value: {}
|
|
|
|
});
|
|
|
|
|
|
|
|
Object.defineProperty(this, '__dataWas', {
|
|
|
|
writable: true,
|
|
|
|
enumerable: false,
|
|
|
|
configurable: true,
|
|
|
|
value: {}
|
|
|
|
});
|
|
|
|
|
2012-12-16 17:05:36 +00:00
|
|
|
if (data['__cachedRelations']) {
|
|
|
|
this.__cachedRelations = data['__cachedRelations'];
|
|
|
|
}
|
|
|
|
|
2013-04-18 09:05:11 +00:00
|
|
|
for (var i in data) {
|
|
|
|
if (i in properties) {
|
|
|
|
this.__data[i] = this.__dataWas[i] = data[i];
|
|
|
|
} else if (i in ctor.relations) {
|
|
|
|
this.__data[ctor.relations[i].keyFrom] = this.__dataWas[i] = data[i][ctor.relations[i].keyTo];
|
|
|
|
this.__cachedRelations[i] = data[i];
|
|
|
|
}
|
|
|
|
}
|
2012-10-13 13:59:25 +00:00
|
|
|
|
2013-04-07 13:59:24 +00:00
|
|
|
if (applySetters === true) {
|
|
|
|
Object.keys(data).forEach(function (attr) {
|
|
|
|
self[attr] = data[attr];
|
2011-10-08 17:11:26 +00:00
|
|
|
});
|
2012-10-13 13:59:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ctor.forEachProperty(function (attr) {
|
|
|
|
|
2013-03-24 21:25:10 +00:00
|
|
|
if ('undefined' === typeof self.__data[attr]) {
|
2012-10-13 13:59:25 +00:00
|
|
|
self.__data[attr] = self.__dataWas[attr] = getDefault(attr);
|
|
|
|
} else {
|
|
|
|
self.__dataWas[attr] = self.__data[attr];
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
ctor.forEachProperty(function (attr) {
|
2011-10-08 17:11:26 +00:00
|
|
|
|
2012-09-10 15:57:21 +00:00
|
|
|
var type = properties[attr].type;
|
|
|
|
|
|
|
|
if (BASE_TYPES.indexOf(type.name) === -1) {
|
2012-10-13 13:59:25 +00:00
|
|
|
if (typeof self.__data[attr] !== 'object' && self.__data[attr]) {
|
2012-09-11 16:51:31 +00:00
|
|
|
try {
|
2012-10-13 13:59:25 +00:00
|
|
|
self.__data[attr] = JSON.parse(self.__data[attr] + '');
|
2012-09-11 16:51:31 +00:00
|
|
|
} catch (e) {
|
2013-03-05 19:02:07 +00:00
|
|
|
self.__data[attr] = String(self.__data[attr]);
|
2012-09-10 15:57:21 +00:00
|
|
|
}
|
2012-09-11 16:51:31 +00:00
|
|
|
}
|
|
|
|
if (type.name === 'Array' || typeof type === 'object' && type.constructor.name === 'Array') {
|
2012-10-13 13:59:25 +00:00
|
|
|
self.__data[attr] = new List(self.__data[attr], type, self);
|
2012-09-10 15:57:21 +00:00
|
|
|
}
|
2012-05-29 11:16:24 +00:00
|
|
|
}
|
|
|
|
|
2012-10-13 13:59:25 +00:00
|
|
|
});
|
2011-10-11 19:51:32 +00:00
|
|
|
|
|
|
|
function getDefault(attr) {
|
2012-07-13 13:53:22 +00:00
|
|
|
var def = properties[attr]['default'];
|
2011-10-11 19:51:32 +00:00
|
|
|
if (isdef(def)) {
|
|
|
|
if (typeof def === 'function') {
|
|
|
|
return def();
|
|
|
|
} else {
|
|
|
|
return def;
|
|
|
|
}
|
|
|
|
} else {
|
2013-03-24 21:25:10 +00:00
|
|
|
return undefined;
|
2011-10-11 19:51:32 +00:00
|
|
|
}
|
|
|
|
}
|
2011-11-17 04:24:43 +00:00
|
|
|
|
2013-03-24 21:25:10 +00:00
|
|
|
this.trigger('initialize');
|
2012-07-13 13:53:22 +00:00
|
|
|
}
|
2011-10-08 17:11:26 +00:00
|
|
|
|
2012-03-27 14:22:24 +00:00
|
|
|
/**
|
|
|
|
* @param {String} prop - property name
|
|
|
|
* @param {Object} params - various property configuration
|
|
|
|
*/
|
2013-05-17 15:49:57 +00:00
|
|
|
ModelBaseClass.defineProperty = function (prop, params) {
|
2011-12-09 15:23:29 +00:00
|
|
|
this.schema.defineProperty(this.modelName, prop, params);
|
|
|
|
};
|
|
|
|
|
2013-05-17 15:49:57 +00:00
|
|
|
ModelBaseClass.whatTypeName = function (propName) {
|
2013-04-23 14:10:56 +00:00
|
|
|
var prop = this.schema.definitions[this.modelName].properties[propName];
|
|
|
|
if (!prop || !prop.type) {
|
|
|
|
throw new Error('Undefined type for ' + this.modelName + ':' + propName);
|
|
|
|
}
|
|
|
|
return prop.type.name;
|
2012-05-29 11:16:24 +00:00
|
|
|
};
|
|
|
|
|
2013-05-17 15:49:57 +00:00
|
|
|
ModelBaseClass.prototype.whatTypeName = function (propName) {
|
2012-01-18 15:20:05 +00:00
|
|
|
return this.constructor.whatTypeName(propName);
|
|
|
|
};
|
|
|
|
|
2012-03-27 14:22:24 +00:00
|
|
|
/**
|
|
|
|
* Return string representation of class
|
|
|
|
*
|
|
|
|
* @override default toString method
|
|
|
|
*/
|
2013-05-17 15:49:57 +00:00
|
|
|
ModelBaseClass.toString = function () {
|
2011-10-08 17:11:26 +00:00
|
|
|
return '[Model ' + this.modelName + ']';
|
2012-07-13 13:53:22 +00:00
|
|
|
};
|
2011-10-08 17:11:26 +00:00
|
|
|
|
2012-03-27 14:22:24 +00:00
|
|
|
/**
|
|
|
|
* Convert instance to Object
|
|
|
|
*
|
|
|
|
* @param {Boolean} onlySchema - restrict properties to schema only, default false
|
|
|
|
* when onlySchema == true, only properties defined in schema returned,
|
|
|
|
* otherwise all enumerable properties returned
|
|
|
|
* @returns {Object} - canonical object representation (no getters and setters)
|
|
|
|
*/
|
2013-05-17 15:49:57 +00:00
|
|
|
ModelBaseClass.prototype.toObject = function (onlySchema) {
|
2011-10-08 17:11:26 +00:00
|
|
|
var data = {};
|
2011-10-18 20:36:03 +00:00
|
|
|
var ds = this.constructor.schema.definitions[this.constructor.modelName];
|
|
|
|
var properties = ds.properties;
|
2012-10-13 13:59:25 +00:00
|
|
|
var self = this;
|
|
|
|
|
|
|
|
this.constructor.forEachProperty(function (attr) {
|
|
|
|
if (self[attr] instanceof List) {
|
|
|
|
data[attr] = self[attr].toObject();
|
2012-10-15 21:29:30 +00:00
|
|
|
} else if (self.__data.hasOwnProperty(attr)) {
|
2012-10-13 13:59:25 +00:00
|
|
|
data[attr] = self[attr];
|
2012-09-10 15:57:21 +00:00
|
|
|
} else {
|
2012-10-13 13:59:25 +00:00
|
|
|
data[attr] = null;
|
2012-09-10 15:57:21 +00:00
|
|
|
}
|
2012-10-13 13:59:25 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
if (!onlySchema) {
|
|
|
|
Object.keys(self).forEach(function (attr) {
|
|
|
|
if (!data.hasOwnProperty(attr)) {
|
|
|
|
data[attr] = this[attr];
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2011-10-08 17:11:26 +00:00
|
|
|
return data;
|
|
|
|
};
|
|
|
|
|
2013-05-17 15:49:57 +00:00
|
|
|
// ModelBaseClass.prototype.hasOwnProperty = function (prop) {
|
2012-10-15 21:29:30 +00:00
|
|
|
// return this.__data && this.__data.hasOwnProperty(prop) ||
|
|
|
|
// Object.getOwnPropertyNames(this).indexOf(prop) !== -1;
|
|
|
|
// };
|
2012-10-13 13:59:25 +00:00
|
|
|
|
2013-05-17 15:49:57 +00:00
|
|
|
ModelBaseClass.prototype.toJSON = function () {
|
2012-09-10 15:57:21 +00:00
|
|
|
return this.toObject();
|
|
|
|
};
|
|
|
|
|
2013-05-17 15:49:57 +00:00
|
|
|
ModelBaseClass.prototype.fromObject = function (obj) {
|
2012-04-09 16:24:35 +00:00
|
|
|
Object.keys(obj).forEach(function (key) {
|
|
|
|
this[key] = obj[key];
|
|
|
|
}.bind(this));
|
|
|
|
};
|
|
|
|
|
2011-10-08 17:11:26 +00:00
|
|
|
/**
|
|
|
|
* Checks is property changed based on current property and initial value
|
2012-03-27 14:22:24 +00:00
|
|
|
*
|
|
|
|
* @param {String} attr - property name
|
2011-10-08 17:11:26 +00:00
|
|
|
* @return Boolean
|
|
|
|
*/
|
2013-05-17 15:49:57 +00:00
|
|
|
ModelBaseClass.prototype.propertyChanged = function propertyChanged(attr) {
|
2012-10-13 13:59:25 +00:00
|
|
|
return this.__data[attr] !== this.__dataWas[attr];
|
2011-10-08 17:11:26 +00:00
|
|
|
};
|
|
|
|
|
2012-03-27 14:22:24 +00:00
|
|
|
/**
|
|
|
|
* Reset dirty attributes
|
|
|
|
*
|
|
|
|
* this method does not perform any database operation it just reset object to it's
|
|
|
|
* initial state
|
|
|
|
*/
|
2013-05-17 15:49:57 +00:00
|
|
|
ModelBaseClass.prototype.reset = function () {
|
2011-11-28 16:31:01 +00:00
|
|
|
var obj = this;
|
|
|
|
Object.keys(obj).forEach(function (k) {
|
2011-12-11 08:58:34 +00:00
|
|
|
if (k !== 'id' && !obj.constructor.schema.definitions[obj.constructor.modelName].properties[k]) {
|
|
|
|
delete obj[k];
|
|
|
|
}
|
2011-11-28 16:31:01 +00:00
|
|
|
if (obj.propertyChanged(k)) {
|
|
|
|
obj[k] = obj[k + '_was'];
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2013-05-17 15:49:57 +00:00
|
|
|
ModelBaseClass.prototype.inspect = function () {
|
2012-10-15 23:15:29 +00:00
|
|
|
return util.inspect(this.__data, false, 4, true);
|
|
|
|
};
|
|
|
|
|
2012-03-16 14:42:02 +00:00
|
|
|
|
2012-03-27 14:22:24 +00:00
|
|
|
/**
|
|
|
|
* Check whether `s` is not undefined
|
|
|
|
* @param {Mixed} s
|
|
|
|
* @return {Boolean} s is undefined
|
|
|
|
*/
|
2011-10-08 17:11:26 +00:00
|
|
|
function isdef(s) {
|
|
|
|
var undef;
|
|
|
|
return s !== undef;
|
|
|
|
}
|
|
|
|
|