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');
|
|
|
|
var jutil = require('./jutil');
|
2012-03-27 14:22:24 +00:00
|
|
|
var Validatable = require('./validatable').Validatable;
|
2012-09-10 15:57:21 +00:00
|
|
|
var List = require('./list');
|
2012-03-27 14:22:24 +00:00
|
|
|
var Hookable = require('./hookable').Hookable;
|
2012-03-16 14:42:02 +00:00
|
|
|
var DEFAULT_CACHE_LIMIT = 1000;
|
2012-09-10 15:57:21 +00:00
|
|
|
var BASE_TYPES = ['String', 'Boolean', 'Number', 'Date', 'Text'];
|
2011-10-10 13:22:51 +00:00
|
|
|
|
|
|
|
exports.AbstractClass = AbstractClass;
|
|
|
|
|
2012-08-24 08:21:21 +00:00
|
|
|
AbstractClass.__proto__ = Validatable;
|
|
|
|
AbstractClass.prototype.__proto__ = Validatable.prototype;
|
2011-11-17 04:24:43 +00:00
|
|
|
jutil.inherits(AbstractClass, Hookable);
|
2011-10-10 13:22:51 +00:00
|
|
|
|
2011-10-08 17:11:26 +00:00
|
|
|
/**
|
2012-03-27 14:22:24 +00:00
|
|
|
* Abstract class - base class for all persist objects
|
|
|
|
* 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
|
|
|
|
*
|
|
|
|
* `AbstractClass` mixes `Validatable` and `Hookable` classes methods
|
|
|
|
*
|
|
|
|
* @constructor
|
|
|
|
* @param {Object} data - initial object data
|
2011-10-08 17:11:26 +00:00
|
|
|
*/
|
|
|
|
function AbstractClass(data) {
|
2012-04-19 15:01:40 +00:00
|
|
|
this._initProperties(data, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
AbstractClass.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: {}
|
|
|
|
});
|
|
|
|
|
|
|
|
for (var i in data) this.__data[i] = this.__dataWas[i] = data[i];
|
|
|
|
|
|
|
|
if (applySetters && ctor.setter) {
|
|
|
|
Object.keys(ctor.setter).forEach(function (attr) {
|
|
|
|
if (self.__data.hasOwnProperty(attr)) {
|
|
|
|
ctor.setter[attr].call(self, self.__data[attr]);
|
|
|
|
}
|
2011-10-08 17:11:26 +00:00
|
|
|
});
|
2012-10-13 13:59:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ctor.forEachProperty(function (attr) {
|
|
|
|
|
|
|
|
if (!self.__data.hasOwnProperty(attr)) {
|
|
|
|
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) {
|
|
|
|
console.log(e.stack);
|
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 {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
2011-11-17 04:24:43 +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
|
|
|
|
*/
|
2011-12-09 15:23:29 +00:00
|
|
|
AbstractClass.defineProperty = function (prop, params) {
|
|
|
|
this.schema.defineProperty(this.modelName, prop, params);
|
|
|
|
};
|
|
|
|
|
2012-01-18 15:20:05 +00:00
|
|
|
AbstractClass.whatTypeName = function (propName) {
|
|
|
|
var ds = this.schema.definitions[this.modelName];
|
2012-05-29 11:16:24 +00:00
|
|
|
return ds.properties[propName] && ds.properties[propName].type.name;
|
|
|
|
};
|
|
|
|
|
|
|
|
AbstractClass._forDB = function (data) {
|
|
|
|
var res = {};
|
|
|
|
Object.keys(data).forEach(function (propName) {
|
2012-09-11 19:22:55 +00:00
|
|
|
if (this.whatTypeName(propName) === 'JSON' || data[propName] instanceof Array) {
|
2012-05-29 11:16:24 +00:00
|
|
|
res[propName] = JSON.stringify(data[propName]);
|
|
|
|
} else {
|
|
|
|
res[propName] = data[propName];
|
|
|
|
}
|
|
|
|
}.bind(this));
|
|
|
|
return res;
|
2012-01-18 15:20:05 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
AbstractClass.prototype.whatTypeName = function (propName) {
|
|
|
|
return this.constructor.whatTypeName(propName);
|
|
|
|
};
|
|
|
|
|
2011-10-08 17:11:26 +00:00
|
|
|
/**
|
2012-03-27 14:22:24 +00:00
|
|
|
* Create new instance of Model class, saved in database
|
|
|
|
*
|
2011-10-08 17:11:26 +00:00
|
|
|
* @param data [optional]
|
|
|
|
* @param callback(err, obj)
|
2012-03-27 14:22:24 +00:00
|
|
|
* callback called with arguments:
|
|
|
|
*
|
|
|
|
* - err (null or Error)
|
|
|
|
* - instance (null or Model)
|
2011-10-08 17:11:26 +00:00
|
|
|
*/
|
2012-01-27 08:48:37 +00:00
|
|
|
AbstractClass.create = function (data, callback) {
|
2012-03-24 13:50:52 +00:00
|
|
|
if (stillConnecting(this.schema, this, arguments)) return;
|
|
|
|
|
2011-10-08 17:11:26 +00:00
|
|
|
var modelName = this.modelName;
|
|
|
|
|
2012-01-27 08:48:37 +00:00
|
|
|
if (typeof data === 'function') {
|
|
|
|
callback = data;
|
2011-10-08 17:11:26 +00:00
|
|
|
data = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof callback !== 'function') {
|
|
|
|
callback = function () {};
|
|
|
|
}
|
|
|
|
|
|
|
|
var obj = null;
|
2011-10-10 13:22:51 +00:00
|
|
|
// if we come from save
|
2012-10-29 00:05:05 +00:00
|
|
|
if (data instanceof this && !data.id) {
|
2011-10-08 17:11:26 +00:00
|
|
|
obj = data;
|
2011-10-18 20:36:03 +00:00
|
|
|
data = obj.toObject(true);
|
2012-10-29 00:05:05 +00:00
|
|
|
obj._initProperties(data, false);
|
2011-11-19 08:58:49 +00:00
|
|
|
create();
|
2011-10-10 13:22:51 +00:00
|
|
|
} else {
|
|
|
|
obj = new this(data);
|
2011-12-11 11:26:24 +00:00
|
|
|
data = obj.toObject(true);
|
2011-10-10 13:22:51 +00:00
|
|
|
|
|
|
|
// validation required
|
2011-11-19 08:58:49 +00:00
|
|
|
obj.isValid(function (valid) {
|
|
|
|
if (!valid) {
|
|
|
|
callback(new Error('Validation error'), obj);
|
|
|
|
} else {
|
|
|
|
create();
|
|
|
|
}
|
|
|
|
});
|
2011-10-08 17:11:26 +00:00
|
|
|
}
|
|
|
|
|
2011-11-19 08:58:49 +00:00
|
|
|
function create() {
|
2011-11-20 05:36:15 +00:00
|
|
|
obj.trigger('create', function (done) {
|
2012-07-09 01:20:11 +00:00
|
|
|
|
|
|
|
var data = this.toObject(true); // Added this to fix the beforeCreate trigger not fire.
|
|
|
|
// The fix is per issue #72 and the fix was found by by5739.
|
|
|
|
|
2012-05-29 11:16:24 +00:00
|
|
|
this._adapter().create(modelName, this.constructor._forDB(data), function (err, id) {
|
2011-11-19 08:58:49 +00:00
|
|
|
if (id) {
|
2012-10-29 00:05:05 +00:00
|
|
|
obj.__data.id = id;
|
|
|
|
obj.__dataWas.id = id;
|
2011-11-19 08:58:49 +00:00
|
|
|
defineReadonlyProp(obj, 'id', id);
|
|
|
|
}
|
2011-11-20 05:36:15 +00:00
|
|
|
done.call(this, function () {
|
|
|
|
if (callback) {
|
|
|
|
callback(err, obj);
|
|
|
|
}
|
|
|
|
});
|
2011-11-19 08:58:49 +00:00
|
|
|
}.bind(this));
|
|
|
|
});
|
|
|
|
}
|
2011-10-08 17:11:26 +00:00
|
|
|
};
|
|
|
|
|
2012-03-24 13:50:52 +00:00
|
|
|
function stillConnecting(schema, obj, args) {
|
|
|
|
if (schema.connected) return false;
|
|
|
|
var method = args.callee;
|
|
|
|
schema.on('connected', function () {
|
|
|
|
method.apply(obj, [].slice.call(args));
|
|
|
|
});
|
2012-03-25 11:37:44 +00:00
|
|
|
return true;
|
2012-03-24 13:50:52 +00:00
|
|
|
};
|
|
|
|
|
2012-03-27 14:22:24 +00:00
|
|
|
/**
|
|
|
|
* Update or insert
|
|
|
|
*/
|
2012-03-22 19:46:16 +00:00
|
|
|
AbstractClass.upsert = AbstractClass.updateOrCreate = function upsert(data, callback) {
|
2012-03-24 13:50:52 +00:00
|
|
|
if (stillConnecting(this.schema, this, arguments)) return;
|
|
|
|
|
2012-03-22 19:46:16 +00:00
|
|
|
var Model = this;
|
|
|
|
if (!data.id) return this.create(data, callback);
|
|
|
|
if (this.schema.adapter.updateOrCreate) {
|
2012-04-19 16:06:38 +00:00
|
|
|
var inst = new Model(data);
|
|
|
|
this.schema.adapter.updateOrCreate(Model.modelName, inst.toObject(), function (err, data) {
|
|
|
|
var obj;
|
|
|
|
if (data) {
|
|
|
|
inst._initProperties(data);
|
|
|
|
obj = inst;
|
|
|
|
} else {
|
|
|
|
obj = null;
|
|
|
|
}
|
2012-03-22 19:46:16 +00:00
|
|
|
callback(err, obj);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
this.find(data.id, function (err, inst) {
|
|
|
|
if (err) return callback(err);
|
|
|
|
if (inst) {
|
|
|
|
inst.updateAttributes(data, callback);
|
|
|
|
} else {
|
|
|
|
var obj = new Model(data);
|
|
|
|
obj.save(data, callback);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-03-27 14:22:24 +00:00
|
|
|
/**
|
|
|
|
* Check whether object exitst in database
|
|
|
|
*
|
|
|
|
* @param {id} id - identifier of object (primary key value)
|
|
|
|
* @param {Function} cb - callbacl called with (err, exists: Bool)
|
|
|
|
*/
|
2011-10-08 17:11:26 +00:00
|
|
|
AbstractClass.exists = function exists(id, cb) {
|
2012-03-24 13:50:52 +00:00
|
|
|
if (stillConnecting(this.schema, this, arguments)) return;
|
|
|
|
|
2012-01-10 15:43:32 +00:00
|
|
|
if (id) {
|
|
|
|
this.schema.adapter.exists(this.modelName, id, cb);
|
|
|
|
} else {
|
|
|
|
cb(new Error('Model::exists requires positive id argument'));
|
|
|
|
}
|
2011-10-08 17:11:26 +00:00
|
|
|
};
|
|
|
|
|
2012-03-27 14:22:24 +00:00
|
|
|
/**
|
|
|
|
* Find object by id
|
|
|
|
*
|
|
|
|
* @param {id} id - primary key value
|
|
|
|
* @param {Function} cb - callback called with (err, instance)
|
|
|
|
*/
|
2011-10-08 17:11:26 +00:00
|
|
|
AbstractClass.find = function find(id, cb) {
|
2012-03-24 13:50:52 +00:00
|
|
|
if (stillConnecting(this.schema, this, arguments)) return;
|
|
|
|
|
2011-10-08 17:11:26 +00:00
|
|
|
this.schema.adapter.find(this.modelName, id, function (err, data) {
|
|
|
|
var obj = null;
|
|
|
|
if (data) {
|
2012-10-15 22:22:40 +00:00
|
|
|
data.id = id;
|
|
|
|
obj = new this();
|
|
|
|
obj._initProperties(data, false);
|
2011-10-08 17:11:26 +00:00
|
|
|
}
|
|
|
|
cb(err, obj);
|
|
|
|
}.bind(this));
|
|
|
|
};
|
|
|
|
|
2011-11-04 07:30:25 +00:00
|
|
|
/**
|
2012-03-27 14:22:24 +00:00
|
|
|
* Find all instances of Model, matched by query
|
|
|
|
* make sure you have marked as `index: true` fields for filter or sort
|
|
|
|
*
|
|
|
|
* @param {Object} params (optional)
|
|
|
|
*
|
|
|
|
* - where: Object `{ key: val, key2: {gt: 'val2'}}`
|
|
|
|
* - order: String
|
|
|
|
* - limit: Number
|
|
|
|
* - skip: Number
|
|
|
|
*
|
|
|
|
* @param {Function} callback (required) called with arguments:
|
|
|
|
*
|
|
|
|
* - err (null or Error)
|
|
|
|
* - Array of instances
|
2011-11-04 07:30:25 +00:00
|
|
|
*/
|
|
|
|
AbstractClass.all = function all(params, cb) {
|
2012-03-24 13:50:52 +00:00
|
|
|
if (stillConnecting(this.schema, this, arguments)) return;
|
|
|
|
|
2011-10-08 17:11:26 +00:00
|
|
|
if (arguments.length === 1) {
|
2011-11-04 07:30:25 +00:00
|
|
|
cb = params;
|
|
|
|
params = null;
|
2011-10-08 17:11:26 +00:00
|
|
|
}
|
|
|
|
var constr = this;
|
2011-11-04 07:30:25 +00:00
|
|
|
this.schema.adapter.all(this.modelName, params, function (err, data) {
|
2011-10-08 17:11:26 +00:00
|
|
|
if (data && data.map) {
|
2012-10-13 14:25:50 +00:00
|
|
|
data.forEach(function (d, i) {
|
2012-10-15 22:22:40 +00:00
|
|
|
var obj = new constr;
|
|
|
|
obj._initProperties(d, false);
|
|
|
|
data[i] = obj;
|
2011-10-08 17:11:26 +00:00
|
|
|
});
|
2012-08-16 10:21:46 +00:00
|
|
|
if (data && data.countBeforeLimit) {
|
2012-10-16 09:12:34 +00:00
|
|
|
data.countBeforeLimit = data.countBeforeLimit;
|
2012-08-16 10:21:46 +00:00
|
|
|
}
|
2012-10-15 22:22:40 +00:00
|
|
|
cb(err, data);
|
2011-10-08 17:11:26 +00:00
|
|
|
}
|
2012-08-18 14:22:00 +00:00
|
|
|
else
|
|
|
|
cb(err, []);
|
2011-10-08 17:11:26 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2012-03-27 14:22:24 +00:00
|
|
|
/**
|
|
|
|
* Find one record, same as `all`, limited by 1 and return object, not collection
|
|
|
|
*
|
|
|
|
* @param {Object} params - search conditions
|
|
|
|
* @param {Function} cb - callback called with (err, instance)
|
|
|
|
*/
|
2012-03-06 12:46:18 +00:00
|
|
|
AbstractClass.findOne = function findOne(params, cb) {
|
2012-03-24 13:50:52 +00:00
|
|
|
if (stillConnecting(this.schema, this, arguments)) return;
|
|
|
|
|
2012-03-06 12:46:18 +00:00
|
|
|
if (typeof params === 'function') {
|
|
|
|
cb = params;
|
|
|
|
params = {};
|
|
|
|
}
|
|
|
|
params.limit = 1;
|
|
|
|
this.all(params, function (err, collection) {
|
|
|
|
if (err || !collection || !collection.length > 0) return cb(err);
|
|
|
|
cb(err, collection[0]);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2011-11-23 06:05:11 +00:00
|
|
|
function substractDirtyAttributes(object, data) {
|
|
|
|
Object.keys(object.toObject()).forEach(function (attr) {
|
2012-03-03 09:55:29 +00:00
|
|
|
if (data.hasOwnProperty(attr) && object.propertyChanged(attr)) {
|
2011-11-23 06:05:11 +00:00
|
|
|
delete data[attr];
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2012-03-27 14:22:24 +00:00
|
|
|
/**
|
|
|
|
* Destroy all records
|
|
|
|
* @param {Function} cb - callback called with (err)
|
|
|
|
*/
|
2011-10-08 17:11:26 +00:00
|
|
|
AbstractClass.destroyAll = function destroyAll(cb) {
|
2012-03-24 13:50:52 +00:00
|
|
|
if (stillConnecting(this.schema, this, arguments)) return;
|
|
|
|
|
2011-10-08 17:11:26 +00:00
|
|
|
this.schema.adapter.destroyAll(this.modelName, function (err) {
|
|
|
|
cb(err);
|
|
|
|
}.bind(this));
|
|
|
|
};
|
|
|
|
|
2012-03-27 14:22:24 +00:00
|
|
|
/**
|
|
|
|
* Return count of matched records
|
|
|
|
*
|
|
|
|
* @param {Object} where - search conditions (optional)
|
|
|
|
* @param {Function} cb - callback, called with (err, count)
|
|
|
|
*/
|
2012-01-30 13:27:26 +00:00
|
|
|
AbstractClass.count = function (where, cb) {
|
2012-03-24 13:50:52 +00:00
|
|
|
if (stillConnecting(this.schema, this, arguments)) return;
|
|
|
|
|
2012-01-30 13:27:26 +00:00
|
|
|
if (typeof where === 'function') {
|
|
|
|
cb = where;
|
|
|
|
where = null;
|
|
|
|
}
|
|
|
|
this.schema.adapter.count(this.modelName, cb, where);
|
2011-10-08 17:11:26 +00:00
|
|
|
};
|
|
|
|
|
2012-03-27 14:22:24 +00:00
|
|
|
/**
|
|
|
|
* Return string representation of class
|
|
|
|
*
|
|
|
|
* @override default toString method
|
|
|
|
*/
|
2011-10-08 17:11:26 +00:00
|
|
|
AbstractClass.toString = function () {
|
|
|
|
return '[Model ' + this.modelName + ']';
|
2012-07-13 13:53:22 +00:00
|
|
|
};
|
2011-10-08 17:11:26 +00:00
|
|
|
|
|
|
|
/**
|
2011-11-18 09:00:11 +00:00
|
|
|
* Save instance. When instance haven't id, create method called instead.
|
|
|
|
* Triggers: validate, save, update | create
|
2011-10-08 17:11:26 +00:00
|
|
|
* @param options {validate: true, throws: false} [optional]
|
|
|
|
* @param callback(err, obj)
|
|
|
|
*/
|
|
|
|
AbstractClass.prototype.save = function (options, callback) {
|
2012-03-24 13:50:52 +00:00
|
|
|
if (stillConnecting(this.constructor.schema, this, arguments)) return;
|
|
|
|
|
2011-10-08 17:11:26 +00:00
|
|
|
if (typeof options == 'function') {
|
|
|
|
callback = options;
|
|
|
|
options = {};
|
|
|
|
}
|
2011-11-20 05:36:15 +00:00
|
|
|
|
|
|
|
callback = callback || function () {};
|
2011-11-21 21:34:15 +00:00
|
|
|
options = options || {};
|
2011-11-20 05:36:15 +00:00
|
|
|
|
2011-10-08 17:11:26 +00:00
|
|
|
if (!('validate' in options)) {
|
|
|
|
options.validate = true;
|
|
|
|
}
|
|
|
|
if (!('throws' in options)) {
|
|
|
|
options.throws = false;
|
|
|
|
}
|
2011-11-17 07:00:12 +00:00
|
|
|
|
2011-11-19 08:58:49 +00:00
|
|
|
if (options.validate) {
|
|
|
|
this.isValid(function (valid) {
|
|
|
|
if (valid) {
|
|
|
|
save.call(this);
|
|
|
|
} else {
|
|
|
|
var err = new Error('Validation error');
|
|
|
|
// throws option is dangerous for async usage
|
|
|
|
if (options.throws) {
|
|
|
|
throw err;
|
|
|
|
}
|
2011-11-20 05:36:15 +00:00
|
|
|
callback(err, this);
|
2011-11-19 08:58:49 +00:00
|
|
|
}
|
|
|
|
}.bind(this));
|
|
|
|
} else {
|
|
|
|
save.call(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
function save() {
|
2011-11-20 05:36:15 +00:00
|
|
|
this.trigger('save', function (saveDone) {
|
2011-11-19 08:58:49 +00:00
|
|
|
var modelName = this.constructor.modelName;
|
|
|
|
var data = this.toObject(true);
|
2011-11-20 05:36:15 +00:00
|
|
|
var inst = this;
|
|
|
|
if (inst.id) {
|
|
|
|
inst.trigger('update', function (updateDone) {
|
2012-05-29 11:16:24 +00:00
|
|
|
inst._adapter().save(modelName, inst.constructor._forDB(data), function (err) {
|
2011-11-19 08:58:49 +00:00
|
|
|
if (err) {
|
|
|
|
console.log(err);
|
|
|
|
} else {
|
2012-04-19 15:01:40 +00:00
|
|
|
inst._initProperties(data, false);
|
2011-11-19 08:58:49 +00:00
|
|
|
}
|
2011-11-20 05:36:15 +00:00
|
|
|
updateDone.call(inst, function () {
|
|
|
|
saveDone.call(inst, function () {
|
|
|
|
callback(err, inst);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2012-04-09 16:24:35 +00:00
|
|
|
}, data);
|
2011-11-19 08:58:49 +00:00
|
|
|
} else {
|
2011-11-20 05:36:15 +00:00
|
|
|
inst.constructor.create(inst, function (err) {
|
|
|
|
saveDone.call(inst, function () {
|
|
|
|
callback(err, inst);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2011-11-19 08:58:49 +00:00
|
|
|
});
|
2011-11-17 07:00:12 +00:00
|
|
|
}
|
2011-10-08 17:11:26 +00:00
|
|
|
};
|
|
|
|
|
2011-10-10 13:22:51 +00:00
|
|
|
AbstractClass.prototype.isNewRecord = function () {
|
|
|
|
return !this.id;
|
|
|
|
};
|
|
|
|
|
2012-03-27 14:22:24 +00:00
|
|
|
/**
|
|
|
|
* Return adapter of current record
|
|
|
|
* @private
|
|
|
|
*/
|
2011-10-08 17:11:26 +00:00
|
|
|
AbstractClass.prototype._adapter = function () {
|
|
|
|
return this.constructor.schema.adapter;
|
|
|
|
};
|
|
|
|
|
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)
|
|
|
|
*/
|
2011-10-18 20:36:03 +00:00
|
|
|
AbstractClass.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;
|
|
|
|
};
|
|
|
|
|
2012-10-15 21:29:30 +00:00
|
|
|
// AbstractClass.prototype.hasOwnProperty = function (prop) {
|
|
|
|
// return this.__data && this.__data.hasOwnProperty(prop) ||
|
|
|
|
// Object.getOwnPropertyNames(this).indexOf(prop) !== -1;
|
|
|
|
// };
|
2012-10-13 13:59:25 +00:00
|
|
|
|
2012-09-10 15:57:21 +00:00
|
|
|
AbstractClass.prototype.toJSON = function () {
|
|
|
|
return this.toObject();
|
|
|
|
};
|
|
|
|
|
2012-03-27 14:22:24 +00:00
|
|
|
/**
|
|
|
|
* Delete object from persistence
|
|
|
|
*
|
|
|
|
* @triggers `destroy` hook (async) before and after destroying object
|
|
|
|
*/
|
2011-10-08 17:11:26 +00:00
|
|
|
AbstractClass.prototype.destroy = function (cb) {
|
2012-03-24 13:50:52 +00:00
|
|
|
if (stillConnecting(this.constructor.schema, this, arguments)) return;
|
|
|
|
|
2011-11-20 05:36:15 +00:00
|
|
|
this.trigger('destroy', function (destroyed) {
|
2011-11-17 04:24:43 +00:00
|
|
|
this._adapter().destroy(this.constructor.modelName, this.id, function (err) {
|
2011-11-20 05:36:15 +00:00
|
|
|
destroyed(function () {
|
2012-07-13 13:53:22 +00:00
|
|
|
if(cb) cb(err);
|
2011-11-20 05:36:15 +00:00
|
|
|
});
|
2011-11-17 04:24:43 +00:00
|
|
|
}.bind(this));
|
|
|
|
});
|
2011-10-08 17:11:26 +00:00
|
|
|
};
|
|
|
|
|
2012-03-27 14:22:24 +00:00
|
|
|
/**
|
|
|
|
* Update single attribute
|
|
|
|
*
|
|
|
|
* equals to `updateAttributes({name: value}, cb)
|
|
|
|
*
|
|
|
|
* @param {String} name - name of property
|
|
|
|
* @param {Mixed} value - value of property
|
|
|
|
* @param {Function} callback - callback called with (err, instance)
|
|
|
|
*/
|
|
|
|
AbstractClass.prototype.updateAttribute = function updateAttribute(name, value, callback) {
|
2012-04-13 16:58:23 +00:00
|
|
|
var data = {};
|
2011-10-08 17:11:26 +00:00
|
|
|
data[name] = value;
|
2012-03-27 14:22:24 +00:00
|
|
|
this.updateAttributes(data, callback);
|
2011-10-08 17:11:26 +00:00
|
|
|
};
|
|
|
|
|
2012-03-27 14:22:24 +00:00
|
|
|
/**
|
|
|
|
* Update set of attributes
|
|
|
|
*
|
|
|
|
* this method performs validation before updating
|
|
|
|
*
|
|
|
|
* @trigger `validation`, `save` and `update` hooks
|
|
|
|
* @param {Object} data - data to update
|
|
|
|
* @param {Function} callback - callback called with (err, instance)
|
|
|
|
*/
|
2011-10-08 17:11:26 +00:00
|
|
|
AbstractClass.prototype.updateAttributes = function updateAttributes(data, cb) {
|
2012-03-24 13:50:52 +00:00
|
|
|
if (stillConnecting(this.constructor.schema, this, arguments)) return;
|
|
|
|
|
2011-11-20 08:47:49 +00:00
|
|
|
var inst = this;
|
2011-10-08 17:11:26 +00:00
|
|
|
var model = this.constructor.modelName;
|
2012-03-03 09:55:29 +00:00
|
|
|
|
|
|
|
// update instance's properties
|
2011-10-10 13:22:51 +00:00
|
|
|
Object.keys(data).forEach(function (key) {
|
2012-03-03 09:55:29 +00:00
|
|
|
inst[key] = data[key];
|
|
|
|
});
|
2011-11-17 07:00:12 +00:00
|
|
|
|
2012-03-03 09:55:29 +00:00
|
|
|
inst.isValid(function (valid) {
|
2011-11-19 08:58:49 +00:00
|
|
|
if (!valid) {
|
|
|
|
if (cb) {
|
2012-06-26 12:44:57 +00:00
|
|
|
cb(new Error('Validation error'), inst);
|
2011-11-17 04:24:43 +00:00
|
|
|
}
|
2011-11-19 08:58:49 +00:00
|
|
|
} else {
|
2012-03-03 09:55:29 +00:00
|
|
|
update();
|
2011-11-19 08:58:49 +00:00
|
|
|
}
|
2012-03-03 09:55:29 +00:00
|
|
|
});
|
2011-11-19 08:58:49 +00:00
|
|
|
|
|
|
|
function update() {
|
2012-03-03 09:55:29 +00:00
|
|
|
inst.trigger('save', function (saveDone) {
|
|
|
|
inst.trigger('update', function (done) {
|
2011-11-20 08:47:49 +00:00
|
|
|
|
|
|
|
Object.keys(data).forEach(function (key) {
|
2012-03-03 09:55:29 +00:00
|
|
|
data[key] = inst[key];
|
|
|
|
});
|
2011-11-20 08:47:49 +00:00
|
|
|
|
2012-05-29 11:16:24 +00:00
|
|
|
inst._adapter().updateAttributes(model, inst.id, inst.constructor._forDB(data), function (err) {
|
2011-11-20 08:47:49 +00:00
|
|
|
if (!err) {
|
2012-10-13 13:59:25 +00:00
|
|
|
// update _was attrs
|
2011-11-20 08:47:49 +00:00
|
|
|
Object.keys(data).forEach(function (key) {
|
2012-10-13 13:59:25 +00:00
|
|
|
inst.__dataWas[key] = inst.__data[key];
|
2011-11-19 08:58:49 +00:00
|
|
|
});
|
2011-11-20 08:47:49 +00:00
|
|
|
}
|
|
|
|
done.call(inst, function () {
|
|
|
|
saveDone.call(inst, function () {
|
2012-03-22 19:46:16 +00:00
|
|
|
cb(err, inst);
|
2011-11-20 08:47:49 +00:00
|
|
|
});
|
|
|
|
});
|
2012-03-03 09:55:29 +00:00
|
|
|
});
|
2012-04-09 16:24:35 +00:00
|
|
|
}, data);
|
2011-11-19 08:58:49 +00:00
|
|
|
});
|
|
|
|
}
|
2011-10-08 17:11:26 +00:00
|
|
|
};
|
|
|
|
|
2012-04-09 16:24:35 +00:00
|
|
|
AbstractClass.prototype.fromObject = function (obj) {
|
|
|
|
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
|
|
|
|
*/
|
2012-03-27 14:22:24 +00:00
|
|
|
AbstractClass.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
|
|
|
/**
|
|
|
|
* Reload object from persistence
|
|
|
|
*
|
|
|
|
* @requires `id` member of `object` to be able to call `find`
|
|
|
|
* @param {Function} callback - called with (err, instance) arguments
|
|
|
|
*/
|
|
|
|
AbstractClass.prototype.reload = function reload(callback) {
|
2012-03-24 13:50:52 +00:00
|
|
|
if (stillConnecting(this.constructor.schema, this, arguments)) return;
|
|
|
|
|
2012-03-27 14:22:24 +00:00
|
|
|
this.constructor.find(this.id, callback);
|
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
|
|
|
|
*/
|
2011-11-28 16:31:01 +00:00
|
|
|
AbstractClass.prototype.reset = function () {
|
|
|
|
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'];
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2012-03-27 14:22:24 +00:00
|
|
|
/**
|
|
|
|
* Declare hasMany relation
|
|
|
|
*
|
|
|
|
* @param {Class} anotherClass - class to has many
|
|
|
|
* @param {Object} params - configuration {as:, foreignKey:}
|
|
|
|
* @example `User.hasMany(Post, {as: 'posts', foreignKey: 'authorId'});`
|
|
|
|
*/
|
|
|
|
AbstractClass.hasMany = function hasMany(anotherClass, params) {
|
2011-10-08 17:11:26 +00:00
|
|
|
var methodName = params.as; // or pluralize(anotherClass.modelName)
|
|
|
|
var fk = params.foreignKey;
|
2012-12-14 15:28:29 +00:00
|
|
|
|
|
|
|
this.relations[params['as']] = {
|
|
|
|
type: 'hasMany',
|
|
|
|
keyFrom: 'id',
|
|
|
|
keyTo: params['foreignKey'],
|
|
|
|
multiple: true
|
|
|
|
};
|
2011-10-08 17:11:26 +00:00
|
|
|
// each instance of this class should have method named
|
|
|
|
// pluralize(anotherClass.modelName)
|
2011-12-09 15:23:29 +00:00
|
|
|
// which is actually just anotherClass.all({where: {thisModelNameId: this.id}}, cb);
|
2011-10-15 15:57:35 +00:00
|
|
|
defineScope(this.prototype, anotherClass, methodName, function () {
|
|
|
|
var x = {};
|
|
|
|
x[fk] = this.id;
|
2011-11-04 07:30:25 +00:00
|
|
|
return {where: x};
|
2011-10-15 15:57:35 +00:00
|
|
|
}, {
|
|
|
|
find: find,
|
|
|
|
destroy: destroy
|
|
|
|
});
|
2011-10-08 17:11:26 +00:00
|
|
|
|
|
|
|
// obviously, anotherClass should have attribute called `fk`
|
|
|
|
anotherClass.schema.defineForeignKey(anotherClass.modelName, fk);
|
|
|
|
|
2011-10-15 15:57:35 +00:00
|
|
|
function find(id, cb) {
|
|
|
|
anotherClass.find(id, function (err, inst) {
|
|
|
|
if (err) return cb(err);
|
2012-04-11 15:36:10 +00:00
|
|
|
if (!inst) return cb(new Error('Not found'));
|
2012-04-09 16:24:35 +00:00
|
|
|
if (inst[fk] == this.id) {
|
2011-10-15 15:57:35 +00:00
|
|
|
cb(null, inst);
|
|
|
|
} else {
|
|
|
|
cb(new Error('Permission denied'));
|
|
|
|
}
|
|
|
|
}.bind(this));
|
|
|
|
}
|
2011-10-08 17:11:26 +00:00
|
|
|
|
2011-10-15 15:57:35 +00:00
|
|
|
function destroy(id, cb) {
|
|
|
|
this.find(id, function (err, inst) {
|
|
|
|
if (err) return cb(err);
|
|
|
|
if (inst) {
|
|
|
|
inst.destroy(cb);
|
|
|
|
} else {
|
|
|
|
cb(new Error('Not found'));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2011-10-08 17:11:26 +00:00
|
|
|
|
|
|
|
};
|
|
|
|
|
2012-03-27 14:22:24 +00:00
|
|
|
/**
|
|
|
|
* Declare belongsTo relation
|
|
|
|
*
|
|
|
|
* @param {Class} anotherClass - class to belong
|
|
|
|
* @param {Object} params - configuration {as: 'propertyName', foreignKey: 'keyName'}
|
2012-11-03 00:39:35 +00:00
|
|
|
*
|
2012-11-03 01:05:27 +00:00
|
|
|
* **Usage examples**
|
|
|
|
* Suppose model Post have a *belongsTo* relationship with User (the author of the post). You could declare it this way:
|
|
|
|
* Post.belongsTo(User, {as: 'author', foreignKey: 'userId'});
|
2012-11-03 00:39:35 +00:00
|
|
|
*
|
2012-11-03 01:05:27 +00:00
|
|
|
* When a post is loaded, you can load the related author with:
|
|
|
|
* post.author(function(err, user) {
|
|
|
|
* // the user variable is your user object
|
|
|
|
* });
|
2012-11-03 00:39:35 +00:00
|
|
|
*
|
2012-11-03 01:05:27 +00:00
|
|
|
* The related object is cached, so if later you try to get again the author, no additional request will be made.
|
|
|
|
* But there is an optional boolean parameter in first position that set whether or not you want to reload the cache:
|
|
|
|
* post.author(true, function(err, user) {
|
|
|
|
* // The user is reloaded, even if it was already cached.
|
|
|
|
* });
|
2012-11-03 00:39:35 +00:00
|
|
|
*
|
2012-11-03 01:05:27 +00:00
|
|
|
* This optional parameter default value is false, so the related object will be loaded from cache if available.
|
2012-03-27 14:22:24 +00:00
|
|
|
*/
|
2011-10-08 17:11:26 +00:00
|
|
|
AbstractClass.belongsTo = function (anotherClass, params) {
|
|
|
|
var methodName = params.as;
|
|
|
|
var fk = params.foreignKey;
|
2012-03-01 17:26:52 +00:00
|
|
|
|
2012-12-14 15:28:29 +00:00
|
|
|
this.relations[params['as']] = {
|
|
|
|
type: 'belongsTo',
|
|
|
|
keyFrom: params['foreignKey'],
|
|
|
|
keyTo: 'id',
|
|
|
|
multiple: false
|
|
|
|
};
|
|
|
|
|
2012-03-01 17:26:52 +00:00
|
|
|
this.schema.defineForeignKey(this.modelName, fk);
|
2012-07-13 13:53:22 +00:00
|
|
|
this.prototype['__finders__'] = this.prototype['__finders__'] || {};
|
2012-02-08 05:55:49 +00:00
|
|
|
|
|
|
|
this.prototype['__finders__'][methodName] = function (id, cb) {
|
|
|
|
anotherClass.find(id, function (err,inst) {
|
|
|
|
if (err) return cb(err);
|
2012-08-24 08:21:21 +00:00
|
|
|
if (!inst) return cb(null, null);
|
2012-02-08 05:55:49 +00:00
|
|
|
if (inst[fk] === this.id) {
|
|
|
|
cb(null, inst);
|
|
|
|
} else {
|
|
|
|
cb(new Error('Permission denied'));
|
|
|
|
}
|
|
|
|
}.bind(this));
|
2012-07-13 13:53:22 +00:00
|
|
|
};
|
2012-02-08 05:55:49 +00:00
|
|
|
|
2012-11-01 00:13:16 +00:00
|
|
|
this.prototype[methodName] = function (refresh, p) {
|
|
|
|
if (arguments.length === 1) {
|
|
|
|
p = refresh;
|
|
|
|
refresh = false;
|
|
|
|
} else if (arguments.length > 2) {
|
2012-11-03 00:39:35 +00:00
|
|
|
throw new Error('Method can\'t be called with more than two arguments');
|
2012-11-01 00:13:16 +00:00
|
|
|
}
|
2012-10-29 23:37:19 +00:00
|
|
|
var self = this;
|
2012-11-01 00:13:16 +00:00
|
|
|
var cachedValue;
|
|
|
|
if (!refresh && this.__cachedRelations && (typeof this.__cachedRelations[methodName] !== 'undefined')) {
|
2012-10-29 23:37:19 +00:00
|
|
|
cachedValue = this.__cachedRelations[methodName];
|
|
|
|
}
|
2011-10-08 17:11:26 +00:00
|
|
|
if (p instanceof AbstractClass) { // acts as setter
|
|
|
|
this[fk] = p.id;
|
2012-10-29 23:37:19 +00:00
|
|
|
this.__cachedRelations[methodName] = p;
|
2011-10-08 17:11:26 +00:00
|
|
|
} else if (typeof p === 'function') { // acts as async getter
|
2012-11-01 00:13:16 +00:00
|
|
|
if (typeof cachedValue === 'undefined') {
|
2012-10-29 23:37:19 +00:00
|
|
|
this.__finders__[methodName](this[fk], function(err, inst) {
|
|
|
|
if (!err) {
|
|
|
|
self.__cachedRelations[methodName] = inst;
|
|
|
|
}
|
|
|
|
p(err, inst);
|
|
|
|
});
|
|
|
|
return this[fk];
|
|
|
|
} else {
|
|
|
|
p(null, cachedValue);
|
|
|
|
return cachedValue;
|
|
|
|
}
|
2012-03-01 17:26:52 +00:00
|
|
|
} else if (typeof p === 'undefined') { // acts as sync getter
|
2012-02-08 05:55:49 +00:00
|
|
|
return this[fk];
|
2012-03-01 17:26:52 +00:00
|
|
|
} else { // setter
|
|
|
|
this[fk] = p;
|
2012-11-01 00:13:16 +00:00
|
|
|
delete this.__cachedRelations[methodName];
|
2011-10-08 17:11:26 +00:00
|
|
|
}
|
2011-10-15 15:57:35 +00:00
|
|
|
};
|
2012-02-08 05:55:49 +00:00
|
|
|
|
2011-10-08 17:11:26 +00:00
|
|
|
};
|
|
|
|
|
2012-03-27 14:22:24 +00:00
|
|
|
/**
|
|
|
|
* Define scope
|
|
|
|
* TODO: describe behavior and usage examples
|
|
|
|
*/
|
2011-10-15 15:57:35 +00:00
|
|
|
AbstractClass.scope = function (name, params) {
|
|
|
|
defineScope(this, this, name, params);
|
|
|
|
};
|
|
|
|
|
2011-11-08 18:00:44 +00:00
|
|
|
function defineScope(cls, targetClass, name, params, methods) {
|
2011-10-15 15:57:35 +00:00
|
|
|
|
|
|
|
// collect meta info about scope
|
2011-11-08 18:00:44 +00:00
|
|
|
if (!cls._scopeMeta) {
|
|
|
|
cls._scopeMeta = {};
|
2011-10-15 15:57:35 +00:00
|
|
|
}
|
|
|
|
|
2012-10-13 13:59:25 +00:00
|
|
|
// only makes sence to add scope in meta if base and target classes
|
2011-10-15 15:57:35 +00:00
|
|
|
// are same
|
2011-11-08 18:00:44 +00:00
|
|
|
if (cls === targetClass) {
|
|
|
|
cls._scopeMeta[name] = params;
|
2011-10-15 15:57:35 +00:00
|
|
|
} else {
|
|
|
|
if (!targetClass._scopeMeta) {
|
|
|
|
targetClass._scopeMeta = {};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-08 18:00:44 +00:00
|
|
|
Object.defineProperty(cls, name, {
|
2011-10-15 15:57:35 +00:00
|
|
|
enumerable: false,
|
|
|
|
configurable: true,
|
|
|
|
get: function () {
|
2012-10-29 23:37:19 +00:00
|
|
|
var f = function caller(condOrRefresh, cb) {
|
|
|
|
var actualCond = {};
|
|
|
|
var actualRefresh = false;
|
2012-11-01 00:13:16 +00:00
|
|
|
var saveOnCache = true;
|
2011-10-15 15:57:35 +00:00
|
|
|
if (arguments.length === 1) {
|
2012-10-29 23:37:19 +00:00
|
|
|
cb = condOrRefresh;
|
2011-10-15 15:57:35 +00:00
|
|
|
} else if (arguments.length === 2) {
|
2012-10-29 23:37:19 +00:00
|
|
|
if (typeof condOrRefresh === 'boolean') {
|
|
|
|
actualRefresh = condOrRefresh;
|
|
|
|
} else {
|
|
|
|
actualCond = condOrRefresh;
|
2012-11-01 00:13:16 +00:00
|
|
|
actualRefresh = true;
|
|
|
|
saveOnCache = false;
|
2012-10-29 23:37:19 +00:00
|
|
|
}
|
2011-10-15 15:57:35 +00:00
|
|
|
} else {
|
2012-11-03 00:39:35 +00:00
|
|
|
throw new Error('Method can be only called with one or two arguments');
|
2011-10-15 15:57:35 +00:00
|
|
|
}
|
|
|
|
|
2012-11-01 00:13:16 +00:00
|
|
|
if (!this.__cachedRelations || (typeof this.__cachedRelations[name] == 'undefined') || actualRefresh) {
|
2012-10-29 23:37:19 +00:00
|
|
|
var self = this;
|
|
|
|
return targetClass.all(mergeParams(actualCond, caller._scope), function(err, data) {
|
2012-11-01 00:13:16 +00:00
|
|
|
if (!err && saveOnCache) {
|
|
|
|
self.__cachedRelations[name] = data;
|
|
|
|
}
|
2012-10-29 23:37:19 +00:00
|
|
|
cb(err, data);
|
|
|
|
});
|
|
|
|
} else {
|
2012-11-01 00:13:16 +00:00
|
|
|
cb(null, this.__cachedRelations[name]);
|
2012-10-29 23:37:19 +00:00
|
|
|
}
|
2011-10-15 15:57:35 +00:00
|
|
|
};
|
|
|
|
f._scope = typeof params === 'function' ? params.call(this) : params;
|
|
|
|
f.build = build;
|
|
|
|
f.create = create;
|
|
|
|
f.destroyAll = destroyAll;
|
|
|
|
for (var i in methods) {
|
2012-04-07 13:43:15 +00:00
|
|
|
f[i] = methods[i].bind(this);
|
2011-10-15 15:57:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// define sub-scopes
|
|
|
|
Object.keys(targetClass._scopeMeta).forEach(function (name) {
|
|
|
|
Object.defineProperty(f, name, {
|
|
|
|
enumerable: false,
|
|
|
|
get: function () {
|
2011-11-04 07:30:25 +00:00
|
|
|
mergeParams(f._scope, targetClass._scopeMeta[name]);
|
2011-10-15 15:57:35 +00:00
|
|
|
return f;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}.bind(this));
|
|
|
|
return f;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// and it should have create/build methods with binded thisModelNameId param
|
|
|
|
function build(data) {
|
2012-10-13 13:59:25 +00:00
|
|
|
return new targetClass(mergeParams(this._scope, {where:data || {}}).where);
|
2011-10-15 15:57:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function create(data, cb) {
|
|
|
|
if (typeof data === 'function') {
|
|
|
|
cb = data;
|
|
|
|
data = {};
|
|
|
|
}
|
|
|
|
this.build(data).save(cb);
|
|
|
|
}
|
|
|
|
|
2012-07-13 13:53:22 +00:00
|
|
|
/*
|
|
|
|
Callback
|
|
|
|
- The callback will be called after all elements are destroyed
|
|
|
|
- For every destroy call which results in an error
|
|
|
|
- If fetching the Elements on which destroyAll is called results in an error
|
|
|
|
*/
|
2012-03-21 11:12:13 +00:00
|
|
|
function destroyAll(cb) {
|
|
|
|
targetClass.all(this._scope, function (err, data) {
|
|
|
|
if (err) {
|
|
|
|
cb(err);
|
|
|
|
} else {
|
2012-07-13 13:53:22 +00:00
|
|
|
(function loopOfDestruction (data) {
|
|
|
|
if(data.length > 0) {
|
|
|
|
data.shift().destroy(function(err) {
|
|
|
|
if(err && cb) cb(err);
|
|
|
|
loopOfDestruction(data);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
if(cb) cb();
|
|
|
|
}
|
|
|
|
}(data));
|
2012-03-21 11:12:13 +00:00
|
|
|
}
|
|
|
|
});
|
2011-10-15 15:57:35 +00:00
|
|
|
}
|
2011-11-04 07:30:25 +00:00
|
|
|
|
|
|
|
function mergeParams(base, update) {
|
|
|
|
if (update.where) {
|
|
|
|
base.where = merge(base.where, update.where);
|
|
|
|
}
|
|
|
|
|
|
|
|
// overwrite order
|
|
|
|
if (update.order) {
|
|
|
|
base.order = update.order;
|
|
|
|
}
|
|
|
|
|
|
|
|
return base;
|
|
|
|
|
|
|
|
}
|
2011-10-15 15:57:35 +00:00
|
|
|
}
|
|
|
|
|
2012-10-15 23:15:29 +00:00
|
|
|
AbstractClass.prototype.inspect = function () {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2012-03-27 14:22:24 +00:00
|
|
|
/**
|
|
|
|
* Merge `base` and `update` params
|
|
|
|
* @param {Object} base - base object (updating this object)
|
|
|
|
* @param {Object} update - object with new data to update base
|
|
|
|
* @returns {Object} `base`
|
|
|
|
*/
|
2011-10-08 17:11:26 +00:00
|
|
|
function merge(base, update) {
|
2011-10-15 15:57:35 +00:00
|
|
|
base = base || {};
|
|
|
|
if (update) {
|
|
|
|
Object.keys(update).forEach(function (key) {
|
|
|
|
base[key] = update[key];
|
|
|
|
});
|
|
|
|
}
|
2011-10-08 17:11:26 +00:00
|
|
|
return base;
|
|
|
|
}
|
|
|
|
|
2012-03-27 14:22:24 +00:00
|
|
|
/**
|
|
|
|
* Define readonly property on object
|
|
|
|
*
|
|
|
|
* @param {Object} obj
|
|
|
|
* @param {String} key
|
|
|
|
* @param {Mixed} value
|
|
|
|
*/
|
2011-10-08 17:11:26 +00:00
|
|
|
function defineReadonlyProp(obj, key, value) {
|
|
|
|
Object.defineProperty(obj, key, {
|
|
|
|
writable: false,
|
|
|
|
enumerable: true,
|
|
|
|
configurable: true,
|
|
|
|
value: value
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|