1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | 'use strict';
|
7 |
|
8 | const _ = require('lodash');
|
9 | const i8n = require('inflection');
|
10 | const g = require('strong-globalize')();
|
11 | const utils = require('./utils');
|
12 | const defineCachedRelations = utils.defineCachedRelations;
|
13 | const setScopeValuesFromWhere = utils.setScopeValuesFromWhere;
|
14 | const mergeQuery = utils.mergeQuery;
|
15 | const DefaultModelBaseClass = require('./model.js');
|
16 | const collectTargetIds = utils.collectTargetIds;
|
17 | const idName = utils.idName;
|
18 | const deprecated = require('depd')('loopback-datasource-juggler');
|
19 |
|
20 |
|
21 |
|
22 |
|
23 | exports.defineScope = defineScope;
|
24 |
|
25 | function ScopeDefinition(definition) {
|
26 | this.isStatic = definition.isStatic;
|
27 | this.modelFrom = definition.modelFrom;
|
28 | this.modelTo = definition.modelTo || definition.modelFrom;
|
29 | this.name = definition.name;
|
30 | this.params = definition.params;
|
31 | this.methods = definition.methods || {};
|
32 | this.options = definition.options || {};
|
33 | }
|
34 |
|
35 | ScopeDefinition.prototype.targetModel = function(receiver) {
|
36 | let modelTo;
|
37 | if (typeof this.options.modelTo === 'function') {
|
38 | modelTo = this.options.modelTo.call(this, receiver) || this.modelTo;
|
39 | } else {
|
40 | modelTo = this.modelTo;
|
41 | }
|
42 | if (!(modelTo.prototype instanceof DefaultModelBaseClass)) {
|
43 | let msg = 'Invalid target model for scope `';
|
44 | msg += (this.isStatic ? this.modelFrom : this.modelFrom.constructor).modelName;
|
45 | msg += this.isStatic ? '.' : '.prototype.';
|
46 | msg += this.name + '`.';
|
47 | throw new Error(msg);
|
48 | }
|
49 | return modelTo;
|
50 | };
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 | ScopeDefinition.prototype.related = function(receiver, scopeParams, condOrRefresh, options, cb) {
|
62 | const name = this.name;
|
63 | const self = receiver;
|
64 |
|
65 | let actualCond = {};
|
66 | let actualRefresh = false;
|
67 | let saveOnCache = receiver instanceof DefaultModelBaseClass;
|
68 | if (typeof condOrRefresh === 'function' &&
|
69 | options === undefined && cb === undefined) {
|
70 |
|
71 | cb = condOrRefresh;
|
72 | options = {};
|
73 | condOrRefresh = undefined;
|
74 | } else if (typeof options === 'function' && cb === undefined) {
|
75 | cb = options;
|
76 | options = {};
|
77 | }
|
78 | options = options || {};
|
79 | if (condOrRefresh !== undefined) {
|
80 | if (typeof condOrRefresh === 'boolean') {
|
81 | actualRefresh = condOrRefresh;
|
82 | } else {
|
83 | actualCond = condOrRefresh;
|
84 | actualRefresh = true;
|
85 | saveOnCache = false;
|
86 | }
|
87 | }
|
88 | cb = cb || utils.createPromiseCallback();
|
89 | const refreshIsNeeded = !self.__cachedRelations ||
|
90 | self.__cachedRelations[name] === undefined ||
|
91 | actualRefresh;
|
92 | if (refreshIsNeeded) {
|
93 |
|
94 | const params = mergeQuery(actualCond, scopeParams, {nestedInclude: true});
|
95 | const targetModel = this.targetModel(receiver);
|
96 |
|
97 |
|
98 |
|
99 |
|
100 | const scopeOnRelatedModel = params.collect &&
|
101 | params.include.scope !== null &&
|
102 | typeof params.include.scope === 'object';
|
103 | let filter, queryRelated;
|
104 | if (scopeOnRelatedModel) {
|
105 | filter = params.include;
|
106 |
|
107 | queryRelated = filter.scope;
|
108 | delete params.include.scope;
|
109 | }
|
110 |
|
111 | targetModel.find(params, options, function(err, data) {
|
112 | if (!err && saveOnCache) {
|
113 | defineCachedRelations(self);
|
114 | self.__cachedRelations[name] = data;
|
115 | }
|
116 |
|
117 | if (scopeOnRelatedModel === true) {
|
118 | const relatedModel = targetModel.relations[filter.relation].modelTo;
|
119 | const IdKey = idName(relatedModel);
|
120 |
|
121 |
|
122 | const smartMerge = function(idCollection, qWhere) {
|
123 | if (!qWhere[IdKey]) return idCollection;
|
124 | let merged = {};
|
125 |
|
126 | const idsA = idCollection.inq;
|
127 | const idsB = qWhere[IdKey].inq ? qWhere[IdKey].inq : [qWhere[IdKey]];
|
128 |
|
129 | const intersect = _.intersectionWith(idsA, idsB, _.isEqual);
|
130 | if (intersect.length === 1) merged = intersect[0];
|
131 | if (intersect.length > 1) merged = {inq: intersect};
|
132 |
|
133 | return merged;
|
134 | };
|
135 |
|
136 | if (queryRelated.where !== undefined) {
|
137 |
|
138 | const IdKeyCondition = {};
|
139 | IdKeyCondition[IdKey] = smartMerge(collectTargetIds(data, IdKey),
|
140 | queryRelated.where);
|
141 |
|
142 |
|
143 |
|
144 | if (_.isObject(IdKeyCondition[IdKey]) && _.isEmpty(IdKeyCondition[IdKey])) return cb(null, []);
|
145 |
|
146 | const mergedWhere = {
|
147 | and: [
|
148 | IdKeyCondition,
|
149 | _.omit(queryRelated.where, IdKey),
|
150 | ],
|
151 | };
|
152 | queryRelated.where = mergedWhere;
|
153 | } else {
|
154 | queryRelated.where = {};
|
155 | queryRelated.where[IdKey] = collectTargetIds(data, IdKey);
|
156 | }
|
157 |
|
158 | relatedModel.find(queryRelated, options, cb);
|
159 | } else {
|
160 | cb(err, data);
|
161 | }
|
162 | });
|
163 | } else {
|
164 |
|
165 | cb(null, self.__cachedRelations[name]);
|
166 | }
|
167 | return cb.promise;
|
168 | };
|
169 |
|
170 |
|
171 |
|
172 |
|
173 |
|
174 |
|
175 | ScopeDefinition.prototype.defineMethod = function(name, fn) {
|
176 | return this.methods[name] = fn;
|
177 | };
|
178 |
|
179 |
|
180 |
|
181 |
|
182 |
|
183 |
|
184 |
|
185 |
|
186 |
|
187 |
|
188 | function defineScope(cls, targetClass, name, params, methods, options) {
|
189 |
|
190 | if (!cls._scopeMeta) {
|
191 | cls._scopeMeta = {};
|
192 | }
|
193 |
|
194 |
|
195 |
|
196 | if (cls === targetClass) {
|
197 | cls._scopeMeta[name] = params;
|
198 | } else if (targetClass) {
|
199 | if (!targetClass._scopeMeta) {
|
200 | targetClass._scopeMeta = {};
|
201 | }
|
202 | }
|
203 |
|
204 | options = options || {};
|
205 |
|
206 | const isStatic = (typeof cls === 'function') || options.isStatic || false;
|
207 | const definition = new ScopeDefinition({
|
208 | isStatic: isStatic,
|
209 | modelFrom: cls,
|
210 | modelTo: targetClass,
|
211 | name: name,
|
212 | params: params,
|
213 | methods: methods,
|
214 | options: options,
|
215 | });
|
216 |
|
217 | if (isStatic) {
|
218 | cls.scopes = cls.scopes || {};
|
219 | cls.scopes[name] = definition;
|
220 | } else {
|
221 | cls.constructor.scopes = cls.constructor.scopes || {};
|
222 | cls.constructor.scopes[name] = definition;
|
223 | }
|
224 |
|
225 |
|
226 | Object.defineProperty(cls, name, {
|
227 | enumerable: false,
|
228 | configurable: true,
|
229 | |
230 |
|
231 |
|
232 |
|
233 |
|
234 |
|
235 |
|
236 |
|
237 |
|
238 |
|
239 |
|
240 | get: function() {
|
241 | const targetModel = definition.targetModel(this);
|
242 | const self = this;
|
243 |
|
244 | const f = function(condOrRefresh, options, cb) {
|
245 | if (arguments.length === 0) {
|
246 | if (typeof f.value === 'function') {
|
247 | return f.value(self);
|
248 | } else if (self.__cachedRelations) {
|
249 | return self.__cachedRelations[name];
|
250 | }
|
251 | } else {
|
252 | const condOrRefreshIsCallBack = typeof condOrRefresh === 'function' &&
|
253 | options === undefined &&
|
254 | cb === undefined;
|
255 | if (condOrRefreshIsCallBack) {
|
256 |
|
257 | cb = condOrRefresh;
|
258 | options = {};
|
259 | condOrRefresh = undefined;
|
260 | } else if (typeof options === 'function' && cb === undefined) {
|
261 |
|
262 | cb = options;
|
263 | options = {};
|
264 | }
|
265 | options = options || {};
|
266 |
|
267 |
|
268 | if (f._scope.collect &&
|
269 | condOrRefresh !== null && typeof condOrRefresh === 'object') {
|
270 | f._scope.include = {
|
271 | relation: f._scope.collect,
|
272 | scope: condOrRefresh,
|
273 | };
|
274 | condOrRefresh = {};
|
275 | }
|
276 | return definition.related(self, f._scope, condOrRefresh, options, cb);
|
277 | }
|
278 | };
|
279 |
|
280 | f._receiver = this;
|
281 | f._scope = typeof definition.params === 'function' ?
|
282 | definition.params.call(self) : definition.params;
|
283 |
|
284 | f._targetClass = targetModel.modelName;
|
285 | if (f._scope.collect) {
|
286 | const rel = targetModel.relations[f._scope.collect];
|
287 | f._targetClass = rel && rel.modelTo && rel.modelTo.modelName || i8n.camelize(f._scope.collect);
|
288 | }
|
289 |
|
290 | f.find = function(condOrRefresh, options, cb) {
|
291 | if (typeof condOrRefresh === 'function' &&
|
292 | options === undefined && cb === undefined) {
|
293 | cb = condOrRefresh;
|
294 | options = {};
|
295 | condOrRefresh = {};
|
296 | } else if (typeof options === 'function' && cb === undefined) {
|
297 | cb = options;
|
298 | options = {};
|
299 | }
|
300 | options = options || {};
|
301 | return definition.related(self, f._scope, condOrRefresh, options, cb);
|
302 | };
|
303 |
|
304 | f.getAsync = function() {
|
305 | deprecated(g.f('Scope method "getAsync()" is deprecated, use "find()" instead.'));
|
306 | return this.find.apply(this, arguments);
|
307 | };
|
308 |
|
309 | f.build = build;
|
310 | f.create = create;
|
311 | f.updateAll = updateAll;
|
312 | f.destroyAll = destroyAll;
|
313 | f.findById = findById;
|
314 | f.findOne = findOne;
|
315 | f.count = count;
|
316 |
|
317 | for (const i in definition.methods) {
|
318 | f[i] = definition.methods[i].bind(self);
|
319 | }
|
320 |
|
321 | if (!targetClass) return f;
|
322 |
|
323 |
|
324 |
|
325 |
|
326 |
|
327 | Object.keys(targetClass._scopeMeta).forEach(function(name) {
|
328 | Object.defineProperty(f, name, {
|
329 | enumerable: false,
|
330 | get: function() {
|
331 | mergeQuery(f._scope, targetModel._scopeMeta[name]);
|
332 | return f;
|
333 | },
|
334 | });
|
335 | }.bind(self));
|
336 | return f;
|
337 | },
|
338 | });
|
339 |
|
340 |
|
341 | const fn = function() {
|
342 |
|
343 | const f = this[name];
|
344 |
|
345 | f.apply(this[name], arguments);
|
346 | };
|
347 |
|
348 | cls['__get__' + name] = fn;
|
349 |
|
350 | const fnCreate = function() {
|
351 | const f = this[name].create;
|
352 | f.apply(this[name], arguments);
|
353 | };
|
354 |
|
355 | cls['__create__' + name] = fnCreate;
|
356 |
|
357 | const fnDelete = function() {
|
358 | const f = this[name].destroyAll;
|
359 | f.apply(this[name], arguments);
|
360 | };
|
361 |
|
362 | cls['__delete__' + name] = fnDelete;
|
363 |
|
364 | const fnUpdate = function() {
|
365 | const f = this[name].updateAll;
|
366 | f.apply(this[name], arguments);
|
367 | };
|
368 |
|
369 | cls['__update__' + name] = fnUpdate;
|
370 |
|
371 | const fnFindById = function(cb) {
|
372 | const f = this[name].findById;
|
373 | f.apply(this[name], arguments);
|
374 | };
|
375 |
|
376 | cls['__findById__' + name] = fnFindById;
|
377 |
|
378 | const fnFindOne = function(cb) {
|
379 | const f = this[name].findOne;
|
380 | f.apply(this[name], arguments);
|
381 | };
|
382 |
|
383 | cls['__findOne__' + name] = fnFindOne;
|
384 |
|
385 | const fnCount = function(cb) {
|
386 | const f = this[name].count;
|
387 | f.apply(this[name], arguments);
|
388 | };
|
389 |
|
390 | cls['__count__' + name] = fnCount;
|
391 |
|
392 |
|
393 | function build(data) {
|
394 | data = data || {};
|
395 |
|
396 | const targetModel = definition.targetModel(this._receiver);
|
397 | const where = (this._scope && this._scope.where) || {};
|
398 | setScopeValuesFromWhere(data, where, targetModel);
|
399 | return new targetModel(data);
|
400 | }
|
401 |
|
402 | function create(data, options, cb) {
|
403 | if (typeof data === 'function' &&
|
404 | options === undefined && cb === undefined) {
|
405 |
|
406 | cb = data;
|
407 | data = {};
|
408 | } else if (typeof options === 'function' && cb === undefined) {
|
409 |
|
410 | cb = options;
|
411 | options = {};
|
412 | }
|
413 | options = options || {};
|
414 | return this.build(data).save(options, cb);
|
415 | }
|
416 |
|
417 | |
418 |
|
419 |
|
420 |
|
421 |
|
422 |
|
423 | function destroyAll(where, options, cb) {
|
424 | if (typeof where === 'function') {
|
425 |
|
426 | cb = where;
|
427 | where = {};
|
428 | } else if (typeof options === 'function' && cb === undefined) {
|
429 |
|
430 | cb = options;
|
431 | options = {};
|
432 | }
|
433 | options = options || {};
|
434 |
|
435 | const targetModel = definition.targetModel(this._receiver);
|
436 | const scoped = (this._scope && this._scope.where) || {};
|
437 | const filter = mergeQuery({where: scoped}, {where: where || {}});
|
438 | return targetModel.destroyAll(filter.where, options, cb);
|
439 | }
|
440 |
|
441 | function updateAll(where, data, options, cb) {
|
442 | if (typeof data === 'function' &&
|
443 | options === undefined && cb === undefined) {
|
444 |
|
445 | cb = data;
|
446 | data = where;
|
447 | where = {};
|
448 | options = {};
|
449 | } else if (typeof options === 'function' && cb === undefined) {
|
450 |
|
451 | cb = options;
|
452 | options = {};
|
453 | }
|
454 | options = options || {};
|
455 | const targetModel = definition.targetModel(this._receiver);
|
456 | const scoped = (this._scope && this._scope.where) || {};
|
457 | const filter = mergeQuery({where: scoped}, {where: where || {}});
|
458 | return targetModel.updateAll(filter.where, data, options, cb);
|
459 | }
|
460 |
|
461 | function findById(id, filter, options, cb) {
|
462 | if (options === undefined && cb === undefined) {
|
463 | if (typeof filter === 'function') {
|
464 |
|
465 | cb = filter;
|
466 | filter = {};
|
467 | }
|
468 | } else if (cb === undefined) {
|
469 | if (typeof options === 'function') {
|
470 |
|
471 | cb = options;
|
472 | options = {};
|
473 | if (typeof filter === 'object' && !(filter.include || filter.fields)) {
|
474 |
|
475 | options = filter;
|
476 | filter = {};
|
477 | }
|
478 | }
|
479 | }
|
480 |
|
481 | options = options || {};
|
482 | filter = filter || {};
|
483 | const targetModel = definition.targetModel(this._receiver);
|
484 | const idName = targetModel.definition.idName();
|
485 | let query = {where: {}};
|
486 | query.where[idName] = id;
|
487 | query = mergeQuery(query, filter);
|
488 | return this.findOne(query, options, cb);
|
489 | }
|
490 |
|
491 | function findOne(filter, options, cb) {
|
492 | if (typeof filter === 'function') {
|
493 |
|
494 | cb = filter;
|
495 | filter = {};
|
496 | options = {};
|
497 | } else if (typeof options === 'function' && cb === undefined) {
|
498 |
|
499 | cb = options;
|
500 | options = {};
|
501 | }
|
502 | options = options || {};
|
503 | const targetModel = definition.targetModel(this._receiver);
|
504 | const scoped = (this._scope && this._scope.where) || {};
|
505 | filter = mergeQuery({where: scoped}, filter || {});
|
506 | return targetModel.findOne(filter, options, cb);
|
507 | }
|
508 |
|
509 | function count(where, options, cb) {
|
510 | if (typeof where === 'function') {
|
511 |
|
512 | cb = where;
|
513 | where = {};
|
514 | } else if (typeof options === 'function' && cb === undefined) {
|
515 |
|
516 | cb = options;
|
517 | options = {};
|
518 | }
|
519 | options = options || {};
|
520 |
|
521 | const targetModel = definition.targetModel(this._receiver);
|
522 | const scoped = (this._scope && this._scope.where) || {};
|
523 | const filter = mergeQuery({where: scoped}, {where: where || {}});
|
524 | return targetModel.count(filter.where, options, cb);
|
525 | }
|
526 |
|
527 | return definition;
|
528 | }
|