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');
|
2012-03-16 14:42:02 +00:00
|
|
|
var DEFAULT_CACHE_LIMIT = 1000;
|
2011-10-10 13:22:51 +00:00
|
|
|
|
|
|
|
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 () {
|
2011-12-11 11:01:23 +00:00
|
|
|
if (this.constructor.getter[attr]) {
|
|
|
|
return this.constructor.getter[attr].call(this);
|
|
|
|
} else {
|
|
|
|
return this[_attr];
|
|
|
|
}
|
2011-10-08 17:11:26 +00:00
|
|
|
},
|
|
|
|
set: function (value) {
|
2011-12-11 11:01:23 +00:00
|
|
|
if (this.constructor.setter[attr]) {
|
|
|
|
this.constructor.setter[attr].call(this, value);
|
|
|
|
} else {
|
|
|
|
this[_attr] = value;
|
|
|
|
}
|
2011-10-08 17:11:26 +00:00
|
|
|
},
|
|
|
|
configurable: true,
|
|
|
|
enumerable: true
|
|
|
|
});
|
|
|
|
|
2012-03-03 09:55:29 +00:00
|
|
|
if (data.hasOwnProperty(attr)) {
|
|
|
|
// Getter for initial property
|
|
|
|
Object.defineProperty(this, attr_was, {
|
|
|
|
writable: true,
|
|
|
|
value: data[attr],
|
|
|
|
configurable: true,
|
|
|
|
enumerable: false
|
|
|
|
});
|
|
|
|
}
|
2011-10-08 17:11:26 +00:00
|
|
|
|
|
|
|
}.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
|
|
|
};
|
|
|
|
|
2011-12-11 11:01:23 +00:00
|
|
|
AbstractClass.setter = {};
|
|
|
|
AbstractClass.getter = {};
|
|
|
|
|
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];
|
|
|
|
return ds.properties[propName].type.name;
|
|
|
|
};
|
|
|
|
|
|
|
|
AbstractClass.prototype.whatTypeName = function (propName) {
|
|
|
|
return this.constructor.whatTypeName(propName);
|
|
|
|
};
|
|
|
|
|
2011-10-08 17:11:26 +00:00
|
|
|
/**
|
|
|
|
* @param data [optional]
|
|
|
|
* @param callback(err, obj)
|
|
|
|
*/
|
2012-01-27 08:48:37 +00:00
|
|
|
AbstractClass.create = function (data, callback) {
|
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
|
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);
|
2012-03-03 09:55:29 +00:00
|
|
|
// recall constructor to update _was property states (maybe bad idea)
|
|
|
|
this.call(obj, data);
|
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) {
|
2011-11-19 08:58:49 +00:00
|
|
|
this._adapter().create(modelName, data, function (err, id) {
|
|
|
|
if (id) {
|
|
|
|
defineReadonlyProp(obj, 'id', id);
|
2012-03-16 14:42:02 +00:00
|
|
|
addToCache(this.constructor, obj);
|
2011-11-19 08:58:49 +00:00
|
|
|
}
|
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-22 19:46:16 +00:00
|
|
|
AbstractClass.upsert = AbstractClass.updateOrCreate = function upsert(data, callback) {
|
|
|
|
var Model = this;
|
|
|
|
if (!data.id) return this.create(data, callback);
|
|
|
|
if (this.schema.adapter.updateOrCreate) {
|
|
|
|
this.schema.adapter.updateOrCreate(Model.modelName, data, function (err, data) {
|
|
|
|
var obj = data ? new Model(data) : null;
|
|
|
|
if (obj) {
|
|
|
|
addToCache(Model, obj);
|
|
|
|
}
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2011-10-08 17:11:26 +00:00
|
|
|
AbstractClass.exists = function exists(id, cb) {
|
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
|
|
|
};
|
|
|
|
|
|
|
|
AbstractClass.find = function find(id, cb) {
|
|
|
|
this.schema.adapter.find(this.modelName, id, function (err, data) {
|
|
|
|
var obj = null;
|
|
|
|
if (data) {
|
2012-03-16 14:42:02 +00:00
|
|
|
var cached = getCached(this, data.id);
|
|
|
|
if (cached) {
|
|
|
|
obj = cached;
|
2011-11-23 06:05:11 +00:00
|
|
|
substractDirtyAttributes(obj, data);
|
2011-10-08 17:11:26 +00:00
|
|
|
this.call(obj, data);
|
|
|
|
} else {
|
2012-02-20 18:33:11 +00:00
|
|
|
data.id = id;
|
2011-10-08 17:11:26 +00:00
|
|
|
obj = new this(data);
|
2012-03-16 14:42:02 +00:00
|
|
|
addToCache(this, id);
|
2011-10-08 17:11:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
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;
|
2012-03-03 09:55:29 +00:00
|
|
|
// do not create different instances for the same object
|
2012-03-16 14:42:02 +00:00
|
|
|
var cached = getCached(constr, d.id);
|
|
|
|
if (cached) {
|
|
|
|
obj = cached;
|
2011-11-23 05:38:04 +00:00
|
|
|
// keep dirty attributes untouthed (remove from dataset)
|
2011-11-23 06:05:11 +00:00
|
|
|
substractDirtyAttributes(obj, d);
|
2011-10-08 17:11:26 +00:00
|
|
|
constr.call(obj, d);
|
|
|
|
} else {
|
|
|
|
obj = new constr(d);
|
2012-03-16 14:42:02 +00:00
|
|
|
if (obj.id) addToCache(constr, obj);
|
2011-10-08 17:11:26 +00:00
|
|
|
}
|
|
|
|
return obj;
|
|
|
|
});
|
|
|
|
cb(err, collection);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2012-03-06 12:46:18 +00:00
|
|
|
AbstractClass.findOne = function findOne(params, cb) {
|
|
|
|
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];
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2011-10-08 17:11:26 +00:00
|
|
|
AbstractClass.destroyAll = function destroyAll(cb) {
|
|
|
|
this.schema.adapter.destroyAll(this.modelName, function (err) {
|
2012-03-22 19:46:16 +00:00
|
|
|
clearCache(this);
|
2011-10-08 17:11:26 +00:00
|
|
|
cb(err);
|
|
|
|
}.bind(this));
|
|
|
|
};
|
|
|
|
|
2012-01-30 13:27:26 +00:00
|
|
|
AbstractClass.count = function (where, cb) {
|
|
|
|
if (typeof where === 'function') {
|
|
|
|
cb = where;
|
|
|
|
where = null;
|
|
|
|
}
|
|
|
|
this.schema.adapter.count(this.modelName, cb, where);
|
2011-10-08 17:11:26 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
AbstractClass.toString = function () {
|
|
|
|
return '[Model ' + this.modelName + ']';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
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) {
|
|
|
|
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) {
|
|
|
|
inst._adapter().save(modelName, data, function (err) {
|
2011-11-19 08:58:49 +00:00
|
|
|
if (err) {
|
|
|
|
console.log(err);
|
|
|
|
} else {
|
2011-11-20 05:36:15 +00:00
|
|
|
inst.constructor.call(inst, data);
|
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);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
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;
|
|
|
|
};
|
|
|
|
|
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-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) {
|
2012-03-16 14:42:02 +00:00
|
|
|
removeFromCache(this.constructor, this.id);
|
2011-11-20 05:36:15 +00:00
|
|
|
destroyed(function () {
|
|
|
|
cb && cb(err);
|
|
|
|
});
|
2011-11-17 04:24:43 +00:00
|
|
|
}.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) {
|
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) {
|
|
|
|
cb(new Error('Validation error'));
|
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-03-03 09:55:29 +00:00
|
|
|
inst._adapter().updateAttributes(model, inst.id, data, function (err) {
|
2011-11-20 08:47:49 +00:00
|
|
|
if (!err) {
|
2012-03-03 09:55:29 +00:00
|
|
|
inst.constructor.call(inst, data);
|
|
|
|
/*
|
2011-11-20 08:47:49 +00:00
|
|
|
Object.keys(data).forEach(function (key) {
|
|
|
|
inst[key] = data[key];
|
|
|
|
Object.defineProperty(inst, key + '_was', {
|
|
|
|
writable: false,
|
|
|
|
configurable: true,
|
|
|
|
enumerable: false,
|
|
|
|
value: data[key]
|
|
|
|
});
|
2011-11-19 08:58:49 +00:00
|
|
|
});
|
2012-03-03 09:55:29 +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
|
|
|
});
|
2011-11-20 08:47:49 +00:00
|
|
|
});
|
2011-11-19 08:58:49 +00:00
|
|
|
});
|
|
|
|
}
|
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) {
|
2012-03-16 14:42:02 +00:00
|
|
|
var obj = getCached(this.constructor, this.id);
|
2011-11-27 06:08:40 +00:00
|
|
|
if (obj) {
|
2011-11-28 16:31:01 +00:00
|
|
|
obj.reset();
|
2011-11-27 06:08:40 +00:00
|
|
|
}
|
2011-10-08 17:11:26 +00:00
|
|
|
this.constructor.find(this.id, cb);
|
|
|
|
};
|
|
|
|
|
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'];
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2011-10-08 17:11:26 +00:00
|
|
|
// relations
|
|
|
|
AbstractClass.hasMany = function (anotherClass, params) {
|
|
|
|
var methodName = params.as; // or pluralize(anotherClass.modelName)
|
|
|
|
var fk = params.foreignKey;
|
|
|
|
// 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);
|
|
|
|
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;
|
2012-03-01 17:26:52 +00:00
|
|
|
|
|
|
|
this.schema.defineForeignKey(this.modelName, fk);
|
2012-02-08 05:55:49 +00:00
|
|
|
this.prototype['__finders__'] = this.prototype['__finders__'] || {}
|
|
|
|
|
|
|
|
this.prototype['__finders__'][methodName] = function (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));
|
|
|
|
}
|
|
|
|
|
|
|
|
this.prototype[methodName] = function (p) {
|
2011-10-08 17:11:26 +00:00
|
|
|
if (p instanceof AbstractClass) { // acts as setter
|
|
|
|
this[fk] = p.id;
|
|
|
|
this.cachedRelations[methodName] = p;
|
|
|
|
} else if (typeof p === 'function') { // acts as async getter
|
2012-03-01 17:26:52 +00:00
|
|
|
this.__finders__[methodName](this[fk], p);
|
|
|
|
return this[fk];
|
|
|
|
} 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;
|
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
|
|
|
};
|
|
|
|
|
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
|
|
|
|
//
|
2012-03-16 14:42:02 +00:00
|
|
|
|
2011-10-08 17:11:26 +00:00
|
|
|
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
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2012-03-16 14:42:02 +00:00
|
|
|
function addToCache(constr, obj) {
|
|
|
|
touchCache(constr, obj.id);
|
|
|
|
constr.cache[obj.id] = obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
function touchCache(constr, id) {
|
|
|
|
var cacheLimit = constr.CACHE_LIMIT || DEFAULT_CACHE_LIMIT;
|
|
|
|
|
|
|
|
var ind = constr.mru.indexOf(id);
|
|
|
|
if (~ind) constr.mru.splice(ind, 1);
|
|
|
|
if (constr.mru.length >= cacheLimit * 2) {
|
|
|
|
for (var i = 0; i < cacheLimit;i += 1) {
|
|
|
|
delete constr.cache[constr.mru[i]];
|
|
|
|
}
|
|
|
|
constr.mru.splice(0, cacheLimit);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function getCached(constr, id) {
|
|
|
|
if (id) touchCache(constr, id);
|
|
|
|
return id && constr.cache[id];
|
|
|
|
}
|
|
|
|
|
|
|
|
function clearCache(constr) {
|
|
|
|
constr.cache = {};
|
|
|
|
constr.mru = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
function removeFromCache(constr, id) {
|
|
|
|
var ind = constr.mru.indexOf(id);
|
|
|
|
if (!~ind) constr.mru.splice(ind, 1);
|
|
|
|
delete constr.cache[id];
|
|
|
|
}
|
|
|
|
|