2011-10-10 13:22:51 +00:00
|
|
|
/**
|
|
|
|
* Module deps
|
|
|
|
*/
|
|
|
|
var Validatable = require('./validatable').Validatable;
|
2011-11-17 04:24:43 +00:00
|
|
|
var Hookable = require('./hookable').Hookable;
|
2011-10-10 13:22:51 +00:00
|
|
|
var util = require('util');
|
|
|
|
var jutil = require('./jutil');
|
|
|
|
|
|
|
|
exports.AbstractClass = AbstractClass;
|
|
|
|
|
|
|
|
jutil.inherits(AbstractClass, Validatable);
|
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
|
|
|
/**
|
|
|
|
* Abstract class constructor
|
|
|
|
*/
|
|
|
|
function AbstractClass(data) {
|
|
|
|
var self = this;
|
|
|
|
var ds = this.constructor.schema.definitions[this.constructor.modelName];
|
|
|
|
var properties = ds.properties;
|
|
|
|
var settings = ds.setings;
|
|
|
|
data = data || {};
|
|
|
|
|
|
|
|
if (data.id) {
|
|
|
|
defineReadonlyProp(this, 'id', data.id);
|
|
|
|
}
|
|
|
|
|
|
|
|
Object.defineProperty(this, 'cachedRelations', {
|
|
|
|
writable: true,
|
|
|
|
enumerable: false,
|
|
|
|
configurable: true,
|
|
|
|
value: {}
|
|
|
|
});
|
|
|
|
|
|
|
|
Object.keys(properties).forEach(function (attr) {
|
|
|
|
var _attr = '_' + attr,
|
2011-10-11 19:51:32 +00:00
|
|
|
attr_was = attr + '_was';
|
2011-10-08 17:11:26 +00:00
|
|
|
|
|
|
|
// Hidden property to store currrent value
|
|
|
|
Object.defineProperty(this, _attr, {
|
|
|
|
writable: true,
|
|
|
|
enumerable: false,
|
|
|
|
configurable: true,
|
|
|
|
value: isdef(data[attr]) ? data[attr] :
|
2011-10-11 19:51:32 +00:00
|
|
|
(isdef(this[attr]) ? this[attr] : (
|
|
|
|
getDefault(attr)
|
|
|
|
))
|
2011-10-08 17:11:26 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// Public setters and getters
|
|
|
|
Object.defineProperty(this, attr, {
|
|
|
|
get: function () {
|
|
|
|
return this[_attr];
|
|
|
|
},
|
|
|
|
set: function (value) {
|
|
|
|
this[_attr] = value;
|
|
|
|
},
|
|
|
|
configurable: true,
|
|
|
|
enumerable: true
|
|
|
|
});
|
|
|
|
|
|
|
|
// Getter for initial property
|
|
|
|
Object.defineProperty(this, attr_was, {
|
|
|
|
writable: true,
|
|
|
|
value: data[attr],
|
|
|
|
configurable: true,
|
|
|
|
enumerable: false
|
|
|
|
});
|
|
|
|
|
|
|
|
}.bind(this));
|
2011-10-11 19:51:32 +00:00
|
|
|
|
|
|
|
function getDefault(attr) {
|
|
|
|
var def = properties[attr]['default']
|
|
|
|
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");
|
2011-10-08 17:11:26 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param data [optional]
|
|
|
|
* @param callback(err, obj)
|
|
|
|
*/
|
|
|
|
AbstractClass.create = function (data) {
|
|
|
|
var modelName = this.modelName;
|
|
|
|
|
|
|
|
// define callback manually
|
|
|
|
var callback = arguments[arguments.length - 1];
|
|
|
|
if (arguments.length == 0 || data === callback) {
|
|
|
|
data = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof callback !== 'function') {
|
|
|
|
callback = function () {};
|
|
|
|
}
|
|
|
|
|
|
|
|
var obj = null;
|
2011-10-10 13:22:51 +00:00
|
|
|
// if we come from save
|
2011-10-08 17:11:26 +00:00
|
|
|
if (data instanceof AbstractClass && !data.id) {
|
|
|
|
obj = data;
|
2011-10-18 20:36:03 +00:00
|
|
|
data = obj.toObject(true);
|
2011-10-10 13:22:51 +00:00
|
|
|
} else {
|
|
|
|
obj = new this(data);
|
|
|
|
|
|
|
|
// validation required
|
2011-11-17 04:24:43 +00:00
|
|
|
obj.trigger("validation", function(){
|
|
|
|
if (!obj.isValid()) {
|
|
|
|
return callback(new Error('Validation error'), obj);
|
|
|
|
}
|
|
|
|
});
|
2011-10-08 17:11:26 +00:00
|
|
|
}
|
|
|
|
|
2011-11-17 04:24:43 +00:00
|
|
|
obj.trigger("create", function(){
|
|
|
|
this._adapter().create(modelName, data, function (err, id) {
|
|
|
|
if (id) {
|
|
|
|
defineReadonlyProp(obj, 'id', id);
|
|
|
|
this.constructor.cache[id] = obj;
|
|
|
|
}
|
|
|
|
if (callback) {
|
|
|
|
callback(err, obj);
|
|
|
|
}
|
|
|
|
}.bind(this));
|
|
|
|
});
|
2011-10-08 17:11:26 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
AbstractClass.exists = function exists(id, cb) {
|
|
|
|
this.schema.adapter.exists(this.modelName, id, cb);
|
|
|
|
};
|
|
|
|
|
|
|
|
AbstractClass.find = function find(id, cb) {
|
|
|
|
this.schema.adapter.find(this.modelName, id, function (err, data) {
|
|
|
|
var obj = null;
|
|
|
|
if (data) {
|
|
|
|
if (this.cache[data.id]) {
|
|
|
|
obj = this.cache[data.id];
|
|
|
|
this.call(obj, data);
|
|
|
|
} else {
|
|
|
|
obj = new this(data);
|
|
|
|
this.cache[data.id] = obj;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
cb(err, obj);
|
|
|
|
}.bind(this));
|
|
|
|
};
|
|
|
|
|
2011-11-04 07:30:25 +00:00
|
|
|
/**
|
|
|
|
* Query collection of objects
|
|
|
|
* @param params {where: {}, order: '', limit: 1, offset: 0,...}
|
|
|
|
* @param cb (err, array of AbstractClass)
|
|
|
|
*/
|
|
|
|
AbstractClass.all = function all(params, cb) {
|
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
|
|
|
var collection = null;
|
|
|
|
if (data && data.map) {
|
|
|
|
collection = data.map(function (d) {
|
|
|
|
var obj = null;
|
2011-11-04 07:30:25 +00:00
|
|
|
// really questionable stuff
|
|
|
|
// goal is obvious: to not create different instances for the same object
|
|
|
|
// but the way it implemented...
|
|
|
|
// we can lost some dirty state of object, for example
|
|
|
|
// TODO: think about better implementation, test keeping dirty state
|
2011-10-08 17:11:26 +00:00
|
|
|
if (constr.cache[d.id]) {
|
|
|
|
obj = constr.cache[d.id];
|
|
|
|
constr.call(obj, d);
|
|
|
|
} else {
|
|
|
|
obj = new constr(d);
|
|
|
|
constr.cache[d.id] = obj;
|
|
|
|
}
|
|
|
|
return obj;
|
|
|
|
});
|
|
|
|
cb(err, collection);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
AbstractClass.destroyAll = function destroyAll(cb) {
|
|
|
|
this.schema.adapter.destroyAll(this.modelName, function (err) {
|
|
|
|
if (!err) {
|
|
|
|
Object.keys(this.cache).forEach(function (id) {
|
|
|
|
delete this.cache[id];
|
|
|
|
}.bind(this));
|
|
|
|
}
|
|
|
|
cb(err);
|
|
|
|
}.bind(this));
|
|
|
|
};
|
|
|
|
|
|
|
|
AbstractClass.count = function (cb) {
|
|
|
|
this.schema.adapter.count(this.modelName, cb);
|
|
|
|
};
|
|
|
|
|
|
|
|
AbstractClass.toString = function () {
|
|
|
|
return '[Model ' + this.modelName + ']';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param options {validate: true, throws: false} [optional]
|
|
|
|
* @param callback(err, obj)
|
|
|
|
*/
|
|
|
|
AbstractClass.prototype.save = function (options, callback) {
|
|
|
|
if (typeof options == 'function') {
|
|
|
|
callback = options;
|
|
|
|
options = {};
|
|
|
|
}
|
|
|
|
if (!('validate' in options)) {
|
|
|
|
options.validate = true;
|
|
|
|
}
|
|
|
|
if (!('throws' in options)) {
|
|
|
|
options.throws = false;
|
|
|
|
}
|
2011-11-17 04:24:43 +00:00
|
|
|
this.trigger("validation", function(){
|
|
|
|
if (options.validate && !this.isValid()) {
|
|
|
|
var err = new Error('Validation error');
|
|
|
|
if (options.throws) {
|
|
|
|
throw err;
|
2011-10-08 17:11:26 +00:00
|
|
|
}
|
2011-11-17 04:24:43 +00:00
|
|
|
return callback && callback(err);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
this.trigger("save", function(){
|
|
|
|
var modelName = this.constructor.modelName;
|
|
|
|
var data = this.toObject(true);
|
|
|
|
if (this.id) {
|
|
|
|
this.trigger("update", function(){
|
|
|
|
this._adapter().save(modelName, data, function (err) {
|
|
|
|
if (err) {
|
|
|
|
console.log(err);
|
|
|
|
} else {
|
|
|
|
this.constructor.call(this, data);
|
|
|
|
}
|
|
|
|
if (callback) {
|
|
|
|
callback(err, this);
|
|
|
|
}
|
|
|
|
}.bind(this));
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
this.constructor.create(this, callback);
|
|
|
|
}
|
|
|
|
});
|
2011-10-08 17:11:26 +00:00
|
|
|
};
|
|
|
|
|
2011-10-10 13:22:51 +00:00
|
|
|
AbstractClass.prototype.isNewRecord = function () {
|
|
|
|
return !this.id;
|
|
|
|
};
|
|
|
|
|
2011-10-08 17:11:26 +00:00
|
|
|
AbstractClass.prototype._adapter = function () {
|
|
|
|
return this.constructor.schema.adapter;
|
|
|
|
};
|
|
|
|
|
|
|
|
AbstractClass.prototype.propertyChanged = function (name) {
|
|
|
|
return this[name + '_was'] !== this['_' + name];
|
|
|
|
};
|
|
|
|
|
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;
|
2011-10-19 17:17:48 +00:00
|
|
|
// weird
|
2011-10-18 20:36:03 +00:00
|
|
|
Object.keys(onlySchema ? properties : this).concat(['id']).forEach(function (property) {
|
2011-10-08 17:11:26 +00:00
|
|
|
data[property] = this[property];
|
|
|
|
}.bind(this));
|
|
|
|
return data;
|
|
|
|
};
|
|
|
|
|
|
|
|
AbstractClass.prototype.destroy = function (cb) {
|
2011-11-17 04:24:43 +00:00
|
|
|
this.trigger("destroy", function(){
|
|
|
|
this._adapter().destroy(this.constructor.modelName, this.id, function (err) {
|
|
|
|
delete this.constructor.cache[this.id];
|
|
|
|
cb && cb(err);
|
|
|
|
}.bind(this));
|
|
|
|
});
|
2011-10-08 17:11:26 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
AbstractClass.prototype.updateAttribute = function (name, value, cb) {
|
|
|
|
data = {};
|
|
|
|
data[name] = value;
|
|
|
|
this.updateAttributes(data, cb);
|
|
|
|
};
|
|
|
|
|
|
|
|
AbstractClass.prototype.updateAttributes = function updateAttributes(data, cb) {
|
|
|
|
var model = this.constructor.modelName;
|
2011-10-10 13:22:51 +00:00
|
|
|
Object.keys(data).forEach(function (key) {
|
|
|
|
this[key] = data[key];
|
|
|
|
}.bind(this));
|
2011-11-17 04:24:43 +00:00
|
|
|
this.trigger("validation", function(){
|
|
|
|
if (!this.isValid()) {
|
|
|
|
var err = new Error('Validation error');
|
|
|
|
return cb && cb(err);
|
2011-10-08 17:11:26 +00:00
|
|
|
}
|
2011-11-17 04:24:43 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
this.trigger("update", function(){
|
|
|
|
this._adapter().updateAttributes(model, this.id, data, function (err) {
|
|
|
|
if (!err) {
|
|
|
|
Object.keys(data).forEach(function (key) {
|
|
|
|
this[key] = data[key];
|
|
|
|
Object.defineProperty(this, key + '_was', {
|
|
|
|
writable: false,
|
|
|
|
configurable: true,
|
|
|
|
enumerable: false,
|
|
|
|
value: data[key]
|
|
|
|
});
|
|
|
|
}.bind(this));
|
|
|
|
}
|
|
|
|
cb(err);
|
|
|
|
}.bind(this));
|
|
|
|
});
|
2011-10-08 17:11:26 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks is property changed based on current property and initial value
|
|
|
|
* @param {attr} String - property name
|
|
|
|
* @return Boolean
|
|
|
|
*/
|
|
|
|
AbstractClass.prototype.propertyChanged = function (attr) {
|
|
|
|
return this['_' + attr] !== this[attr + '_was'];
|
|
|
|
};
|
|
|
|
|
|
|
|
AbstractClass.prototype.reload = function (cb) {
|
|
|
|
this.constructor.find(this.id, cb);
|
|
|
|
};
|
|
|
|
|
|
|
|
// relations
|
|
|
|
AbstractClass.hasMany = function (anotherClass, params) {
|
|
|
|
var methodName = params.as; // or pluralize(anotherClass.modelName)
|
|
|
|
var fk = params.foreignKey;
|
|
|
|
// console.log(this.modelName, 'has many', anotherClass.modelName, 'as', params.as, 'queried by', params.foreignKey);
|
|
|
|
// each instance of this class should have method named
|
|
|
|
// pluralize(anotherClass.modelName)
|
|
|
|
// which is actually just anotherClass.all({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);
|
|
|
|
if (inst[fk] === this.id) {
|
|
|
|
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
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
AbstractClass.belongsTo = function (anotherClass, params) {
|
|
|
|
var methodName = params.as;
|
|
|
|
var fk = params.foreignKey;
|
2011-10-15 15:57:35 +00:00
|
|
|
this.schema.defineForeignKey(anotherClass.modelName, fk);
|
2011-10-08 17:11:26 +00:00
|
|
|
this.prototype[methodName] = function (p, cb) {
|
|
|
|
if (p instanceof AbstractClass) { // acts as setter
|
|
|
|
this[fk] = p.id;
|
|
|
|
this.cachedRelations[methodName] = p;
|
|
|
|
} else if (typeof p === 'function') { // acts as async getter
|
|
|
|
this.find(this[fk], function (err, obj) {
|
|
|
|
if (err) return p(err);
|
|
|
|
this.cachedRelations[methodName] = obj;
|
|
|
|
}.bind(this));
|
|
|
|
} else if (!p) { // acts as sync getter
|
|
|
|
return this.cachedRelations[methodName] || this[fk];
|
|
|
|
}
|
2011-10-15 15:57:35 +00:00
|
|
|
};
|
2011-10-08 17:11:26 +00:00
|
|
|
};
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
// anly make sence to add scope in meta if base and target classes
|
|
|
|
// 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 () {
|
|
|
|
var f = function caller(cond, cb) {
|
|
|
|
var actualCond;
|
|
|
|
if (arguments.length === 1) {
|
|
|
|
actualCond = {};
|
|
|
|
cb = cond;
|
|
|
|
} else if (arguments.length === 2) {
|
|
|
|
actualCond = cond;
|
|
|
|
} else {
|
|
|
|
throw new Error('Method only can be called with one or two arguments');
|
|
|
|
}
|
|
|
|
|
2011-11-04 07:30:25 +00:00
|
|
|
return targetClass.all(mergeParams(actualCond, caller._scope), cb);
|
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) {
|
|
|
|
f[i] = methods;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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) {
|
|
|
|
data = data || {};
|
2011-11-04 07:30: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);
|
|
|
|
}
|
|
|
|
|
|
|
|
function destroyAll(id, cb) {
|
|
|
|
// implement me
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2011-10-08 17:11:26 +00:00
|
|
|
// helper methods
|
|
|
|
//
|
|
|
|
function isdef(s) {
|
|
|
|
var undef;
|
|
|
|
return s !== undef;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
function defineReadonlyProp(obj, key, value) {
|
|
|
|
Object.defineProperty(obj, key, {
|
|
|
|
writable: false,
|
|
|
|
enumerable: true,
|
|
|
|
configurable: true,
|
|
|
|
value: value
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|