loopback-datasource-juggler/lib/scope.js

199 lines
6.1 KiB
JavaScript
Raw Normal View History

2013-04-11 23:23:34 +00:00
/**
* Module exports
*/
exports.defineScope = defineScope;
function defineScope(cls, targetClass, name, params, methods) {
// collect meta info about scope
if (!cls._scopeMeta) {
cls._scopeMeta = {};
}
// only makes sence to add scope in meta if base and target classes
// are same
if (cls === targetClass) {
cls._scopeMeta[name] = params;
} else {
if (!targetClass._scopeMeta) {
targetClass._scopeMeta = {};
}
}
// Define a property for the scope
2013-04-11 23:23:34 +00:00
Object.defineProperty(cls, name, {
enumerable: false,
configurable: true,
get: function () {
var f = function caller(condOrRefresh, cb) {
var actualCond = {};
var actualRefresh = false;
var saveOnCache = true;
if (arguments.length === 1) {
cb = condOrRefresh;
} else if (arguments.length === 2) {
if (typeof condOrRefresh === 'boolean') {
actualRefresh = condOrRefresh;
} else {
actualCond = condOrRefresh;
actualRefresh = true;
saveOnCache = false;
}
} else {
throw new Error('Method can be only called with one or two arguments');
}
if (!this.__cachedRelations || (typeof this.__cachedRelations[name] == 'undefined') || actualRefresh) {
var self = this;
var params = mergeParams(actualCond, caller._scope);
return targetClass.find(params, function(err, data) {
2013-04-11 23:23:34 +00:00
if (!err && saveOnCache) {
2013-04-11 23:56:51 +00:00
if (!self.__cachedRelations) {
self.__cachedRelations = {};
}
2013-04-11 23:23:34 +00:00
self.__cachedRelations[name] = data;
}
cb(err, data);
});
} else {
cb(null, this.__cachedRelations[name]);
}
};
f._scope = typeof params === 'function' ? params.call(this) : params;
2013-12-21 01:28:21 +00:00
2013-04-11 23:23:34 +00:00
f.build = build;
f.create = create;
f.destroyAll = destroyAll;
for (var i in methods) {
f[i] = methods[i].bind(this);
}
// define sub-scopes
Object.keys(targetClass._scopeMeta).forEach(function (name) {
Object.defineProperty(f, name, {
enumerable: false,
get: function () {
mergeParams(f._scope, targetClass._scopeMeta[name]);
return f;
}
});
}.bind(this));
return f;
}
});
// Wrap the property into a function for remoting
var fn = function() {
2013-12-21 01:28:21 +00:00
var f = this[name];
f.apply(this[name], arguments);
};
fn.shared = true;
fn.http = {verb: 'get', path: '/' + name};
fn.accepts = {arg: 'where', type: 'object'};
fn.description = 'Fetches ' + name;
fn.returns = {arg: name, type: 'array', root: true};
cls['__get__' + name] = fn;
var fn_create = function() {
2013-12-21 01:28:21 +00:00
var f = this[name].create;
f.apply(this[name], arguments);
};
fn_create.shared = true;
fn_create.http = {verb: 'post', path: '/' + name};
2013-12-21 01:28:21 +00:00
fn_create.accepts = {arg: 'data', type: 'object', http: {source: 'body'}};
fn_create.description = 'Creates ' + name;
fn_create.returns = {arg: 'data', type: 'object', root: true};
cls['__create__' + name] = fn_create;
var fn_delete = function() {
2013-12-21 01:28:21 +00:00
var f = this[name].destroyAll;
f.apply(this[name], arguments);
};
fn_delete.shared = true;
fn_delete.http = {verb: 'delete', path: '/' + name};
fn_delete.description = 'Deletes ' + name;
fn_delete.returns = {arg: 'data', type: 'object', root: true};
cls['__delete__' + name] = fn_delete;
2013-04-11 23:23:34 +00:00
// and it should have create/build methods with binded thisModelNameId param
function build(data) {
return new targetClass(mergeParams(this._scope, {where:data || {}}).where);
}
function create(data, cb) {
if (typeof data === 'function') {
cb = data;
data = {};
}
this.build(data).save(cb);
}
/*
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
*/
function destroyAll(cb) {
targetClass.find(this._scope, function (err, data) {
2013-04-11 23:23:34 +00:00
if (err) {
cb(err);
} else {
(function loopOfDestruction (data) {
if(data.length > 0) {
data.shift().destroy(function(err) {
if(err && cb) cb(err);
loopOfDestruction(data);
});
} else {
if(cb) cb();
}
}(data));
}
});
}
function mergeParams(base, update) {
2013-08-29 04:53:55 +00:00
base = base || {};
2013-04-11 23:23:34 +00:00
if (update.where) {
base.where = merge(base.where, update.where);
}
if (update.include) {
base.include = update.include;
}
if (update.collect) {
base.collect = update.collect;
}
2013-04-11 23:23:34 +00:00
// overwrite order
if (update.order) {
base.order = update.order;
}
return base;
}
}
/**
* 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`
*/
function merge(base, update) {
base = base || {};
if (update) {
Object.keys(update).forEach(function (key) {
base[key] = update[key];
});
}
return base;
}