2020-01-21 18:12:14 +00:00
|
|
|
// Copyright IBM Corp. 2013,2019. All Rights Reserved.
|
2016-04-01 22:25:16 +00:00
|
|
|
// Node module: loopback-datasource-juggler
|
|
|
|
// This file is licensed under the MIT License.
|
|
|
|
// License text available at https://opensource.org/licenses/MIT
|
2019-05-08 15:45:37 +00:00
|
|
|
|
2016-08-22 19:55:22 +00:00
|
|
|
'use strict';
|
2016-04-01 22:25:16 +00:00
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
const _ = require('lodash');
|
|
|
|
const i8n = require('inflection');
|
|
|
|
const g = require('strong-globalize')();
|
|
|
|
const utils = require('./utils');
|
|
|
|
const defineCachedRelations = utils.defineCachedRelations;
|
|
|
|
const setScopeValuesFromWhere = utils.setScopeValuesFromWhere;
|
|
|
|
const mergeQuery = utils.mergeQuery;
|
|
|
|
const DefaultModelBaseClass = require('./model.js');
|
|
|
|
const collectTargetIds = utils.collectTargetIds;
|
|
|
|
const idName = utils.idName;
|
|
|
|
const deprecated = require('depd')('loopback-datasource-juggler');
|
2014-08-30 18:58:06 +00:00
|
|
|
|
2013-04-11 23:23:34 +00:00
|
|
|
/**
|
|
|
|
* Module exports
|
|
|
|
*/
|
|
|
|
exports.defineScope = defineScope;
|
|
|
|
|
2014-06-16 17:50:42 +00:00
|
|
|
function ScopeDefinition(definition) {
|
2014-08-08 22:52:30 +00:00
|
|
|
this.isStatic = definition.isStatic;
|
|
|
|
this.modelFrom = definition.modelFrom;
|
|
|
|
this.modelTo = definition.modelTo || definition.modelFrom;
|
2014-06-16 17:50:42 +00:00
|
|
|
this.name = definition.name;
|
|
|
|
this.params = definition.params;
|
2014-08-30 18:58:06 +00:00
|
|
|
this.methods = definition.methods || {};
|
|
|
|
this.options = definition.options || {};
|
2014-06-16 17:50:42 +00:00
|
|
|
}
|
|
|
|
|
2014-08-30 18:58:06 +00:00
|
|
|
ScopeDefinition.prototype.targetModel = function(receiver) {
|
2018-12-07 14:54:29 +00:00
|
|
|
let modelTo;
|
2014-08-30 18:58:06 +00:00
|
|
|
if (typeof this.options.modelTo === 'function') {
|
2015-05-16 17:11:17 +00:00
|
|
|
modelTo = this.options.modelTo.call(this, receiver) || this.modelTo;
|
2014-08-30 18:58:06 +00:00
|
|
|
} else {
|
2015-05-16 17:11:17 +00:00
|
|
|
modelTo = this.modelTo;
|
2014-08-30 18:58:06 +00:00
|
|
|
}
|
|
|
|
if (!(modelTo.prototype instanceof DefaultModelBaseClass)) {
|
2018-12-07 14:54:29 +00:00
|
|
|
let msg = 'Invalid target model for scope `';
|
2014-08-30 18:58:06 +00:00
|
|
|
msg += (this.isStatic ? this.modelFrom : this.modelFrom.constructor).modelName;
|
|
|
|
msg += this.isStatic ? '.' : '.prototype.';
|
|
|
|
msg += this.name + '`.';
|
|
|
|
throw new Error(msg);
|
|
|
|
}
|
|
|
|
return modelTo;
|
|
|
|
};
|
|
|
|
|
2015-05-16 17:11:17 +00:00
|
|
|
/*!
|
|
|
|
* Find related model instances
|
|
|
|
* @param {*} receiver The target model class/prototype
|
|
|
|
* @param {Object|Function} scopeParams
|
|
|
|
* @param {Boolean|Object} [condOrRefresh] true for refresh or object as a filter
|
|
|
|
* @param {Object} [options]
|
|
|
|
* @param {Function} cb
|
|
|
|
* @returns {*}
|
|
|
|
*/
|
|
|
|
ScopeDefinition.prototype.related = function(receiver, scopeParams, condOrRefresh, options, cb) {
|
2018-12-07 14:54:29 +00:00
|
|
|
const name = this.name;
|
|
|
|
const self = receiver;
|
2015-02-18 05:04:31 +00:00
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
let actualCond = {};
|
|
|
|
let actualRefresh = false;
|
|
|
|
let saveOnCache = receiver instanceof DefaultModelBaseClass;
|
2015-05-16 17:11:17 +00:00
|
|
|
if (typeof condOrRefresh === 'function' &&
|
|
|
|
options === undefined && cb === undefined) {
|
|
|
|
// related(receiver, scopeParams, cb)
|
2014-06-16 17:50:42 +00:00
|
|
|
cb = condOrRefresh;
|
2015-05-16 17:11:17 +00:00
|
|
|
options = {};
|
|
|
|
condOrRefresh = undefined;
|
|
|
|
} else if (typeof options === 'function' && cb === undefined) {
|
|
|
|
cb = options;
|
|
|
|
options = {};
|
|
|
|
}
|
|
|
|
options = options || {};
|
|
|
|
if (condOrRefresh !== undefined) {
|
2014-06-16 17:50:42 +00:00
|
|
|
if (typeof condOrRefresh === 'boolean') {
|
|
|
|
actualRefresh = condOrRefresh;
|
|
|
|
} else {
|
|
|
|
actualCond = condOrRefresh;
|
|
|
|
actualRefresh = true;
|
|
|
|
saveOnCache = false;
|
|
|
|
}
|
|
|
|
}
|
2015-05-16 17:11:17 +00:00
|
|
|
cb = cb || utils.createPromiseCallback();
|
2017-08-01 18:15:21 +00:00
|
|
|
const refreshIsNeeded = !self.__cachedRelations ||
|
|
|
|
self.__cachedRelations[name] === undefined ||
|
|
|
|
actualRefresh;
|
|
|
|
if (refreshIsNeeded) {
|
2014-06-16 17:50:42 +00:00
|
|
|
// It either doesn't hit the cache or refresh is required
|
2018-12-07 14:54:29 +00:00
|
|
|
const params = mergeQuery(actualCond, scopeParams, {nestedInclude: true});
|
|
|
|
const targetModel = this.targetModel(receiver);
|
2016-10-27 14:18:54 +00:00
|
|
|
|
|
|
|
// If there is a through model
|
|
|
|
// run another query to apply filter on relatedModel(targetModel)
|
|
|
|
// see github.com/strongloop/loopback-datasource-juggler/issues/166
|
2018-12-07 14:54:29 +00:00
|
|
|
const scopeOnRelatedModel = params.collect &&
|
2017-08-01 18:15:21 +00:00
|
|
|
params.include.scope !== null &&
|
|
|
|
typeof params.include.scope === 'object';
|
2018-12-07 15:22:36 +00:00
|
|
|
let filter, queryRelated;
|
2016-10-27 14:18:54 +00:00
|
|
|
if (scopeOnRelatedModel) {
|
2018-12-07 15:22:36 +00:00
|
|
|
filter = params.include;
|
2016-10-27 14:18:54 +00:00
|
|
|
// The filter applied on relatedModel
|
2018-12-07 15:22:36 +00:00
|
|
|
queryRelated = filter.scope;
|
2016-10-27 14:18:54 +00:00
|
|
|
delete params.include.scope;
|
2018-12-07 14:43:40 +00:00
|
|
|
}
|
2016-10-27 14:18:54 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
targetModel.find(params, options, function(err, data) {
|
2014-06-16 17:50:42 +00:00
|
|
|
if (!err && saveOnCache) {
|
|
|
|
defineCachedRelations(self);
|
|
|
|
self.__cachedRelations[name] = data;
|
|
|
|
}
|
2016-10-27 14:18:54 +00:00
|
|
|
|
|
|
|
if (scopeOnRelatedModel === true) {
|
2018-12-07 14:54:29 +00:00
|
|
|
const relatedModel = targetModel.relations[filter.relation].modelTo;
|
|
|
|
const IdKey = idName(relatedModel);
|
2016-10-27 14:18:54 +00:00
|
|
|
|
2017-11-14 19:22:48 +00:00
|
|
|
// return {inq: [1,2,3]}}
|
2018-12-07 14:54:29 +00:00
|
|
|
const smartMerge = function(idCollection, qWhere) {
|
2017-11-14 19:22:48 +00:00
|
|
|
if (!qWhere[IdKey]) return idCollection;
|
2018-12-07 14:54:29 +00:00
|
|
|
let merged = {};
|
2017-11-14 19:22:48 +00:00
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
const idsA = idCollection.inq;
|
|
|
|
const idsB = qWhere[IdKey].inq ? qWhere[IdKey].inq : [qWhere[IdKey]];
|
2017-11-14 19:22:48 +00:00
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
const intersect = _.intersectionWith(idsA, idsB, _.isEqual);
|
2017-11-14 19:22:48 +00:00
|
|
|
if (intersect.length === 1) merged = intersect[0];
|
|
|
|
if (intersect.length > 1) merged = {inq: intersect};
|
|
|
|
|
2017-05-01 01:59:44 +00:00
|
|
|
return merged;
|
|
|
|
};
|
2017-11-14 19:22:48 +00:00
|
|
|
|
2016-10-27 14:18:54 +00:00
|
|
|
if (queryRelated.where !== undefined) {
|
2017-11-14 19:22:48 +00:00
|
|
|
// Merge queryRelated filter and targetId filter
|
2018-12-07 14:54:29 +00:00
|
|
|
const IdKeyCondition = {};
|
2017-11-14 19:22:48 +00:00
|
|
|
IdKeyCondition[IdKey] = smartMerge(collectTargetIds(data, IdKey),
|
|
|
|
queryRelated.where);
|
|
|
|
|
|
|
|
// if the id in filter doesn't exist after the merge,
|
|
|
|
// return empty result
|
|
|
|
if (_.isObject(IdKeyCondition[IdKey]) && _.isEmpty(IdKeyCondition[IdKey])) return cb(null, []);
|
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
const mergedWhere = {
|
2017-11-14 19:22:48 +00:00
|
|
|
and: [
|
|
|
|
IdKeyCondition,
|
|
|
|
_.omit(queryRelated.where, IdKey),
|
|
|
|
],
|
|
|
|
};
|
|
|
|
queryRelated.where = mergedWhere;
|
2016-10-27 14:18:54 +00:00
|
|
|
} else {
|
|
|
|
queryRelated.where = {};
|
|
|
|
queryRelated.where[IdKey] = collectTargetIds(data, IdKey);
|
|
|
|
}
|
|
|
|
|
2017-08-01 18:15:21 +00:00
|
|
|
relatedModel.find(queryRelated, options, cb);
|
2016-10-27 14:18:54 +00:00
|
|
|
} else {
|
|
|
|
cb(err, data);
|
|
|
|
}
|
2014-06-16 17:50:42 +00:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
// Return from cache
|
|
|
|
cb(null, self.__cachedRelations[name]);
|
|
|
|
}
|
2015-05-16 17:11:17 +00:00
|
|
|
return cb.promise;
|
2016-04-01 11:48:17 +00:00
|
|
|
};
|
2014-06-16 17:50:42 +00:00
|
|
|
|
2014-08-13 09:28:23 +00:00
|
|
|
/**
|
|
|
|
* Define a scope method
|
|
|
|
* @param {String} name of the method
|
|
|
|
* @param {Function} function to define
|
|
|
|
*/
|
|
|
|
ScopeDefinition.prototype.defineMethod = function(name, fn) {
|
|
|
|
return this.methods[name] = fn;
|
2016-04-01 11:48:17 +00:00
|
|
|
};
|
2014-08-13 09:28:23 +00:00
|
|
|
|
2014-03-11 22:38:07 +00:00
|
|
|
/**
|
|
|
|
* Define a scope to the class
|
|
|
|
* @param {Model} cls The class where the scope method is added
|
|
|
|
* @param {Model} targetClass The class that a query to run against
|
|
|
|
* @param {String} name The name of the scope
|
|
|
|
* @param {Object|Function} params The parameters object for the query or a function
|
|
|
|
* to return the query object
|
|
|
|
* @param methods An object of methods keyed by the method name to be bound to the class
|
|
|
|
*/
|
2014-08-08 16:01:24 +00:00
|
|
|
function defineScope(cls, targetClass, name, params, methods, options) {
|
2014-01-24 17:09:53 +00:00
|
|
|
// collect meta info about scope
|
|
|
|
if (!cls._scopeMeta) {
|
|
|
|
cls._scopeMeta = {};
|
|
|
|
}
|
|
|
|
|
2014-06-16 17:50:42 +00:00
|
|
|
// only makes sense to add scope in meta if base and target classes
|
2014-01-24 17:09:53 +00:00
|
|
|
// are same
|
|
|
|
if (cls === targetClass) {
|
|
|
|
cls._scopeMeta[name] = params;
|
2014-08-30 18:58:06 +00:00
|
|
|
} else if (targetClass) {
|
2014-01-24 17:09:53 +00:00
|
|
|
if (!targetClass._scopeMeta) {
|
|
|
|
targetClass._scopeMeta = {};
|
2013-04-11 23:23:34 +00:00
|
|
|
}
|
2014-01-24 17:09:53 +00:00
|
|
|
}
|
|
|
|
|
2014-08-08 22:52:30 +00:00
|
|
|
options = options || {};
|
|
|
|
// Check if the cls is the class itself or its prototype
|
2018-12-07 14:54:29 +00:00
|
|
|
const isStatic = (typeof cls === 'function') || options.isStatic || false;
|
|
|
|
const definition = new ScopeDefinition({
|
2014-08-08 22:52:30 +00:00
|
|
|
isStatic: isStatic,
|
2014-08-08 16:01:24 +00:00
|
|
|
modelFrom: cls,
|
|
|
|
modelTo: targetClass,
|
2014-06-16 17:50:42 +00:00
|
|
|
name: name,
|
|
|
|
params: params,
|
2014-08-08 16:01:24 +00:00
|
|
|
methods: methods,
|
2016-04-01 11:48:17 +00:00
|
|
|
options: options,
|
2014-06-16 17:50:42 +00:00
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
if (isStatic) {
|
2014-08-08 22:52:30 +00:00
|
|
|
cls.scopes = cls.scopes || {};
|
|
|
|
cls.scopes[name] = definition;
|
|
|
|
} else {
|
|
|
|
cls.constructor.scopes = cls.constructor.scopes || {};
|
|
|
|
cls.constructor.scopes[name] = definition;
|
|
|
|
}
|
2014-08-08 16:01:24 +00:00
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
// Define a property for the scope
|
|
|
|
Object.defineProperty(cls, name, {
|
|
|
|
enumerable: false,
|
|
|
|
configurable: true,
|
|
|
|
/**
|
|
|
|
* This defines a property for the scope. For example, user.accounts or
|
|
|
|
* User.vips. Please note the cls can be the model class or prototype of
|
|
|
|
* the model class.
|
|
|
|
*
|
|
|
|
* The property value is function. It can be used to query the scope,
|
|
|
|
* such as user.accounts(condOrRefresh, cb) or User.vips(cb). The value
|
|
|
|
* can also have child properties for create/build/delete. For example,
|
|
|
|
* user.accounts.create(act, cb).
|
|
|
|
*
|
|
|
|
*/
|
2016-04-01 11:48:17 +00:00
|
|
|
get: function() {
|
2018-12-07 14:54:29 +00:00
|
|
|
const targetModel = definition.targetModel(this);
|
|
|
|
const self = this;
|
2015-02-18 05:04:31 +00:00
|
|
|
|
2018-12-07 15:22:36 +00:00
|
|
|
const f = function(condOrRefresh, options, cb) {
|
2014-09-04 19:23:24 +00:00
|
|
|
if (arguments.length === 0) {
|
2014-09-04 19:51:59 +00:00
|
|
|
if (typeof f.value === 'function') {
|
|
|
|
return f.value(self);
|
|
|
|
} else if (self.__cachedRelations) {
|
|
|
|
return self.__cachedRelations[name];
|
|
|
|
}
|
2014-01-24 17:09:53 +00:00
|
|
|
} else {
|
2017-08-01 18:15:21 +00:00
|
|
|
const condOrRefreshIsCallBack = typeof condOrRefresh === 'function' &&
|
|
|
|
options === undefined &&
|
|
|
|
cb === undefined;
|
|
|
|
if (condOrRefreshIsCallBack) {
|
2015-05-16 17:11:17 +00:00
|
|
|
// customer.orders(cb)
|
|
|
|
cb = condOrRefresh;
|
|
|
|
options = {};
|
|
|
|
condOrRefresh = undefined;
|
|
|
|
} else if (typeof options === 'function' && cb === undefined) {
|
|
|
|
// customer.orders(condOrRefresh, cb);
|
|
|
|
cb = options;
|
|
|
|
options = {};
|
|
|
|
}
|
2016-04-01 11:48:17 +00:00
|
|
|
options = options || {};
|
2015-02-28 18:53:18 +00:00
|
|
|
// Check if there is a through model
|
|
|
|
// see https://github.com/strongloop/loopback/issues/1076
|
|
|
|
if (f._scope.collect &&
|
|
|
|
condOrRefresh !== null && typeof condOrRefresh === 'object') {
|
|
|
|
f._scope.include = {
|
|
|
|
relation: f._scope.collect,
|
2016-04-01 11:48:17 +00:00
|
|
|
scope: condOrRefresh,
|
2015-02-28 18:53:18 +00:00
|
|
|
};
|
|
|
|
condOrRefresh = {};
|
|
|
|
}
|
2015-05-16 17:11:17 +00:00
|
|
|
return definition.related(self, f._scope, condOrRefresh, options, cb);
|
2013-04-11 23:23:34 +00:00
|
|
|
}
|
2014-01-24 17:09:53 +00:00
|
|
|
};
|
2015-02-18 05:04:31 +00:00
|
|
|
|
2014-08-30 18:58:06 +00:00
|
|
|
f._receiver = this;
|
2014-06-16 17:50:42 +00:00
|
|
|
f._scope = typeof definition.params === 'function' ?
|
|
|
|
definition.params.call(self) : definition.params;
|
2015-02-18 05:04:31 +00:00
|
|
|
|
2014-08-30 18:58:06 +00:00
|
|
|
f._targetClass = targetModel.modelName;
|
2014-04-02 17:32:37 +00:00
|
|
|
if (f._scope.collect) {
|
2017-03-22 21:06:36 +00:00
|
|
|
const rel = targetModel.relations[f._scope.collect];
|
|
|
|
f._targetClass = rel && rel.modelTo && rel.modelTo.modelName || i8n.camelize(f._scope.collect);
|
2014-04-02 17:32:37 +00:00
|
|
|
}
|
2015-02-18 05:04:31 +00:00
|
|
|
|
2017-07-19 13:31:21 +00:00
|
|
|
f.find = function(condOrRefresh, options, cb) {
|
2016-04-01 13:23:42 +00:00
|
|
|
if (typeof condOrRefresh === 'function' &&
|
|
|
|
options === undefined && cb === undefined) {
|
2015-05-16 17:11:17 +00:00
|
|
|
cb = condOrRefresh;
|
|
|
|
options = {};
|
|
|
|
condOrRefresh = {};
|
|
|
|
} else if (typeof options === 'function' && cb === undefined) {
|
|
|
|
cb = options;
|
|
|
|
options = {};
|
2015-02-18 05:04:31 +00:00
|
|
|
}
|
2016-04-01 11:48:17 +00:00
|
|
|
options = options || {};
|
2015-05-16 17:11:17 +00:00
|
|
|
return definition.related(self, f._scope, condOrRefresh, options, cb);
|
2016-04-01 11:48:17 +00:00
|
|
|
};
|
2015-02-18 05:04:31 +00:00
|
|
|
|
2017-07-19 13:31:21 +00:00
|
|
|
f.getAsync = function() {
|
|
|
|
deprecated(g.f('Scope method "getAsync()" is deprecated, use "find()" instead.'));
|
|
|
|
return this.find.apply(this, arguments);
|
|
|
|
};
|
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
f.build = build;
|
|
|
|
f.create = create;
|
2015-03-28 20:25:24 +00:00
|
|
|
f.updateAll = updateAll;
|
2014-01-24 17:09:53 +00:00
|
|
|
f.destroyAll = destroyAll;
|
2015-03-20 15:37:46 +00:00
|
|
|
f.findById = findById;
|
2015-03-21 12:44:06 +00:00
|
|
|
f.findOne = findOne;
|
2014-08-12 12:44:33 +00:00
|
|
|
f.count = count;
|
2015-03-24 14:35:56 +00:00
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
for (const i in definition.methods) {
|
2014-06-16 17:50:42 +00:00
|
|
|
f[i] = definition.methods[i].bind(self);
|
2014-01-24 17:09:53 +00:00
|
|
|
}
|
2015-03-24 14:35:56 +00:00
|
|
|
|
2014-08-30 18:58:06 +00:00
|
|
|
if (!targetClass) return f;
|
2014-01-24 17:09:53 +00:00
|
|
|
|
2014-06-16 17:50:42 +00:00
|
|
|
// Define scope-chaining, such as
|
|
|
|
// Station.scope('active', {where: {isActive: true}});
|
|
|
|
// Station.scope('subway', {where: {isUndeground: true}});
|
|
|
|
// Station.active.subway(cb);
|
2016-04-01 11:48:17 +00:00
|
|
|
Object.keys(targetClass._scopeMeta).forEach(function(name) {
|
2014-01-24 17:09:53 +00:00
|
|
|
Object.defineProperty(f, name, {
|
|
|
|
enumerable: false,
|
2016-04-01 11:48:17 +00:00
|
|
|
get: function() {
|
2014-08-30 18:58:06 +00:00
|
|
|
mergeQuery(f._scope, targetModel._scopeMeta[name]);
|
2014-01-24 17:09:53 +00:00
|
|
|
return f;
|
2016-04-01 11:48:17 +00:00
|
|
|
},
|
2014-01-24 17:09:53 +00:00
|
|
|
});
|
2014-06-16 17:50:42 +00:00
|
|
|
}.bind(self));
|
2014-01-24 17:09:53 +00:00
|
|
|
return f;
|
2016-04-01 11:48:17 +00:00
|
|
|
},
|
2014-01-24 17:09:53 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// Wrap the property into a function for remoting
|
2018-12-07 14:54:29 +00:00
|
|
|
const fn = function() {
|
2014-01-24 17:09:53 +00:00
|
|
|
// primaryObject.scopeName, such as user.accounts
|
2018-12-07 14:54:29 +00:00
|
|
|
const f = this[name];
|
2014-01-24 17:09:53 +00:00
|
|
|
// set receiver to be the scope property whose value is a function
|
|
|
|
f.apply(this[name], arguments);
|
|
|
|
};
|
|
|
|
|
|
|
|
cls['__get__' + name] = fn;
|
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
const fnCreate = function() {
|
|
|
|
const f = this[name].create;
|
2014-01-24 17:09:53 +00:00
|
|
|
f.apply(this[name], arguments);
|
|
|
|
};
|
|
|
|
|
2016-04-01 13:23:42 +00:00
|
|
|
cls['__create__' + name] = fnCreate;
|
2014-01-24 17:09:53 +00:00
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
const fnDelete = function() {
|
|
|
|
const f = this[name].destroyAll;
|
2014-01-24 17:09:53 +00:00
|
|
|
f.apply(this[name], arguments);
|
|
|
|
};
|
|
|
|
|
2016-04-01 13:23:42 +00:00
|
|
|
cls['__delete__' + name] = fnDelete;
|
2014-01-24 17:09:53 +00:00
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
const fnUpdate = function() {
|
|
|
|
const f = this[name].updateAll;
|
2015-03-28 20:25:24 +00:00
|
|
|
f.apply(this[name], arguments);
|
|
|
|
};
|
|
|
|
|
2016-04-01 13:23:42 +00:00
|
|
|
cls['__update__' + name] = fnUpdate;
|
2015-03-28 20:25:24 +00:00
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
const fnFindById = function(cb) {
|
|
|
|
const f = this[name].findById;
|
2015-03-20 15:37:46 +00:00
|
|
|
f.apply(this[name], arguments);
|
|
|
|
};
|
|
|
|
|
2016-04-01 13:23:42 +00:00
|
|
|
cls['__findById__' + name] = fnFindById;
|
2015-03-20 15:37:46 +00:00
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
const fnFindOne = function(cb) {
|
|
|
|
const f = this[name].findOne;
|
2015-03-21 12:44:06 +00:00
|
|
|
f.apply(this[name], arguments);
|
|
|
|
};
|
|
|
|
|
2016-04-01 13:23:42 +00:00
|
|
|
cls['__findOne__' + name] = fnFindOne;
|
2015-02-18 05:04:31 +00:00
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
const fnCount = function(cb) {
|
|
|
|
const f = this[name].count;
|
2014-08-12 12:44:33 +00:00
|
|
|
f.apply(this[name], arguments);
|
|
|
|
};
|
|
|
|
|
2016-04-01 13:23:42 +00:00
|
|
|
cls['__count__' + name] = fnCount;
|
2014-08-12 12:44:33 +00:00
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
// and it should have create/build methods with binded thisModelNameId param
|
|
|
|
function build(data) {
|
2014-06-16 17:50:42 +00:00
|
|
|
data = data || {};
|
2014-06-19 19:00:49 +00:00
|
|
|
// Find all fixed property values for the scope
|
2018-12-07 14:54:29 +00:00
|
|
|
const targetModel = definition.targetModel(this._receiver);
|
|
|
|
const where = (this._scope && this._scope.where) || {};
|
2014-08-30 18:58:06 +00:00
|
|
|
setScopeValuesFromWhere(data, where, targetModel);
|
|
|
|
return new targetModel(data);
|
2014-01-24 17:09:53 +00:00
|
|
|
}
|
|
|
|
|
2015-05-16 17:11:17 +00:00
|
|
|
function create(data, options, cb) {
|
|
|
|
if (typeof data === 'function' &&
|
|
|
|
options === undefined && cb === undefined) {
|
|
|
|
// create(cb)
|
2014-01-24 17:09:53 +00:00
|
|
|
cb = data;
|
|
|
|
data = {};
|
2015-05-16 17:11:17 +00:00
|
|
|
} else if (typeof options === 'function' && cb === undefined) {
|
|
|
|
// create(data, cb)
|
|
|
|
cb = options;
|
|
|
|
options = {};
|
2014-01-24 17:09:53 +00:00
|
|
|
}
|
2015-05-16 17:11:17 +00:00
|
|
|
options = options || {};
|
|
|
|
return this.build(data).save(options, cb);
|
2014-01-24 17:09:53 +00:00
|
|
|
}
|
2015-02-18 05:04:31 +00:00
|
|
|
|
2014-01-24 17:09:53 +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
|
|
|
|
*/
|
2015-05-16 17:11:17 +00:00
|
|
|
function destroyAll(where, options, cb) {
|
|
|
|
if (typeof where === 'function') {
|
|
|
|
// destroyAll(cb)
|
|
|
|
cb = where;
|
|
|
|
where = {};
|
|
|
|
} else if (typeof options === 'function' && cb === undefined) {
|
|
|
|
// destroyAll(where, cb)
|
|
|
|
cb = options;
|
|
|
|
options = {};
|
|
|
|
}
|
|
|
|
options = options || {};
|
2015-02-18 05:04:31 +00:00
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
const targetModel = definition.targetModel(this._receiver);
|
|
|
|
const scoped = (this._scope && this._scope.where) || {};
|
|
|
|
const filter = mergeQuery({where: scoped}, {where: where || {}});
|
2015-05-16 17:11:17 +00:00
|
|
|
return targetModel.destroyAll(filter.where, options, cb);
|
2014-01-24 17:09:53 +00:00
|
|
|
}
|
2014-08-12 12:44:33 +00:00
|
|
|
|
2015-05-16 17:11:17 +00:00
|
|
|
function updateAll(where, data, options, cb) {
|
|
|
|
if (typeof data === 'function' &&
|
|
|
|
options === undefined && cb === undefined) {
|
|
|
|
// updateAll(data, cb)
|
2015-03-28 20:25:24 +00:00
|
|
|
cb = data;
|
|
|
|
data = where;
|
|
|
|
where = {};
|
2015-05-16 17:11:17 +00:00
|
|
|
options = {};
|
|
|
|
} else if (typeof options === 'function' && cb === undefined) {
|
|
|
|
// updateAll(where, data, cb)
|
|
|
|
cb = options;
|
|
|
|
options = {};
|
2015-03-28 20:25:24 +00:00
|
|
|
}
|
2015-05-16 17:11:17 +00:00
|
|
|
options = options || {};
|
2018-12-07 14:54:29 +00:00
|
|
|
const targetModel = definition.targetModel(this._receiver);
|
|
|
|
const scoped = (this._scope && this._scope.where) || {};
|
|
|
|
const filter = mergeQuery({where: scoped}, {where: where || {}});
|
2015-05-16 17:11:17 +00:00
|
|
|
return targetModel.updateAll(filter.where, data, options, cb);
|
2015-03-28 20:25:24 +00:00
|
|
|
}
|
|
|
|
|
2015-05-16 17:11:17 +00:00
|
|
|
function findById(id, filter, options, cb) {
|
|
|
|
if (options === undefined && cb === undefined) {
|
|
|
|
if (typeof filter === 'function') {
|
|
|
|
// findById(id, cb)
|
|
|
|
cb = filter;
|
|
|
|
filter = {};
|
|
|
|
}
|
|
|
|
} else if (cb === undefined) {
|
|
|
|
if (typeof options === 'function') {
|
|
|
|
// findById(id, query, cb)
|
|
|
|
cb = options;
|
|
|
|
options = {};
|
|
|
|
if (typeof filter === 'object' && !(filter.include || filter.fields)) {
|
|
|
|
// If filter doesn't have include or fields, assuming it's options
|
|
|
|
options = filter;
|
|
|
|
filter = {};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-12-14 22:16:20 +00:00
|
|
|
|
2015-05-16 17:11:17 +00:00
|
|
|
options = options || {};
|
|
|
|
filter = filter || {};
|
2018-12-07 14:54:29 +00:00
|
|
|
const targetModel = definition.targetModel(this._receiver);
|
|
|
|
const idName = targetModel.definition.idName();
|
|
|
|
let query = {where: {}};
|
2015-05-16 17:11:17 +00:00
|
|
|
query.where[idName] = id;
|
|
|
|
query = mergeQuery(query, filter);
|
|
|
|
return this.findOne(query, options, cb);
|
2015-03-21 12:44:06 +00:00
|
|
|
}
|
|
|
|
|
2015-05-16 17:11:17 +00:00
|
|
|
function findOne(filter, options, cb) {
|
|
|
|
if (typeof filter === 'function') {
|
|
|
|
// findOne(cb)
|
|
|
|
cb = filter;
|
|
|
|
filter = {};
|
|
|
|
options = {};
|
|
|
|
} else if (typeof options === 'function' && cb === undefined) {
|
|
|
|
// findOne(filter, cb)
|
|
|
|
cb = options;
|
|
|
|
options = {};
|
|
|
|
}
|
|
|
|
options = options || {};
|
2018-12-07 14:54:29 +00:00
|
|
|
const targetModel = definition.targetModel(this._receiver);
|
|
|
|
const scoped = (this._scope && this._scope.where) || {};
|
2016-08-19 17:46:59 +00:00
|
|
|
filter = mergeQuery({where: scoped}, filter || {});
|
2015-05-16 17:11:17 +00:00
|
|
|
return targetModel.findOne(filter, options, cb);
|
2015-03-20 15:37:46 +00:00
|
|
|
}
|
|
|
|
|
2015-05-16 17:11:17 +00:00
|
|
|
function count(where, options, cb) {
|
|
|
|
if (typeof where === 'function') {
|
|
|
|
// count(cb)
|
|
|
|
cb = where;
|
|
|
|
where = {};
|
|
|
|
} else if (typeof options === 'function' && cb === undefined) {
|
|
|
|
// count(where, cb)
|
|
|
|
cb = options;
|
|
|
|
options = {};
|
|
|
|
}
|
|
|
|
options = options || {};
|
2015-02-18 05:04:31 +00:00
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
const targetModel = definition.targetModel(this._receiver);
|
|
|
|
const scoped = (this._scope && this._scope.where) || {};
|
|
|
|
const filter = mergeQuery({where: scoped}, {where: where || {}});
|
2015-05-16 17:11:17 +00:00
|
|
|
return targetModel.count(filter.where, options, cb);
|
2015-02-18 05:04:31 +00:00
|
|
|
}
|
2015-03-20 15:37:46 +00:00
|
|
|
|
2014-07-27 14:30:45 +00:00
|
|
|
return definition;
|
2014-06-16 17:50:42 +00:00
|
|
|
}
|