2020-01-21 18:12:14 +00:00
|
|
|
// Copyright IBM Corp. 2014,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
|
|
|
|
|
2017-01-31 15:14:53 +00:00
|
|
|
'use strict';
|
2016-08-26 13:49:34 +00:00
|
|
|
|
2014-06-15 22:53:58 +00:00
|
|
|
/*!
|
|
|
|
* Dependencies
|
|
|
|
*/
|
2018-12-07 14:54:29 +00:00
|
|
|
const assert = require('assert');
|
|
|
|
const util = require('util');
|
|
|
|
const async = require('async');
|
|
|
|
const utils = require('./utils');
|
|
|
|
const i8n = require('inflection');
|
|
|
|
const defineScope = require('./scope.js').defineScope;
|
|
|
|
const g = require('strong-globalize')();
|
|
|
|
const mergeQuery = utils.mergeQuery;
|
|
|
|
const idEquals = utils.idEquals;
|
|
|
|
const idsHaveDuplicates = utils.idsHaveDuplicates;
|
|
|
|
const ModelBaseClass = require('./model.js');
|
|
|
|
const applyFilter = require('./connectors/memory').applyFilter;
|
|
|
|
const ValidationError = require('./validations.js').ValidationError;
|
|
|
|
const deprecated = require('depd')('loopback-datasource-juggler');
|
|
|
|
const debug = require('debug')('loopback:relations');
|
|
|
|
|
|
|
|
const RelationTypes = {
|
2014-06-15 22:53:58 +00:00
|
|
|
belongsTo: 'belongsTo',
|
|
|
|
hasMany: 'hasMany',
|
|
|
|
hasOne: 'hasOne',
|
2014-07-27 14:30:45 +00:00
|
|
|
hasAndBelongsToMany: 'hasAndBelongsToMany',
|
2014-07-29 08:51:33 +00:00
|
|
|
referencesMany: 'referencesMany',
|
2014-08-19 20:10:35 +00:00
|
|
|
embedsOne: 'embedsOne',
|
2016-04-01 11:48:17 +00:00
|
|
|
embedsMany: 'embedsMany',
|
2014-06-15 22:53:58 +00:00
|
|
|
};
|
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
const RelationClasses = {
|
2014-06-15 22:53:58 +00:00
|
|
|
belongsTo: BelongsTo,
|
|
|
|
hasMany: HasMany,
|
|
|
|
hasManyThrough: HasManyThrough,
|
|
|
|
hasOne: HasOne,
|
2014-07-27 14:30:45 +00:00
|
|
|
hasAndBelongsToMany: HasAndBelongsToMany,
|
2014-07-29 08:51:33 +00:00
|
|
|
referencesMany: ReferencesMany,
|
2014-08-19 20:10:35 +00:00
|
|
|
embedsOne: EmbedsOne,
|
2016-04-01 11:48:17 +00:00
|
|
|
embedsMany: EmbedsMany,
|
2014-06-15 22:53:58 +00:00
|
|
|
};
|
|
|
|
|
2014-09-04 19:04:55 +00:00
|
|
|
exports.Relation = Relation;
|
|
|
|
exports.RelationDefinition = RelationDefinition;
|
|
|
|
|
|
|
|
exports.RelationTypes = RelationTypes;
|
|
|
|
exports.RelationClasses = RelationClasses;
|
|
|
|
|
|
|
|
exports.HasMany = HasMany;
|
|
|
|
exports.HasManyThrough = HasManyThrough;
|
|
|
|
exports.HasOne = HasOne;
|
|
|
|
exports.HasAndBelongsToMany = HasAndBelongsToMany;
|
|
|
|
exports.BelongsTo = BelongsTo;
|
|
|
|
exports.ReferencesMany = ReferencesMany;
|
|
|
|
exports.EmbedsOne = EmbedsOne;
|
|
|
|
exports.EmbedsMany = EmbedsMany;
|
|
|
|
|
2014-06-15 22:53:58 +00:00
|
|
|
function normalizeType(type) {
|
|
|
|
if (!type) {
|
|
|
|
return type;
|
|
|
|
}
|
2018-12-07 14:54:29 +00:00
|
|
|
const t1 = type.toLowerCase();
|
|
|
|
for (const t2 in RelationTypes) {
|
2014-06-15 22:53:58 +00:00
|
|
|
if (t2.toLowerCase() === t1) {
|
|
|
|
return t2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
2018-12-07 14:43:40 +00:00
|
|
|
}
|
2014-07-29 20:59:44 +00:00
|
|
|
|
|
|
|
function extendScopeMethods(definition, scopeMethods, ext) {
|
2018-12-07 14:54:29 +00:00
|
|
|
let customMethods = [];
|
|
|
|
let relationClass = RelationClasses[definition.type];
|
2014-07-29 20:59:44 +00:00
|
|
|
if (definition.type === RelationTypes.hasMany && definition.modelThrough) {
|
|
|
|
relationClass = RelationClasses.hasManyThrough;
|
|
|
|
}
|
|
|
|
if (typeof ext === 'function') {
|
2014-07-30 14:46:05 +00:00
|
|
|
customMethods = ext.call(definition, scopeMethods, relationClass);
|
2014-07-29 20:59:44 +00:00
|
|
|
} else if (typeof ext === 'object') {
|
2014-08-13 09:28:23 +00:00
|
|
|
function createFunc(definition, relationMethod) {
|
|
|
|
return function() {
|
2018-12-07 14:54:29 +00:00
|
|
|
const relation = new relationClass(definition, this);
|
2014-07-30 15:30:21 +00:00
|
|
|
return relationMethod.apply(relation, arguments);
|
2014-07-29 20:59:44 +00:00
|
|
|
};
|
2018-12-07 14:43:40 +00:00
|
|
|
}
|
2018-12-07 14:54:29 +00:00
|
|
|
for (const key in ext) {
|
|
|
|
const relationMethod = ext[key];
|
|
|
|
const method = scopeMethods[key] = createFunc(definition, relationMethod);
|
2014-07-30 15:30:21 +00:00
|
|
|
if (relationMethod.shared) {
|
2014-08-01 09:24:41 +00:00
|
|
|
sharedMethod(definition, key, method, relationMethod);
|
2014-07-30 15:30:21 +00:00
|
|
|
}
|
2014-07-30 14:46:05 +00:00
|
|
|
customMethods.push(key);
|
2014-07-29 20:59:44 +00:00
|
|
|
}
|
|
|
|
}
|
2014-07-30 14:46:05 +00:00
|
|
|
return [].concat(customMethods || []);
|
2018-12-07 14:43:40 +00:00
|
|
|
}
|
2014-06-15 22:53:58 +00:00
|
|
|
|
2015-03-19 15:50:26 +00:00
|
|
|
function bindRelationMethods(relation, relationMethod, definition) {
|
2018-12-07 14:54:29 +00:00
|
|
|
const methods = definition.methods || {};
|
2015-03-19 15:50:26 +00:00
|
|
|
Object.keys(methods).forEach(function(m) {
|
|
|
|
if (typeof methods[m] !== 'function') return;
|
|
|
|
relationMethod[m] = methods[m].bind(relation);
|
|
|
|
});
|
2018-12-07 14:43:40 +00:00
|
|
|
}
|
2015-03-19 15:50:26 +00:00
|
|
|
|
2018-01-17 18:34:37 +00:00
|
|
|
function preventFkOverride(inst, data, fkProp) {
|
|
|
|
if (!fkProp) return undefined;
|
|
|
|
if (data[fkProp] !== undefined && !idEquals(data[fkProp], inst[fkProp])) {
|
2018-12-07 15:22:36 +00:00
|
|
|
return new Error(g.f(
|
2018-01-17 18:34:37 +00:00
|
|
|
'Cannot override foreign key %s from %s to %s',
|
|
|
|
fkProp,
|
|
|
|
inst[fkProp],
|
2019-12-03 09:09:16 +00:00
|
|
|
data[fkProp],
|
2018-07-16 06:46:25 +00:00
|
|
|
));
|
2018-01-17 18:34:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-15 22:53:58 +00:00
|
|
|
/**
|
|
|
|
* Relation definition class. Use to define relationships between models.
|
|
|
|
* @param {Object} definition
|
|
|
|
* @class RelationDefinition
|
|
|
|
*/
|
|
|
|
function RelationDefinition(definition) {
|
|
|
|
if (!(this instanceof RelationDefinition)) {
|
|
|
|
return new RelationDefinition(definition);
|
|
|
|
}
|
|
|
|
definition = definition || {};
|
|
|
|
this.name = definition.name;
|
|
|
|
assert(this.name, 'Relation name is missing');
|
|
|
|
this.type = normalizeType(definition.type);
|
|
|
|
assert(this.type, 'Invalid relation type: ' + definition.type);
|
|
|
|
this.modelFrom = definition.modelFrom;
|
2014-06-21 18:44:33 +00:00
|
|
|
assert(this.modelFrom, 'Source model is required');
|
2014-06-15 22:53:58 +00:00
|
|
|
this.keyFrom = definition.keyFrom;
|
|
|
|
this.modelTo = definition.modelTo;
|
|
|
|
this.keyTo = definition.keyTo;
|
2014-08-15 10:55:10 +00:00
|
|
|
this.polymorphic = definition.polymorphic;
|
|
|
|
if (typeof this.polymorphic !== 'object') {
|
2014-07-26 10:47:55 +00:00
|
|
|
assert(this.modelTo, 'Target model is required');
|
|
|
|
}
|
2014-06-15 22:53:58 +00:00
|
|
|
this.modelThrough = definition.modelThrough;
|
|
|
|
this.keyThrough = definition.keyThrough;
|
2014-11-24 09:31:28 +00:00
|
|
|
this.multiple = definition.multiple;
|
2014-07-11 22:02:16 +00:00
|
|
|
this.properties = definition.properties || {};
|
2014-07-21 20:39:06 +00:00
|
|
|
this.options = definition.options || {};
|
2014-07-11 13:29:47 +00:00
|
|
|
this.scope = definition.scope;
|
2014-07-27 14:30:45 +00:00
|
|
|
this.embed = definition.embed === true;
|
2015-03-19 15:50:26 +00:00
|
|
|
this.methods = definition.methods || {};
|
2014-06-15 22:53:58 +00:00
|
|
|
}
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
RelationDefinition.prototype.toJSON = function() {
|
2018-12-07 14:54:29 +00:00
|
|
|
const polymorphic = typeof this.polymorphic === 'object';
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
let modelToName = this.modelTo && this.modelTo.modelName;
|
2014-08-16 08:23:32 +00:00
|
|
|
if (!modelToName && polymorphic && this.type === 'belongsTo') {
|
|
|
|
modelToName = '<polymorphic>';
|
2015-02-24 10:36:13 +00:00
|
|
|
}
|
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
const json = {
|
2014-06-15 22:53:58 +00:00
|
|
|
name: this.name,
|
|
|
|
type: this.type,
|
|
|
|
modelFrom: this.modelFrom.modelName,
|
|
|
|
keyFrom: this.keyFrom,
|
2014-08-16 08:23:32 +00:00
|
|
|
modelTo: modelToName,
|
2014-06-15 22:53:58 +00:00
|
|
|
keyTo: this.keyTo,
|
2016-04-01 11:48:17 +00:00
|
|
|
multiple: this.multiple,
|
2014-06-15 22:53:58 +00:00
|
|
|
};
|
|
|
|
if (this.modelThrough) {
|
|
|
|
json.modelThrough = this.modelThrough.modelName;
|
|
|
|
json.keyThrough = this.keyThrough;
|
|
|
|
}
|
2014-08-16 08:23:32 +00:00
|
|
|
if (polymorphic) {
|
|
|
|
json.polymorphic = this.polymorphic;
|
|
|
|
}
|
2014-06-15 22:53:58 +00:00
|
|
|
return json;
|
|
|
|
};
|
|
|
|
|
2014-08-13 09:28:23 +00:00
|
|
|
/**
|
|
|
|
* Define a relation scope method
|
|
|
|
* @param {String} name of the method
|
|
|
|
* @param {Function} function to define
|
|
|
|
*/
|
|
|
|
RelationDefinition.prototype.defineMethod = function(name, fn) {
|
2018-12-07 14:54:29 +00:00
|
|
|
const relationClass = RelationClasses[this.type];
|
|
|
|
const relationName = this.name;
|
|
|
|
const modelFrom = this.modelFrom;
|
|
|
|
const definition = this;
|
|
|
|
let method;
|
2015-03-19 15:50:26 +00:00
|
|
|
if (definition.multiple) {
|
2018-12-07 14:54:29 +00:00
|
|
|
const scope = this.modelFrom.scopes[this.name];
|
2016-07-22 19:26:07 +00:00
|
|
|
if (!scope) throw new Error(g.f('Unknown relation {{scope}}: %s', this.name));
|
2015-03-19 15:50:26 +00:00
|
|
|
method = scope.defineMethod(name, function() {
|
2018-12-07 14:54:29 +00:00
|
|
|
const relation = new relationClass(definition, this);
|
2015-03-19 15:50:26 +00:00
|
|
|
return fn.apply(relation, arguments);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
definition.methods[name] = fn;
|
|
|
|
method = function() {
|
2018-12-07 14:54:29 +00:00
|
|
|
const rel = this[relationName];
|
2015-03-19 15:50:26 +00:00
|
|
|
return rel[name].apply(rel, arguments);
|
2016-04-01 11:48:17 +00:00
|
|
|
};
|
2015-03-19 15:50:26 +00:00
|
|
|
}
|
|
|
|
if (method && fn.shared) {
|
2014-08-13 09:28:23 +00:00
|
|
|
sharedMethod(definition, name, method, fn);
|
|
|
|
modelFrom.prototype['__' + name + '__' + relationName] = method;
|
|
|
|
}
|
|
|
|
return method;
|
|
|
|
};
|
|
|
|
|
2014-07-11 13:29:47 +00:00
|
|
|
/**
|
|
|
|
* Apply the configured scope to the filter/query object.
|
|
|
|
* @param {Object} modelInstance
|
|
|
|
* @param {Object} filter (where, order, limit, fields, ...)
|
|
|
|
*/
|
|
|
|
RelationDefinition.prototype.applyScope = function(modelInstance, filter) {
|
2014-07-30 11:22:20 +00:00
|
|
|
filter = filter || {};
|
2014-07-26 10:47:55 +00:00
|
|
|
filter.where = filter.where || {};
|
2016-04-01 13:23:42 +00:00
|
|
|
if ((this.type !== 'belongsTo' || this.type === 'hasOne') &&
|
|
|
|
typeof this.polymorphic === 'object') { // polymorphic
|
2018-12-07 14:54:29 +00:00
|
|
|
const discriminator = this.polymorphic.discriminator;
|
2014-08-15 10:55:10 +00:00
|
|
|
if (this.polymorphic.invert) {
|
|
|
|
filter.where[discriminator] = this.modelTo.modelName;
|
|
|
|
} else {
|
|
|
|
filter.where[discriminator] = this.modelFrom.modelName;
|
|
|
|
}
|
2014-07-26 10:47:55 +00:00
|
|
|
}
|
2018-12-07 14:54:29 +00:00
|
|
|
let scope;
|
2014-07-11 13:29:47 +00:00
|
|
|
if (typeof this.scope === 'function') {
|
2015-02-20 22:57:48 +00:00
|
|
|
scope = this.scope.call(this, modelInstance, filter);
|
2014-07-26 10:47:55 +00:00
|
|
|
} else {
|
2015-02-20 22:57:48 +00:00
|
|
|
scope = this.scope;
|
2014-07-26 10:47:55 +00:00
|
|
|
}
|
|
|
|
if (typeof scope === 'object') {
|
|
|
|
mergeQuery(filter, scope);
|
2014-07-11 13:29:47 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2014-07-11 22:02:16 +00:00
|
|
|
* Apply the configured properties to the target object.
|
2014-07-11 13:29:47 +00:00
|
|
|
* @param {Object} modelInstance
|
|
|
|
* @param {Object} target
|
|
|
|
*/
|
2014-08-15 09:28:25 +00:00
|
|
|
RelationDefinition.prototype.applyProperties = function(modelInstance, obj) {
|
2018-12-07 14:54:29 +00:00
|
|
|
let source = modelInstance, target = obj;
|
2014-08-15 09:28:25 +00:00
|
|
|
if (this.options.invertProperties) {
|
2015-02-20 22:57:48 +00:00
|
|
|
source = obj;
|
|
|
|
target = modelInstance;
|
2014-08-15 09:28:25 +00:00
|
|
|
}
|
2015-01-19 14:49:20 +00:00
|
|
|
if (this.options.embedsProperties) {
|
|
|
|
target = target.__data[this.name] = {};
|
|
|
|
target[this.keyTo] = source[this.keyTo];
|
|
|
|
}
|
2018-12-07 14:54:29 +00:00
|
|
|
let k, key;
|
2014-07-11 22:02:16 +00:00
|
|
|
if (typeof this.properties === 'function') {
|
2018-12-07 14:54:29 +00:00
|
|
|
const data = this.properties.call(this, source, target);
|
2016-04-01 11:48:17 +00:00
|
|
|
for (k in data) {
|
2014-07-11 13:29:47 +00:00
|
|
|
target[k] = data[k];
|
|
|
|
}
|
2015-01-19 14:49:20 +00:00
|
|
|
} else if (Array.isArray(this.properties)) {
|
2016-04-01 11:48:17 +00:00
|
|
|
for (k = 0; k < this.properties.length; k++) {
|
2015-02-20 22:57:48 +00:00
|
|
|
key = this.properties[k];
|
2015-01-19 14:49:20 +00:00
|
|
|
target[key] = source[key];
|
|
|
|
}
|
2014-07-11 22:02:16 +00:00
|
|
|
} else if (typeof this.properties === 'object') {
|
2016-04-01 11:48:17 +00:00
|
|
|
for (k in this.properties) {
|
2015-02-20 22:57:48 +00:00
|
|
|
key = this.properties[k];
|
2014-08-15 09:28:25 +00:00
|
|
|
target[key] = source[k];
|
2014-07-11 13:29:47 +00:00
|
|
|
}
|
|
|
|
}
|
2016-04-01 13:23:42 +00:00
|
|
|
if ((this.type !== 'belongsTo' || this.type === 'hasOne') &&
|
|
|
|
typeof this.polymorphic === 'object') { // polymorphic
|
2018-12-07 14:54:29 +00:00
|
|
|
const discriminator = this.polymorphic.discriminator;
|
2014-08-15 10:55:10 +00:00
|
|
|
if (this.polymorphic.invert) {
|
|
|
|
target[discriminator] = this.modelTo.modelName;
|
|
|
|
} else {
|
|
|
|
target[discriminator] = this.modelFrom.modelName;
|
|
|
|
}
|
2014-07-26 10:47:55 +00:00
|
|
|
}
|
2014-07-11 13:29:47 +00:00
|
|
|
};
|
|
|
|
|
2014-06-15 22:53:58 +00:00
|
|
|
/**
|
|
|
|
* A relation attaching to a given model instance
|
|
|
|
* @param {RelationDefinition|Object} definition
|
|
|
|
* @param {Object} modelInstance
|
|
|
|
* @returns {Relation}
|
|
|
|
* @constructor
|
|
|
|
* @class Relation
|
|
|
|
*/
|
|
|
|
function Relation(definition, modelInstance) {
|
|
|
|
if (!(this instanceof Relation)) {
|
|
|
|
return new Relation(definition, modelInstance);
|
|
|
|
}
|
|
|
|
if (!(definition instanceof RelationDefinition)) {
|
|
|
|
definition = new RelationDefinition(definition);
|
|
|
|
}
|
|
|
|
this.definition = definition;
|
|
|
|
this.modelInstance = modelInstance;
|
|
|
|
}
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
Relation.prototype.resetCache = function(cache) {
|
2014-06-21 18:44:33 +00:00
|
|
|
cache = cache || undefined;
|
|
|
|
this.modelInstance.__cachedRelations[this.definition.name] = cache;
|
|
|
|
};
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
Relation.prototype.getCache = function() {
|
2014-06-21 18:44:33 +00:00
|
|
|
return this.modelInstance.__cachedRelations[this.definition.name];
|
|
|
|
};
|
|
|
|
|
2014-10-12 20:00:13 +00:00
|
|
|
Relation.prototype.callScopeMethod = function(methodName) {
|
2018-12-07 14:54:29 +00:00
|
|
|
const args = Array.prototype.slice.call(arguments, 1);
|
|
|
|
const modelInstance = this.modelInstance;
|
|
|
|
const rel = modelInstance[this.definition.name];
|
2014-10-12 20:00:13 +00:00
|
|
|
if (rel && typeof rel[methodName] === 'function') {
|
|
|
|
return rel[methodName].apply(rel, args);
|
|
|
|
} else {
|
2016-07-22 19:26:07 +00:00
|
|
|
throw new Error(g.f('Unknown scope method: %s', methodName));
|
2014-10-12 20:00:13 +00:00
|
|
|
}
|
2015-05-16 17:11:17 +00:00
|
|
|
};
|
2014-10-12 20:00:13 +00:00
|
|
|
|
2014-08-13 09:28:23 +00:00
|
|
|
/**
|
|
|
|
* Fetch the related model(s) - this is a helper method to unify access.
|
|
|
|
* @param (Boolean|Object} condOrRefresh refresh or conditions object
|
2015-05-16 17:11:17 +00:00
|
|
|
* @param {Object} [options] Options
|
2014-08-13 09:28:23 +00:00
|
|
|
* @param {Function} cb callback
|
|
|
|
*/
|
2015-05-16 17:11:17 +00:00
|
|
|
Relation.prototype.fetch = function(condOrRefresh, options, cb) {
|
2014-08-13 09:28:23 +00:00
|
|
|
this.modelInstance[this.definition.name].apply(this.modelInstance, arguments);
|
|
|
|
};
|
|
|
|
|
2014-06-15 22:53:58 +00:00
|
|
|
/**
|
|
|
|
* HasMany subclass
|
|
|
|
* @param {RelationDefinition|Object} definition
|
|
|
|
* @param {Object} modelInstance
|
|
|
|
* @returns {HasMany}
|
|
|
|
* @constructor
|
|
|
|
* @class HasMany
|
|
|
|
*/
|
|
|
|
function HasMany(definition, modelInstance) {
|
|
|
|
if (!(this instanceof HasMany)) {
|
|
|
|
return new HasMany(definition, modelInstance);
|
|
|
|
}
|
|
|
|
assert(definition.type === RelationTypes.hasMany);
|
|
|
|
Relation.apply(this, arguments);
|
|
|
|
}
|
|
|
|
|
|
|
|
util.inherits(HasMany, Relation);
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
HasMany.prototype.removeFromCache = function(id) {
|
2018-12-07 14:54:29 +00:00
|
|
|
const cache = this.modelInstance.__cachedRelations[this.definition.name];
|
|
|
|
const idName = this.definition.modelTo.definition.idName();
|
2014-06-21 18:44:33 +00:00
|
|
|
if (Array.isArray(cache)) {
|
2018-12-07 14:54:29 +00:00
|
|
|
for (let i = 0, n = cache.length; i < n; i++) {
|
2016-04-01 11:48:17 +00:00
|
|
|
if (idEquals(cache[i][idName], id)) {
|
2014-06-21 18:44:33 +00:00
|
|
|
return cache.splice(i, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
};
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
HasMany.prototype.addToCache = function(inst) {
|
2014-06-21 18:44:33 +00:00
|
|
|
if (!inst) {
|
|
|
|
return;
|
|
|
|
}
|
2018-12-07 14:54:29 +00:00
|
|
|
let cache = this.modelInstance.__cachedRelations[this.definition.name];
|
2014-06-21 18:44:33 +00:00
|
|
|
if (cache === undefined) {
|
|
|
|
cache = this.modelInstance.__cachedRelations[this.definition.name] = [];
|
|
|
|
}
|
2018-12-07 14:54:29 +00:00
|
|
|
const idName = this.definition.modelTo.definition.idName();
|
2014-06-21 18:44:33 +00:00
|
|
|
if (Array.isArray(cache)) {
|
2018-12-07 14:54:29 +00:00
|
|
|
for (let i = 0, n = cache.length; i < n; i++) {
|
2016-04-01 11:48:17 +00:00
|
|
|
if (idEquals(cache[i][idName], inst[idName])) {
|
2014-06-21 18:44:33 +00:00
|
|
|
cache[i] = inst;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
cache.push(inst);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-06-15 22:53:58 +00:00
|
|
|
/**
|
|
|
|
* HasManyThrough subclass
|
|
|
|
* @param {RelationDefinition|Object} definition
|
|
|
|
* @param {Object} modelInstance
|
|
|
|
* @returns {HasManyThrough}
|
|
|
|
* @constructor
|
|
|
|
* @class HasManyThrough
|
|
|
|
*/
|
|
|
|
function HasManyThrough(definition, modelInstance) {
|
|
|
|
if (!(this instanceof HasManyThrough)) {
|
|
|
|
return new HasManyThrough(definition, modelInstance);
|
|
|
|
}
|
|
|
|
assert(definition.type === RelationTypes.hasMany);
|
|
|
|
assert(definition.modelThrough);
|
|
|
|
HasMany.apply(this, arguments);
|
|
|
|
}
|
|
|
|
|
|
|
|
util.inherits(HasManyThrough, HasMany);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* BelongsTo subclass
|
|
|
|
* @param {RelationDefinition|Object} definition
|
|
|
|
* @param {Object} modelInstance
|
|
|
|
* @returns {BelongsTo}
|
|
|
|
* @constructor
|
|
|
|
* @class BelongsTo
|
|
|
|
*/
|
|
|
|
function BelongsTo(definition, modelInstance) {
|
|
|
|
if (!(this instanceof BelongsTo)) {
|
|
|
|
return new BelongsTo(definition, modelInstance);
|
|
|
|
}
|
|
|
|
assert(definition.type === RelationTypes.belongsTo);
|
|
|
|
Relation.apply(this, arguments);
|
|
|
|
}
|
|
|
|
|
|
|
|
util.inherits(BelongsTo, Relation);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* HasAndBelongsToMany subclass
|
|
|
|
* @param {RelationDefinition|Object} definition
|
|
|
|
* @param {Object} modelInstance
|
|
|
|
* @returns {HasAndBelongsToMany}
|
|
|
|
* @constructor
|
|
|
|
* @class HasAndBelongsToMany
|
|
|
|
*/
|
|
|
|
function HasAndBelongsToMany(definition, modelInstance) {
|
|
|
|
if (!(this instanceof HasAndBelongsToMany)) {
|
|
|
|
return new HasAndBelongsToMany(definition, modelInstance);
|
|
|
|
}
|
|
|
|
assert(definition.type === RelationTypes.hasAndBelongsToMany);
|
|
|
|
Relation.apply(this, arguments);
|
|
|
|
}
|
|
|
|
|
|
|
|
util.inherits(HasAndBelongsToMany, Relation);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* HasOne subclass
|
|
|
|
* @param {RelationDefinition|Object} definition
|
|
|
|
* @param {Object} modelInstance
|
|
|
|
* @returns {HasOne}
|
|
|
|
* @constructor
|
|
|
|
* @class HasOne
|
|
|
|
*/
|
|
|
|
function HasOne(definition, modelInstance) {
|
|
|
|
if (!(this instanceof HasOne)) {
|
|
|
|
return new HasOne(definition, modelInstance);
|
|
|
|
}
|
|
|
|
assert(definition.type === RelationTypes.hasOne);
|
|
|
|
Relation.apply(this, arguments);
|
|
|
|
}
|
|
|
|
|
|
|
|
util.inherits(HasOne, Relation);
|
|
|
|
|
2014-08-19 20:10:35 +00:00
|
|
|
/**
|
|
|
|
* EmbedsOne subclass
|
|
|
|
* @param {RelationDefinition|Object} definition
|
|
|
|
* @param {Object} modelInstance
|
2014-09-04 13:37:48 +00:00
|
|
|
* @returns {EmbedsOne}
|
2014-08-19 20:10:35 +00:00
|
|
|
* @constructor
|
|
|
|
* @class EmbedsOne
|
|
|
|
*/
|
|
|
|
function EmbedsOne(definition, modelInstance) {
|
|
|
|
if (!(this instanceof EmbedsOne)) {
|
2014-09-04 13:37:48 +00:00
|
|
|
return new EmbedsOne(definition, modelInstance);
|
2014-08-19 20:10:35 +00:00
|
|
|
}
|
|
|
|
assert(definition.type === RelationTypes.embedsOne);
|
|
|
|
Relation.apply(this, arguments);
|
|
|
|
}
|
|
|
|
|
|
|
|
util.inherits(EmbedsOne, Relation);
|
|
|
|
|
2014-07-27 14:30:45 +00:00
|
|
|
/**
|
|
|
|
* EmbedsMany subclass
|
|
|
|
* @param {RelationDefinition|Object} definition
|
|
|
|
* @param {Object} modelInstance
|
|
|
|
* @returns {EmbedsMany}
|
|
|
|
* @constructor
|
|
|
|
* @class EmbedsMany
|
|
|
|
*/
|
|
|
|
function EmbedsMany(definition, modelInstance) {
|
|
|
|
if (!(this instanceof EmbedsMany)) {
|
|
|
|
return new EmbedsMany(definition, modelInstance);
|
|
|
|
}
|
|
|
|
assert(definition.type === RelationTypes.embedsMany);
|
|
|
|
Relation.apply(this, arguments);
|
|
|
|
}
|
|
|
|
|
|
|
|
util.inherits(EmbedsMany, Relation);
|
2014-06-15 22:53:58 +00:00
|
|
|
|
2014-07-29 08:51:33 +00:00
|
|
|
/**
|
|
|
|
* ReferencesMany subclass
|
|
|
|
* @param {RelationDefinition|Object} definition
|
|
|
|
* @param {Object} modelInstance
|
2014-07-29 19:46:12 +00:00
|
|
|
* @returns {ReferencesMany}
|
2014-07-29 08:51:33 +00:00
|
|
|
* @constructor
|
2014-07-29 19:46:12 +00:00
|
|
|
* @class ReferencesMany
|
2014-07-29 08:51:33 +00:00
|
|
|
*/
|
|
|
|
function ReferencesMany(definition, modelInstance) {
|
2014-07-29 19:46:12 +00:00
|
|
|
if (!(this instanceof ReferencesMany)) {
|
|
|
|
return new ReferencesMany(definition, modelInstance);
|
2014-07-29 08:51:33 +00:00
|
|
|
}
|
2014-07-29 19:46:12 +00:00
|
|
|
assert(definition.type === RelationTypes.referencesMany);
|
2014-07-29 08:51:33 +00:00
|
|
|
Relation.apply(this, arguments);
|
|
|
|
}
|
|
|
|
|
|
|
|
util.inherits(ReferencesMany, Relation);
|
|
|
|
|
2014-06-15 22:53:58 +00:00
|
|
|
/*!
|
|
|
|
* Find the relation by foreign key
|
|
|
|
* @param {*} foreignKey The foreign key
|
2015-02-16 23:36:51 +00:00
|
|
|
* @returns {Array} The array of matching relation objects
|
2014-06-15 22:53:58 +00:00
|
|
|
*/
|
2014-06-16 07:36:12 +00:00
|
|
|
function findBelongsTo(modelFrom, modelTo, keyTo) {
|
2015-03-25 12:47:46 +00:00
|
|
|
return Object.keys(modelFrom.relations)
|
|
|
|
.map(function(k) { return modelFrom.relations[k]; })
|
|
|
|
.filter(function(rel) {
|
|
|
|
return (rel.type === RelationTypes.belongsTo &&
|
|
|
|
rel.modelTo === modelTo &&
|
|
|
|
(keyTo === undefined || rel.keyTo === keyTo));
|
|
|
|
})
|
|
|
|
.map(function(rel) {
|
|
|
|
return rel.keyFrom;
|
|
|
|
});
|
2014-06-15 22:53:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* Look up a model by name from the list of given models
|
|
|
|
* @param {Object} models Models keyed by name
|
|
|
|
* @param {String} modelName The model name
|
|
|
|
* @returns {*} The matching model class
|
|
|
|
*/
|
2014-09-04 15:54:42 +00:00
|
|
|
function lookupModel(models, modelName) {
|
2016-04-01 11:48:17 +00:00
|
|
|
if (models[modelName]) {
|
2014-06-15 22:53:58 +00:00
|
|
|
return models[modelName];
|
|
|
|
}
|
2018-12-07 14:54:29 +00:00
|
|
|
const lookupClassName = modelName.toLowerCase();
|
|
|
|
for (const name in models) {
|
2014-06-15 22:53:58 +00:00
|
|
|
if (name.toLowerCase() === lookupClassName) {
|
|
|
|
return models[name];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-01 09:13:22 +00:00
|
|
|
/*
|
|
|
|
* @param {Object} modelFrom Instance of the 'from' model
|
|
|
|
* @param {Object|String} modelToRef Reference to Model object to which you are
|
|
|
|
* creating the relation: model instance, model name, or name of relation to model.
|
|
|
|
* @param {Object} params The relation params
|
|
|
|
* @param {Boolean} singularize Whether the modelToRef should be singularized when
|
|
|
|
* looking-up modelTo
|
|
|
|
* @return {Object} modelTo Instance of the 'to' model
|
|
|
|
*/
|
|
|
|
function lookupModelTo(modelFrom, modelToRef, params, singularize) {
|
2018-12-07 14:54:29 +00:00
|
|
|
let modelTo;
|
2017-04-01 09:13:22 +00:00
|
|
|
|
|
|
|
if (typeof modelToRef !== 'string') {
|
|
|
|
// modelToRef might already be an instance of model
|
|
|
|
modelTo = modelToRef;
|
|
|
|
} else {
|
|
|
|
// lookup modelTo based on relation params and modelToRef
|
2018-12-07 14:54:29 +00:00
|
|
|
let modelToName;
|
2017-04-01 09:13:22 +00:00
|
|
|
modelTo = params.model || modelToRef; // modelToRef might be modelTo name
|
|
|
|
|
2014-08-16 08:23:32 +00:00
|
|
|
if (typeof modelTo === 'string') {
|
2017-04-01 09:13:22 +00:00
|
|
|
// lookup modelTo by name
|
|
|
|
modelToName = modelTo;
|
|
|
|
modelToName = (singularize ? i8n.singularize(modelToName) : modelToName).toLowerCase();
|
|
|
|
modelTo = lookupModel(modelFrom.dataSource.modelBuilder.models, modelToName);
|
2014-08-09 08:58:03 +00:00
|
|
|
}
|
2017-04-01 09:13:22 +00:00
|
|
|
|
|
|
|
if (!modelTo) {
|
|
|
|
// lookup by modelTo name was not successful. Now looking-up by relationTo name
|
2018-12-07 14:54:29 +00:00
|
|
|
const relationToName = params.as || modelToRef; // modelToRef might be relationTo name
|
2017-04-01 09:13:22 +00:00
|
|
|
modelToName = (singularize ? i8n.singularize(relationToName) : relationToName).toLowerCase();
|
|
|
|
modelTo = lookupModel(modelFrom.dataSource.modelBuilder.models, modelToName);
|
2014-08-09 08:58:03 +00:00
|
|
|
}
|
|
|
|
}
|
2017-04-01 09:13:22 +00:00
|
|
|
if (typeof modelTo !== 'function') {
|
|
|
|
throw new Error(g.f('Could not find relation %s for model %s', params.as, modelFrom.modelName));
|
|
|
|
}
|
2014-08-09 08:58:03 +00:00
|
|
|
return modelTo;
|
|
|
|
}
|
|
|
|
|
2017-04-01 09:13:22 +00:00
|
|
|
/*
|
|
|
|
* Normalize relation's parameter `as`
|
|
|
|
* @param {Object} params The relation params
|
|
|
|
* @param {String} relationName The relation name
|
2014-07-28 08:44:26 +00:00
|
|
|
* @returns {Object} The normalized parameters
|
2017-04-01 09:13:22 +00:00
|
|
|
* NOTE: normalizeRelationAs() mutates the params object
|
2014-07-28 08:44:26 +00:00
|
|
|
*/
|
2017-04-01 09:13:22 +00:00
|
|
|
function normalizeRelationAs(params, relationName) {
|
|
|
|
if (typeof relationName === 'string') {
|
|
|
|
params.as = params.as || relationName;
|
|
|
|
}
|
2014-07-28 08:44:26 +00:00
|
|
|
return params;
|
2014-07-28 21:22:45 +00:00
|
|
|
}
|
2014-07-28 08:44:26 +00:00
|
|
|
|
2017-04-01 09:13:22 +00:00
|
|
|
/*
|
|
|
|
* Normalize relation's polymorphic parameters
|
|
|
|
* @param {Object|String|Boolean} polymorphic Param `polymorphic` of the relation.
|
|
|
|
* @param {String} relationName The name of the relation we are currently setting up.
|
|
|
|
* @returns {Object} The normalized parameters
|
|
|
|
*/
|
|
|
|
function normalizePolymorphic(polymorphic, relationName) {
|
|
|
|
assert(polymorphic, 'polymorphic param can\'t be false, null or undefined');
|
|
|
|
assert(!Array.isArray(polymorphic, 'unexpected type for polymorphic param: \'Array\''));
|
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
let selector;
|
2017-04-01 09:13:22 +00:00
|
|
|
|
|
|
|
if (typeof polymorphic === 'string') {
|
|
|
|
// relation type is different from belongsTo (hasMany, hasManyThrough, hasAndBelongsToMany, ...)
|
|
|
|
// polymorphic is the name of the matching belongsTo relation from modelTo to modelFrom
|
|
|
|
selector = polymorphic;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (polymorphic === true) {
|
|
|
|
// relation type is belongsTo: the relation name is used as the polymorphic selector
|
|
|
|
selector = relationName;
|
|
|
|
}
|
|
|
|
|
|
|
|
// NOTE: use of `polymorphic.as` keyword will be deprecated in LoopBack.next
|
|
|
|
// to avoid confusion with keyword `as` used at the root of the relation definition object
|
|
|
|
// It is replaced with the `polymorphic.selector` keyword
|
|
|
|
if (typeof polymorphic == 'object') {
|
|
|
|
selector = polymorphic.selector || polymorphic.as;
|
|
|
|
}
|
|
|
|
|
|
|
|
// relationName is eventually used as selector if provided and selector not already defined
|
|
|
|
// it ultimately defaults to 'reference'
|
|
|
|
selector = selector || relationName || 'reference';
|
|
|
|
|
|
|
|
// make sure polymorphic is an object
|
|
|
|
if (typeof polymorphic !== 'object') {
|
|
|
|
polymorphic = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
polymorphic.selector = selector;
|
|
|
|
polymorphic.foreignKey = polymorphic.foreignKey || i8n.camelize(selector + '_id', true); // defaults to {{selector}}Id
|
|
|
|
polymorphic.discriminator = polymorphic.discriminator || i8n.camelize(selector + '_type', true); // defaults to {{selectorName}}Type
|
|
|
|
|
|
|
|
return polymorphic;
|
|
|
|
}
|
|
|
|
|
2014-06-15 22:53:58 +00:00
|
|
|
/**
|
|
|
|
* Define a "one to many" relationship by specifying the model name
|
2015-02-24 10:36:13 +00:00
|
|
|
*
|
2014-06-15 22:53:58 +00:00
|
|
|
* Examples:
|
|
|
|
* ```
|
|
|
|
* User.hasMany(Post, {as: 'posts', foreignKey: 'authorId'});
|
|
|
|
* ```
|
2015-02-24 10:36:13 +00:00
|
|
|
*
|
2014-06-15 22:53:58 +00:00
|
|
|
* ```
|
|
|
|
* Book.hasMany(Chapter);
|
|
|
|
* ```
|
|
|
|
* Or, equivalently:
|
|
|
|
* ```
|
|
|
|
* Book.hasMany('chapters', {model: Chapter});
|
|
|
|
* ```
|
|
|
|
* @param {Model} modelFrom Source model class
|
2017-04-01 09:13:22 +00:00
|
|
|
* @param {Object|String} modelToRef Reference to Model object to which you are
|
|
|
|
* creating the relation: model instance, model name, or name of relation to model.
|
2014-06-15 22:53:58 +00:00
|
|
|
* @options {Object} params Configuration parameters; see below.
|
|
|
|
* @property {String} as Name of the property in the referring model that corresponds to the foreign key field in the related model.
|
|
|
|
* @property {String} foreignKey Property name of foreign key field.
|
|
|
|
* @property {Object} model Model object
|
|
|
|
*/
|
2017-04-01 09:13:22 +00:00
|
|
|
RelationDefinition.hasMany = function hasMany(modelFrom, modelToRef, params) {
|
2018-12-07 14:54:29 +00:00
|
|
|
const thisClassName = modelFrom.modelName;
|
2014-06-15 22:53:58 +00:00
|
|
|
params = params || {};
|
2017-04-01 09:13:22 +00:00
|
|
|
normalizeRelationAs(params, modelToRef);
|
2018-12-07 14:54:29 +00:00
|
|
|
const modelTo = lookupModelTo(modelFrom, modelToRef, params, true);
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
const relationName = params.as || i8n.camelize(modelTo.pluralModelName, true);
|
|
|
|
let fk = params.foreignKey || i8n.camelize(thisClassName + '_id', true);
|
|
|
|
let keyThrough = params.keyThrough || i8n.camelize(modelTo.modelName + '_id', true);
|
2014-06-15 22:53:58 +00:00
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
const pkName = params.primaryKey || modelFrom.dataSource.idName(modelFrom.modelName) || 'id';
|
|
|
|
let discriminator, polymorphic;
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2014-07-28 08:44:26 +00:00
|
|
|
if (params.polymorphic) {
|
2017-04-01 09:13:22 +00:00
|
|
|
polymorphic = normalizePolymorphic(params.polymorphic, relationName);
|
2014-09-03 11:58:39 +00:00
|
|
|
if (params.invert) {
|
|
|
|
polymorphic.invert = true;
|
|
|
|
keyThrough = polymorphic.foreignKey;
|
|
|
|
}
|
2014-07-28 08:44:26 +00:00
|
|
|
discriminator = polymorphic.discriminator;
|
2014-07-26 19:11:25 +00:00
|
|
|
if (!params.invert) {
|
2014-07-28 08:44:26 +00:00
|
|
|
fk = polymorphic.foreignKey;
|
2014-07-26 19:11:25 +00:00
|
|
|
}
|
2014-07-26 10:47:55 +00:00
|
|
|
if (!params.through) {
|
2016-08-19 17:46:59 +00:00
|
|
|
modelTo.dataSource.defineProperty(modelTo.modelName, discriminator, {type: 'string', index: true});
|
2014-07-26 10:47:55 +00:00
|
|
|
}
|
|
|
|
}
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
const definition = new RelationDefinition({
|
2014-06-15 22:53:58 +00:00
|
|
|
name: relationName,
|
|
|
|
type: RelationTypes.hasMany,
|
|
|
|
modelFrom: modelFrom,
|
2015-08-31 20:24:47 +00:00
|
|
|
keyFrom: pkName,
|
2014-06-15 22:53:58 +00:00
|
|
|
keyTo: fk,
|
|
|
|
modelTo: modelTo,
|
2014-07-11 13:29:47 +00:00
|
|
|
multiple: true,
|
2014-07-11 22:02:16 +00:00
|
|
|
properties: params.properties,
|
2014-07-21 20:39:06 +00:00
|
|
|
scope: params.scope,
|
2014-08-15 10:55:10 +00:00
|
|
|
options: params.options,
|
2014-09-03 11:58:39 +00:00
|
|
|
keyThrough: keyThrough,
|
2016-04-01 11:48:17 +00:00
|
|
|
polymorphic: polymorphic,
|
2014-06-15 22:53:58 +00:00
|
|
|
});
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2014-07-26 12:54:54 +00:00
|
|
|
definition.modelThrough = params.through;
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2014-06-15 22:53:58 +00:00
|
|
|
modelFrom.relations[relationName] = definition;
|
|
|
|
|
|
|
|
if (!params.through) {
|
|
|
|
// obviously, modelTo should have attribute called `fk`
|
2014-07-26 10:47:55 +00:00
|
|
|
// for polymorphic relations, it is assumed to share the same fk type for all
|
|
|
|
// polymorphic models
|
2015-08-31 20:24:47 +00:00
|
|
|
modelTo.dataSource.defineForeignKey(modelTo.modelName, fk, modelFrom.modelName, pkName);
|
2014-06-15 22:53:58 +00:00
|
|
|
}
|
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
const scopeMethods = {
|
2014-06-15 22:53:58 +00:00
|
|
|
findById: scopeMethod(definition, 'findById'),
|
2014-07-15 15:50:34 +00:00
|
|
|
destroy: scopeMethod(definition, 'destroyById'),
|
|
|
|
updateById: scopeMethod(definition, 'updateById'),
|
2016-04-01 11:48:17 +00:00
|
|
|
exists: scopeMethod(definition, 'exists'),
|
2014-07-15 15:50:34 +00:00
|
|
|
};
|
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
const findByIdFunc = scopeMethods.findById;
|
2014-07-15 15:50:34 +00:00
|
|
|
modelFrom.prototype['__findById__' + relationName] = findByIdFunc;
|
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
const destroyByIdFunc = scopeMethods.destroy;
|
2014-07-15 15:50:34 +00:00
|
|
|
modelFrom.prototype['__destroyById__' + relationName] = destroyByIdFunc;
|
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
const updateByIdFunc = scopeMethods.updateById;
|
2014-07-15 15:50:34 +00:00
|
|
|
modelFrom.prototype['__updateById__' + relationName] = updateByIdFunc;
|
2014-06-15 22:53:58 +00:00
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
const existsByIdFunc = scopeMethods.exists;
|
2014-07-28 09:36:32 +00:00
|
|
|
modelFrom.prototype['__exists__' + relationName] = existsByIdFunc;
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
if (definition.modelThrough) {
|
2014-06-15 22:53:58 +00:00
|
|
|
scopeMethods.create = scopeMethod(definition, 'create');
|
|
|
|
scopeMethods.add = scopeMethod(definition, 'add');
|
|
|
|
scopeMethods.remove = scopeMethod(definition, 'remove');
|
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
const addFunc = scopeMethods.add;
|
2014-07-15 15:50:34 +00:00
|
|
|
modelFrom.prototype['__link__' + relationName] = addFunc;
|
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
const removeFunc = scopeMethods.remove;
|
2014-07-15 15:50:34 +00:00
|
|
|
modelFrom.prototype['__unlink__' + relationName] = removeFunc;
|
2014-06-27 06:38:04 +00:00
|
|
|
} else {
|
|
|
|
scopeMethods.create = scopeMethod(definition, 'create');
|
|
|
|
scopeMethods.build = scopeMethod(definition, 'build');
|
2014-06-15 22:53:58 +00:00
|
|
|
}
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
const customMethods = extendScopeMethods(definition, scopeMethods, params.scopeMethods);
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
for (let i = 0; i < customMethods.length; i++) {
|
|
|
|
const methodName = customMethods[i];
|
|
|
|
const method = scopeMethods[methodName];
|
2014-07-30 14:46:05 +00:00
|
|
|
if (typeof method === 'function' && method.shared === true) {
|
|
|
|
modelFrom.prototype['__' + methodName + '__' + relationName] = method;
|
|
|
|
}
|
2018-12-07 14:43:40 +00:00
|
|
|
}
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2014-06-15 22:53:58 +00:00
|
|
|
// Mix the property and scoped methods into the prototype class
|
2016-04-01 11:48:17 +00:00
|
|
|
defineScope(modelFrom.prototype, params.through || modelTo, relationName, function() {
|
2018-12-07 14:54:29 +00:00
|
|
|
const filter = {};
|
2014-06-15 22:53:58 +00:00
|
|
|
filter.where = {};
|
2015-08-31 20:24:47 +00:00
|
|
|
filter.where[fk] = this[pkName];
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2014-07-11 13:29:47 +00:00
|
|
|
definition.applyScope(this, filter);
|
2014-09-03 03:04:58 +00:00
|
|
|
|
2014-09-03 11:58:39 +00:00
|
|
|
if (definition.modelThrough) {
|
2018-12-07 14:54:29 +00:00
|
|
|
let throughRelationName;
|
2014-09-03 11:58:39 +00:00
|
|
|
|
|
|
|
// find corresponding belongsTo relations from through model as collect
|
2018-12-07 14:54:29 +00:00
|
|
|
for (const r in definition.modelThrough.relations) {
|
|
|
|
const relation = definition.modelThrough.relations[r];
|
2014-09-03 11:58:39 +00:00
|
|
|
|
|
|
|
// should be a belongsTo and match modelTo and keyThrough
|
|
|
|
// if relation is polymorphic then check keyThrough only
|
|
|
|
if (relation.type === RelationTypes.belongsTo &&
|
|
|
|
(relation.polymorphic && !relation.modelTo || relation.modelTo === definition.modelTo) &&
|
|
|
|
(relation.keyFrom === definition.keyThrough)
|
2018-06-12 07:13:32 +00:00
|
|
|
) {
|
2014-09-03 11:58:39 +00:00
|
|
|
throughRelationName = relation.name;
|
|
|
|
break;
|
2014-09-03 03:04:58 +00:00
|
|
|
}
|
2014-09-03 11:58:39 +00:00
|
|
|
}
|
2014-09-03 03:04:58 +00:00
|
|
|
|
2014-09-03 11:58:39 +00:00
|
|
|
if (definition.polymorphic && definition.polymorphic.invert) {
|
2017-04-01 09:13:22 +00:00
|
|
|
filter.collect = definition.polymorphic.selector;
|
2014-09-03 04:31:09 +00:00
|
|
|
filter.include = filter.collect;
|
|
|
|
} else {
|
2014-09-03 11:58:39 +00:00
|
|
|
filter.collect = throughRelationName || i8n.camelize(modelTo.modelName, true);
|
|
|
|
filter.include = filter.collect;
|
2014-09-03 04:31:09 +00:00
|
|
|
}
|
2014-06-15 22:53:58 +00:00
|
|
|
}
|
2014-09-03 04:31:09 +00:00
|
|
|
|
2014-06-15 22:53:58 +00:00
|
|
|
return filter;
|
2014-08-08 16:25:49 +00:00
|
|
|
}, scopeMethods, definition.options);
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2014-08-13 09:28:23 +00:00
|
|
|
return definition;
|
2014-06-15 22:53:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
function scopeMethod(definition, methodName) {
|
2018-12-07 14:54:29 +00:00
|
|
|
let relationClass = RelationClasses[definition.type];
|
2014-07-15 15:50:34 +00:00
|
|
|
if (definition.type === RelationTypes.hasMany && definition.modelThrough) {
|
|
|
|
relationClass = RelationClasses.hasManyThrough;
|
|
|
|
}
|
2018-12-07 14:54:29 +00:00
|
|
|
const method = function() {
|
|
|
|
const relation = new relationClass(definition, this);
|
2014-06-15 22:53:58 +00:00
|
|
|
return relation[methodName].apply(relation, arguments);
|
|
|
|
};
|
2014-07-15 15:50:34 +00:00
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
const relationMethod = relationClass.prototype[methodName];
|
2014-07-15 15:50:34 +00:00
|
|
|
if (relationMethod.shared) {
|
2014-08-01 09:24:41 +00:00
|
|
|
sharedMethod(definition, methodName, method, relationMethod);
|
2014-07-15 15:50:34 +00:00
|
|
|
}
|
2014-06-15 22:53:58 +00:00
|
|
|
return method;
|
|
|
|
}
|
|
|
|
|
2014-08-01 09:24:41 +00:00
|
|
|
function sharedMethod(definition, methodName, method, relationMethod) {
|
|
|
|
method.shared = true;
|
|
|
|
method.accepts = relationMethod.accepts;
|
|
|
|
method.returns = relationMethod.returns;
|
|
|
|
method.http = relationMethod.http;
|
|
|
|
method.description = relationMethod.description;
|
2015-05-16 17:11:17 +00:00
|
|
|
}
|
2014-08-01 09:24:41 +00:00
|
|
|
|
2014-07-15 15:50:34 +00:00
|
|
|
/**
|
|
|
|
* Find a related item by foreign key
|
|
|
|
* @param {*} fkId The foreign key
|
2015-05-16 17:11:17 +00:00
|
|
|
* @param {Object} [options] Options
|
2014-07-15 15:50:34 +00:00
|
|
|
* @param {Function} cb The callback function
|
|
|
|
*/
|
2016-04-01 11:48:17 +00:00
|
|
|
HasMany.prototype.findById = function(fkId, options, cb) {
|
2015-05-16 17:11:17 +00:00
|
|
|
if (typeof options === 'function' && cb === undefined) {
|
|
|
|
cb = options;
|
|
|
|
options = {};
|
|
|
|
}
|
2018-12-07 14:54:29 +00:00
|
|
|
const modelTo = this.definition.modelTo;
|
|
|
|
const modelFrom = this.definition.modelFrom;
|
|
|
|
const fk = this.definition.keyTo;
|
|
|
|
const pk = this.definition.keyFrom;
|
|
|
|
const modelInstance = this.modelInstance;
|
2014-07-15 15:50:34 +00:00
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
const idName = this.definition.modelTo.definition.idName();
|
|
|
|
const filter = {};
|
2014-07-11 13:29:47 +00:00
|
|
|
filter.where = {};
|
2014-07-15 15:50:34 +00:00
|
|
|
filter.where[idName] = fkId;
|
2014-07-11 13:29:47 +00:00
|
|
|
filter.where[fk] = modelInstance[pk];
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2015-02-18 05:04:31 +00:00
|
|
|
cb = cb || utils.createPromiseCallback();
|
|
|
|
|
2014-07-27 07:38:50 +00:00
|
|
|
if (filter.where[fk] === undefined) {
|
|
|
|
// Foreign key is undefined
|
2015-02-18 05:04:31 +00:00
|
|
|
process.nextTick(cb);
|
|
|
|
return cb.promise;
|
2014-07-27 07:38:50 +00:00
|
|
|
}
|
2014-07-11 13:29:47 +00:00
|
|
|
this.definition.applyScope(modelInstance, filter);
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
modelTo.findOne(filter, options, function(err, inst) {
|
2014-06-15 22:53:58 +00:00
|
|
|
if (err) {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
if (!inst) {
|
2016-07-22 19:26:07 +00:00
|
|
|
err = new Error(g.f('No instance with {{id}} %s found for %s', fkId, modelTo.modelName));
|
2014-07-15 15:50:34 +00:00
|
|
|
err.statusCode = 404;
|
|
|
|
return cb(err);
|
2014-06-15 22:53:58 +00:00
|
|
|
}
|
|
|
|
// Check if the foreign key matches the primary key
|
2015-08-18 14:22:36 +00:00
|
|
|
if (inst[fk] != null && idEquals(inst[fk], modelInstance[pk])) {
|
2014-06-15 22:53:58 +00:00
|
|
|
cb(null, inst);
|
|
|
|
} else {
|
2016-07-22 19:26:07 +00:00
|
|
|
err = new Error(g.f('Key mismatch: %s.%s: %s, %s.%s: %s',
|
|
|
|
modelFrom.modelName, pk, modelInstance[pk], modelTo.modelName, fk, inst[fk]));
|
2014-07-15 15:50:34 +00:00
|
|
|
err.statusCode = 400;
|
|
|
|
cb(err);
|
2014-06-15 22:53:58 +00:00
|
|
|
}
|
|
|
|
});
|
2015-02-18 05:04:31 +00:00
|
|
|
return cb.promise;
|
2014-06-15 22:53:58 +00:00
|
|
|
};
|
|
|
|
|
2014-07-15 15:50:34 +00:00
|
|
|
/**
|
|
|
|
* Find a related item by foreign key
|
|
|
|
* @param {*} fkId The foreign key
|
2015-05-16 17:11:17 +00:00
|
|
|
* @param {Object} [options] Options
|
2014-07-15 15:50:34 +00:00
|
|
|
* @param {Function} cb The callback function
|
|
|
|
*/
|
2016-04-01 11:48:17 +00:00
|
|
|
HasMany.prototype.exists = function(fkId, options, cb) {
|
2015-05-16 17:11:17 +00:00
|
|
|
if (typeof options === 'function' && cb === undefined) {
|
|
|
|
cb = options;
|
|
|
|
options = {};
|
|
|
|
}
|
2018-12-07 14:54:29 +00:00
|
|
|
const fk = this.definition.keyTo;
|
|
|
|
const pk = this.definition.keyFrom;
|
|
|
|
const modelInstance = this.modelInstance;
|
2015-02-18 05:04:31 +00:00
|
|
|
cb = cb || utils.createPromiseCallback();
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
this.findById(fkId, function(err, inst) {
|
2014-06-15 22:53:58 +00:00
|
|
|
if (err) {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
if (!inst) {
|
2014-07-15 15:50:34 +00:00
|
|
|
return cb(null, false);
|
2014-06-15 22:53:58 +00:00
|
|
|
}
|
|
|
|
// Check if the foreign key matches the primary key
|
|
|
|
if (inst[fk] && inst[fk].toString() === modelInstance[pk].toString()) {
|
2014-07-15 15:50:34 +00:00
|
|
|
cb(null, true);
|
2014-06-15 22:53:58 +00:00
|
|
|
} else {
|
2014-07-15 15:50:34 +00:00
|
|
|
cb(null, false);
|
|
|
|
}
|
|
|
|
});
|
2015-02-18 05:04:31 +00:00
|
|
|
return cb.promise;
|
2014-07-15 15:50:34 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update a related item by foreign key
|
|
|
|
* @param {*} fkId The foreign key
|
2015-05-16 17:11:17 +00:00
|
|
|
* @param {Object} Changes to the data
|
|
|
|
* @param {Object} [options] Options
|
2014-07-15 15:50:34 +00:00
|
|
|
* @param {Function} cb The callback function
|
|
|
|
*/
|
2016-04-01 11:48:17 +00:00
|
|
|
HasMany.prototype.updateById = function(fkId, data, options, cb) {
|
2015-05-16 17:11:17 +00:00
|
|
|
if (typeof options === 'function' && cb === undefined) {
|
|
|
|
cb = options;
|
|
|
|
options = {};
|
|
|
|
}
|
2015-02-18 05:04:31 +00:00
|
|
|
cb = cb || utils.createPromiseCallback();
|
2018-12-07 14:54:29 +00:00
|
|
|
const fk = this.definition.keyTo;
|
2018-01-17 18:34:37 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
this.findById(fkId, options, function(err, inst) {
|
2014-07-15 15:50:34 +00:00
|
|
|
if (err) {
|
|
|
|
return cb && cb(err);
|
|
|
|
}
|
2018-01-17 18:34:37 +00:00
|
|
|
// Ensure Foreign Key cannot be changed!
|
2018-12-07 14:54:29 +00:00
|
|
|
const fkErr = preventFkOverride(inst, data, fk);
|
2018-01-17 18:34:37 +00:00
|
|
|
if (fkErr) return cb(fkErr);
|
2015-05-16 17:11:17 +00:00
|
|
|
inst.updateAttributes(data, options, cb);
|
2014-07-15 15:50:34 +00:00
|
|
|
});
|
2015-02-18 05:04:31 +00:00
|
|
|
return cb.promise;
|
2014-07-15 15:50:34 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete a related item by foreign key
|
|
|
|
* @param {*} fkId The foreign key
|
2015-05-16 17:11:17 +00:00
|
|
|
* @param {Object} [options] Options
|
2014-07-15 15:50:34 +00:00
|
|
|
* @param {Function} cb The callback function
|
|
|
|
*/
|
2016-04-01 11:48:17 +00:00
|
|
|
HasMany.prototype.destroyById = function(fkId, options, cb) {
|
2015-05-16 17:11:17 +00:00
|
|
|
if (typeof options === 'function' && cb === undefined) {
|
|
|
|
cb = options;
|
|
|
|
options = {};
|
|
|
|
}
|
2015-02-18 05:04:31 +00:00
|
|
|
cb = cb || utils.createPromiseCallback();
|
2018-12-07 14:54:29 +00:00
|
|
|
const self = this;
|
2015-05-16 17:11:17 +00:00
|
|
|
this.findById(fkId, options, function(err, inst) {
|
2014-06-15 22:53:58 +00:00
|
|
|
if (err) {
|
|
|
|
return cb(err);
|
|
|
|
}
|
2015-05-22 20:42:34 +00:00
|
|
|
self.removeFromCache(fkId);
|
2015-05-16 17:11:17 +00:00
|
|
|
inst.destroy(options, cb);
|
2014-06-15 22:53:58 +00:00
|
|
|
});
|
2015-02-18 05:04:31 +00:00
|
|
|
return cb.promise;
|
2014-06-15 22:53:58 +00:00
|
|
|
};
|
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
const throughKeys = function(definition) {
|
|
|
|
const modelThrough = definition.modelThrough;
|
|
|
|
const pk2 = definition.modelTo.definition.idName();
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2017-01-31 15:14:53 +00:00
|
|
|
let fk1, fk2;
|
2014-08-15 10:55:10 +00:00
|
|
|
if (typeof definition.polymorphic === 'object') { // polymorphic
|
2017-01-31 15:14:53 +00:00
|
|
|
fk1 = definition.keyTo;
|
2014-08-15 10:55:10 +00:00
|
|
|
if (definition.polymorphic.invert) {
|
2017-01-31 15:14:53 +00:00
|
|
|
fk2 = definition.polymorphic.foreignKey;
|
2014-08-15 10:55:10 +00:00
|
|
|
} else {
|
2017-01-31 15:14:53 +00:00
|
|
|
fk2 = definition.keyThrough;
|
2014-08-15 10:55:10 +00:00
|
|
|
}
|
2015-02-16 23:36:51 +00:00
|
|
|
} else if (definition.modelFrom === definition.modelTo) {
|
2015-04-24 09:52:50 +00:00
|
|
|
return findBelongsTo(modelThrough, definition.modelTo, pk2).
|
2016-04-01 11:48:17 +00:00
|
|
|
sort(function(fk1, fk2) {
|
2017-01-06 12:33:54 +00:00
|
|
|
// Fix for bug - https://github.com/strongloop/loopback-datasource-juggler/issues/571
|
|
|
|
// Make sure that first key is mapped to modelFrom
|
|
|
|
// & second key to modelTo. Order matters
|
2015-04-24 20:02:33 +00:00
|
|
|
return (definition.keyTo === fk1) ? -1 : 1;
|
|
|
|
});
|
2014-07-26 15:20:25 +00:00
|
|
|
} else {
|
2017-01-31 15:14:53 +00:00
|
|
|
fk1 = findBelongsTo(modelThrough, definition.modelFrom,
|
2018-06-12 07:13:32 +00:00
|
|
|
definition.keyFrom)[0];
|
2017-01-31 15:14:53 +00:00
|
|
|
fk2 = findBelongsTo(modelThrough, definition.modelTo, pk2)[0];
|
2014-07-26 15:20:25 +00:00
|
|
|
}
|
|
|
|
return [fk1, fk2];
|
2016-04-01 11:48:17 +00:00
|
|
|
};
|
2014-07-26 15:20:25 +00:00
|
|
|
|
2014-07-15 15:50:34 +00:00
|
|
|
/**
|
|
|
|
* Find a related item by foreign key
|
|
|
|
* @param {*} fkId The foreign key value
|
2015-05-16 17:11:17 +00:00
|
|
|
* @param {Object} [options] Options
|
2014-07-15 15:50:34 +00:00
|
|
|
* @param {Function} cb The callback function
|
|
|
|
*/
|
2016-04-01 11:48:17 +00:00
|
|
|
HasManyThrough.prototype.findById = function(fkId, options, cb) {
|
2015-05-16 17:11:17 +00:00
|
|
|
if (typeof options === 'function' && cb === undefined) {
|
|
|
|
cb = options;
|
|
|
|
options = {};
|
|
|
|
}
|
2018-12-07 14:54:29 +00:00
|
|
|
const self = this;
|
|
|
|
const modelTo = this.definition.modelTo;
|
|
|
|
const pk = this.definition.keyFrom;
|
|
|
|
const modelInstance = this.modelInstance;
|
|
|
|
const modelThrough = this.definition.modelThrough;
|
2014-07-15 15:50:34 +00:00
|
|
|
|
2015-02-18 05:04:31 +00:00
|
|
|
cb = cb || utils.createPromiseCallback();
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
self.exists(fkId, options, function(err, exists) {
|
2014-07-15 15:50:34 +00:00
|
|
|
if (err || !exists) {
|
|
|
|
if (!err) {
|
2016-07-22 19:26:07 +00:00
|
|
|
err = new Error(g.f('No relation found in %s' +
|
|
|
|
' for (%s.%s,%s.%s)',
|
2018-06-12 07:13:32 +00:00
|
|
|
modelThrough.modelName, self.definition.modelFrom.modelName,
|
|
|
|
modelInstance[pk], modelTo.modelName, fkId));
|
2014-07-15 15:50:34 +00:00
|
|
|
err.statusCode = 404;
|
|
|
|
}
|
|
|
|
return cb(err);
|
|
|
|
}
|
2016-04-01 11:48:17 +00:00
|
|
|
modelTo.findById(fkId, options, function(err, inst) {
|
2014-07-15 15:50:34 +00:00
|
|
|
if (err) {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
if (!inst) {
|
2016-07-22 19:26:07 +00:00
|
|
|
err = new Error(g.f('No instance with id %s found for %s', fkId, modelTo.modelName));
|
2014-07-15 15:50:34 +00:00
|
|
|
err.statusCode = 404;
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
cb(err, inst);
|
|
|
|
});
|
|
|
|
});
|
2015-02-18 05:04:31 +00:00
|
|
|
return cb.promise;
|
2014-07-15 15:50:34 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete a related item by foreign key
|
|
|
|
* @param {*} fkId The foreign key
|
2015-05-16 17:11:17 +00:00
|
|
|
* @param {Object} [options] Options
|
2014-07-15 15:50:34 +00:00
|
|
|
* @param {Function} cb The callback function
|
|
|
|
*/
|
2016-04-01 11:48:17 +00:00
|
|
|
HasManyThrough.prototype.destroyById = function(fkId, options, cb) {
|
2015-05-16 17:11:17 +00:00
|
|
|
if (typeof options === 'function' && cb === undefined) {
|
|
|
|
cb = options;
|
|
|
|
options = {};
|
|
|
|
}
|
2018-12-07 14:54:29 +00:00
|
|
|
const self = this;
|
|
|
|
const modelTo = this.definition.modelTo;
|
|
|
|
const pk = this.definition.keyFrom;
|
|
|
|
const modelInstance = this.modelInstance;
|
|
|
|
const modelThrough = this.definition.modelThrough;
|
2014-07-15 15:50:34 +00:00
|
|
|
|
2015-02-18 05:04:31 +00:00
|
|
|
cb = cb || utils.createPromiseCallback();
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
self.exists(fkId, options, function(err, exists) {
|
2014-07-15 15:50:34 +00:00
|
|
|
if (err || !exists) {
|
|
|
|
if (!err) {
|
2016-07-22 19:26:07 +00:00
|
|
|
err = new Error(g.f('No record found in %s for (%s.%s ,%s.%s)',
|
|
|
|
modelThrough.modelName, self.definition.modelFrom.modelName,
|
|
|
|
modelInstance[pk], modelTo.modelName, fkId));
|
2014-07-15 15:50:34 +00:00
|
|
|
err.statusCode = 404;
|
|
|
|
}
|
|
|
|
return cb(err);
|
2014-06-15 22:53:58 +00:00
|
|
|
}
|
2015-05-16 17:11:17 +00:00
|
|
|
self.remove(fkId, options, function(err) {
|
2016-04-01 11:48:17 +00:00
|
|
|
if (err) {
|
2014-07-15 15:50:34 +00:00
|
|
|
return cb(err);
|
|
|
|
}
|
2015-05-16 17:11:17 +00:00
|
|
|
modelTo.deleteById(fkId, options, cb);
|
2014-07-15 15:50:34 +00:00
|
|
|
});
|
2014-06-15 22:53:58 +00:00
|
|
|
});
|
2015-02-18 05:04:31 +00:00
|
|
|
return cb.promise;
|
2014-06-15 22:53:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Create an instance of the target model and connect it to the instance of
|
|
|
|
// the source model by creating an instance of the through model
|
2015-05-16 17:11:17 +00:00
|
|
|
HasManyThrough.prototype.create = function create(data, options, cb) {
|
|
|
|
if (typeof options === 'function' && cb === undefined) {
|
|
|
|
cb = options;
|
|
|
|
options = {};
|
|
|
|
}
|
2018-12-07 14:54:29 +00:00
|
|
|
const self = this;
|
|
|
|
const definition = this.definition;
|
|
|
|
const modelTo = definition.modelTo;
|
|
|
|
const modelThrough = definition.modelThrough;
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2015-05-16 17:11:17 +00:00
|
|
|
if (typeof data === 'function' && !cb) {
|
|
|
|
cb = data;
|
2014-06-15 22:53:58 +00:00
|
|
|
data = {};
|
|
|
|
}
|
2015-05-16 17:11:17 +00:00
|
|
|
cb = cb || utils.createPromiseCallback();
|
2014-06-15 22:53:58 +00:00
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
const modelInstance = this.modelInstance;
|
2014-06-16 07:36:12 +00:00
|
|
|
|
2014-06-15 22:53:58 +00:00
|
|
|
// First create the target model
|
2016-04-01 11:48:17 +00:00
|
|
|
modelTo.create(data, options, function(err, to) {
|
2014-06-15 22:53:58 +00:00
|
|
|
if (err) {
|
2015-05-16 17:11:17 +00:00
|
|
|
return cb(err, to);
|
2014-06-15 22:53:58 +00:00
|
|
|
}
|
2014-06-16 07:36:12 +00:00
|
|
|
// The primary key for the target model
|
2018-12-07 14:54:29 +00:00
|
|
|
const pk2 = definition.modelTo.definition.idName();
|
|
|
|
const keys = throughKeys(definition);
|
|
|
|
const fk1 = keys[0];
|
|
|
|
const fk2 = keys[1];
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2014-11-11 21:36:49 +00:00
|
|
|
function createRelation(to, next) {
|
2018-12-07 14:54:29 +00:00
|
|
|
const d = {}, q = {}, filter = {where: q};
|
2015-02-12 06:44:08 +00:00
|
|
|
d[fk1] = q[fk1] = modelInstance[definition.keyFrom];
|
|
|
|
d[fk2] = q[fk2] = to[pk2];
|
2014-11-11 21:36:49 +00:00
|
|
|
definition.applyProperties(modelInstance, d);
|
2015-02-12 06:44:08 +00:00
|
|
|
definition.applyScope(modelInstance, filter);
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2014-11-11 21:36:49 +00:00
|
|
|
// Then create the through model
|
2016-04-01 11:48:17 +00:00
|
|
|
modelThrough.findOrCreate(filter, d, options, function(e, through) {
|
2014-11-11 21:36:49 +00:00
|
|
|
if (e) {
|
|
|
|
// Undo creation of the target model
|
2016-04-01 11:48:17 +00:00
|
|
|
to.destroy(options, function() {
|
2014-11-11 21:36:49 +00:00
|
|
|
next(e);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
self.addToCache(to);
|
|
|
|
next(err, to);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// process array or single item
|
|
|
|
if (!Array.isArray(to))
|
2015-05-16 17:11:17 +00:00
|
|
|
createRelation(to, cb);
|
2014-11-11 21:36:49 +00:00
|
|
|
else
|
2015-05-16 17:11:17 +00:00
|
|
|
async.map(to, createRelation, cb);
|
2014-06-15 22:53:58 +00:00
|
|
|
});
|
2015-05-16 17:11:17 +00:00
|
|
|
return cb.promise;
|
2014-06-15 22:53:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add the target model instance to the 'hasMany' relation
|
|
|
|
* @param {Object|ID} acInst The actual instance or id value
|
2014-09-01 01:54:55 +00:00
|
|
|
* @param {Object} [data] Optional data object for the through model to be created
|
2015-05-16 17:11:17 +00:00
|
|
|
* @param {Object} [options] Options
|
|
|
|
* @param {Function} [cb] Callback function
|
2014-06-15 22:53:58 +00:00
|
|
|
*/
|
2016-04-01 11:48:17 +00:00
|
|
|
HasManyThrough.prototype.add = function(acInst, data, options, cb) {
|
2015-05-16 17:11:17 +00:00
|
|
|
if (typeof options === 'function' && cb === undefined) {
|
|
|
|
cb = options;
|
|
|
|
options = {};
|
|
|
|
}
|
2018-12-07 14:54:29 +00:00
|
|
|
const self = this;
|
|
|
|
const definition = this.definition;
|
|
|
|
const modelThrough = definition.modelThrough;
|
|
|
|
const pk1 = definition.keyFrom;
|
2014-06-15 22:53:58 +00:00
|
|
|
|
2014-08-31 13:40:18 +00:00
|
|
|
if (typeof data === 'function') {
|
2015-05-16 17:11:17 +00:00
|
|
|
cb = data;
|
2014-08-31 13:40:18 +00:00
|
|
|
data = {};
|
|
|
|
}
|
2018-12-07 14:54:29 +00:00
|
|
|
const query = {};
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2015-02-18 05:04:31 +00:00
|
|
|
data = data || {};
|
2015-05-16 17:11:17 +00:00
|
|
|
cb = cb || utils.createPromiseCallback();
|
2015-02-18 05:04:31 +00:00
|
|
|
|
2014-06-16 07:36:12 +00:00
|
|
|
// The primary key for the target model
|
2018-12-07 14:54:29 +00:00
|
|
|
const pk2 = definition.modelTo.definition.idName();
|
2014-06-16 07:36:12 +00:00
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
const keys = throughKeys(definition);
|
|
|
|
const fk1 = keys[0];
|
|
|
|
const fk2 = keys[1];
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2014-06-16 07:36:12 +00:00
|
|
|
query[fk1] = this.modelInstance[pk1];
|
2014-07-15 23:09:54 +00:00
|
|
|
query[fk2] = (acInst instanceof definition.modelTo) ? acInst[pk2] : acInst;
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
const filter = {where: query};
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2014-07-11 13:29:47 +00:00
|
|
|
definition.applyScope(this.modelInstance, filter);
|
2014-06-16 07:36:12 +00:00
|
|
|
|
|
|
|
data[fk1] = this.modelInstance[pk1];
|
2014-07-15 23:09:54 +00:00
|
|
|
data[fk2] = (acInst instanceof definition.modelTo) ? acInst[pk2] : acInst;
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2014-07-11 22:02:16 +00:00
|
|
|
definition.applyProperties(this.modelInstance, data);
|
2014-06-16 07:36:12 +00:00
|
|
|
|
2014-06-15 22:53:58 +00:00
|
|
|
// Create an instance of the through model
|
2015-05-16 17:11:17 +00:00
|
|
|
modelThrough.findOrCreate(filter, data, options, function(err, ac) {
|
2016-04-01 11:48:17 +00:00
|
|
|
if (!err) {
|
2014-06-21 18:44:33 +00:00
|
|
|
if (acInst instanceof definition.modelTo) {
|
|
|
|
self.addToCache(acInst);
|
|
|
|
}
|
|
|
|
}
|
2015-05-16 17:11:17 +00:00
|
|
|
cb(err, ac);
|
2014-06-21 18:44:33 +00:00
|
|
|
});
|
2015-05-16 17:11:17 +00:00
|
|
|
return cb.promise;
|
2014-06-15 22:53:58 +00:00
|
|
|
};
|
|
|
|
|
2014-07-15 15:50:34 +00:00
|
|
|
/**
|
|
|
|
* Check if the target model instance is related to the 'hasMany' relation
|
|
|
|
* @param {Object|ID} acInst The actual instance or id value
|
|
|
|
*/
|
2016-04-01 11:48:17 +00:00
|
|
|
HasManyThrough.prototype.exists = function(acInst, options, cb) {
|
2015-05-16 17:11:17 +00:00
|
|
|
if (typeof options === 'function' && cb === undefined) {
|
|
|
|
cb = options;
|
|
|
|
options = {};
|
|
|
|
}
|
2018-12-07 14:54:29 +00:00
|
|
|
const definition = this.definition;
|
|
|
|
const modelThrough = definition.modelThrough;
|
|
|
|
const pk1 = definition.keyFrom;
|
2014-07-15 15:50:34 +00:00
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
const query = {};
|
2014-07-15 15:50:34 +00:00
|
|
|
|
|
|
|
// The primary key for the target model
|
2018-12-07 14:54:29 +00:00
|
|
|
const pk2 = definition.modelTo.definition.idName();
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
const keys = throughKeys(definition);
|
|
|
|
const fk1 = keys[0];
|
|
|
|
const fk2 = keys[1];
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2014-07-15 15:50:34 +00:00
|
|
|
query[fk1] = this.modelInstance[pk1];
|
2014-07-15 23:09:54 +00:00
|
|
|
query[fk2] = (acInst instanceof definition.modelTo) ? acInst[pk2] : acInst;
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
const filter = {where: query};
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2014-07-26 19:11:25 +00:00
|
|
|
definition.applyScope(this.modelInstance, filter);
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2015-05-16 17:11:17 +00:00
|
|
|
cb = cb || utils.createPromiseCallback();
|
2015-02-18 05:04:31 +00:00
|
|
|
|
2015-05-16 17:11:17 +00:00
|
|
|
modelThrough.count(filter.where, options, function(err, ac) {
|
|
|
|
cb(err, ac > 0);
|
2014-07-15 15:50:34 +00:00
|
|
|
});
|
2015-05-16 17:11:17 +00:00
|
|
|
return cb.promise;
|
2014-07-15 15:50:34 +00:00
|
|
|
};
|
|
|
|
|
2014-06-15 22:53:58 +00:00
|
|
|
/**
|
|
|
|
* Remove the target model instance from the 'hasMany' relation
|
|
|
|
* @param {Object|ID) acInst The actual instance or id value
|
|
|
|
*/
|
2016-04-01 11:48:17 +00:00
|
|
|
HasManyThrough.prototype.remove = function(acInst, options, cb) {
|
2015-05-16 17:11:17 +00:00
|
|
|
if (typeof options === 'function' && cb === undefined) {
|
|
|
|
cb = options;
|
|
|
|
options = {};
|
|
|
|
}
|
2018-12-07 14:54:29 +00:00
|
|
|
const self = this;
|
|
|
|
const definition = this.definition;
|
|
|
|
const modelThrough = definition.modelThrough;
|
|
|
|
const pk1 = definition.keyFrom;
|
2014-06-15 22:53:58 +00:00
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
const query = {};
|
2014-06-21 18:44:33 +00:00
|
|
|
|
|
|
|
// The primary key for the target model
|
2018-12-07 14:54:29 +00:00
|
|
|
const pk2 = definition.modelTo.definition.idName();
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
const keys = throughKeys(definition);
|
|
|
|
const fk1 = keys[0];
|
|
|
|
const fk2 = keys[1];
|
2014-06-21 18:44:33 +00:00
|
|
|
|
|
|
|
query[fk1] = this.modelInstance[pk1];
|
2014-07-15 23:09:54 +00:00
|
|
|
query[fk2] = (acInst instanceof definition.modelTo) ? acInst[pk2] : acInst;
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
const filter = {where: query};
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2014-07-11 13:29:47 +00:00
|
|
|
definition.applyScope(this.modelInstance, filter);
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2015-05-16 17:11:17 +00:00
|
|
|
cb = cb || utils.createPromiseCallback();
|
2015-02-18 05:04:31 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
modelThrough.deleteAll(filter.where, options, function(err) {
|
2014-06-21 18:44:33 +00:00
|
|
|
if (!err) {
|
|
|
|
self.removeFromCache(query[fk2]);
|
|
|
|
}
|
2015-05-16 17:11:17 +00:00
|
|
|
cb(err);
|
2014-06-21 18:44:33 +00:00
|
|
|
});
|
2015-05-16 17:11:17 +00:00
|
|
|
return cb.promise;
|
2014-06-15 22:53:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Declare "belongsTo" relation that sets up a one-to-one connection with
|
|
|
|
* another model, such that each instance of the declaring model "belongs to"
|
|
|
|
* one instance of the other model.
|
|
|
|
*
|
|
|
|
* For example, if an application includes users and posts, and each post can
|
|
|
|
* be written by exactly one user. The following code specifies that `Post` has
|
|
|
|
* a reference called `author` to the `User` model via the `userId` property of
|
|
|
|
* `Post` as the foreign key.
|
|
|
|
* ```
|
|
|
|
* Post.belongsTo(User, {as: 'author', foreignKey: 'userId'});
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* This optional parameter default value is false, so the related object will
|
|
|
|
* be loaded from cache if available.
|
2015-02-24 10:36:13 +00:00
|
|
|
*
|
2017-04-01 09:13:22 +00:00
|
|
|
* @param {Object|String} modelToRef Reference to Model object to which you are
|
|
|
|
* creating the relation: model instance, model name, or name of relation to model.
|
2014-06-15 22:53:58 +00:00
|
|
|
* @options {Object} params Configuration parameters; see below.
|
|
|
|
* @property {String} as Name of the property in the referring model that
|
|
|
|
* corresponds to the foreign key field in the related model.
|
|
|
|
* @property {String} foreignKey Name of foreign key property.
|
2015-02-24 10:36:13 +00:00
|
|
|
*
|
2014-06-15 22:53:58 +00:00
|
|
|
*/
|
2017-04-01 09:13:22 +00:00
|
|
|
RelationDefinition.belongsTo = function(modelFrom, modelToRef, params) {
|
2018-12-07 14:54:29 +00:00
|
|
|
let modelTo, discriminator, polymorphic;
|
2014-06-15 22:53:58 +00:00
|
|
|
params = params || {};
|
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
let pkName, relationName, fk;
|
2014-07-26 10:47:55 +00:00
|
|
|
if (params.polymorphic) {
|
2017-04-01 09:13:22 +00:00
|
|
|
relationName = params.as || (typeof modelToRef === 'string' ? modelToRef : null);
|
|
|
|
polymorphic = normalizePolymorphic(params.polymorphic, relationName);
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2017-04-01 09:13:22 +00:00
|
|
|
modelTo = null; // will be looked-up dynamically
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2015-08-31 20:24:47 +00:00
|
|
|
pkName = params.primaryKey || params.idName || 'id';
|
2014-07-28 21:22:45 +00:00
|
|
|
fk = polymorphic.foreignKey;
|
|
|
|
discriminator = polymorphic.discriminator;
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2014-09-04 15:31:53 +00:00
|
|
|
if (polymorphic.idType) { // explicit key type
|
2016-08-19 17:46:59 +00:00
|
|
|
modelFrom.dataSource.defineProperty(modelFrom.modelName, fk, {type: polymorphic.idType, index: true});
|
2014-07-26 10:47:55 +00:00
|
|
|
} else { // try to use the same foreign key type as modelFrom
|
2015-08-31 20:24:47 +00:00
|
|
|
modelFrom.dataSource.defineForeignKey(modelFrom.modelName, fk, modelFrom.modelName, pkName);
|
2014-07-26 10:47:55 +00:00
|
|
|
}
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2016-08-19 17:46:59 +00:00
|
|
|
modelFrom.dataSource.defineProperty(modelFrom.modelName, discriminator, {type: 'string', index: true});
|
2014-07-26 10:47:55 +00:00
|
|
|
} else {
|
2017-04-01 09:13:22 +00:00
|
|
|
// relation is not polymorphic
|
|
|
|
normalizeRelationAs(params, modelToRef);
|
|
|
|
modelTo = lookupModelTo(modelFrom, modelToRef, params);
|
2015-08-31 20:24:47 +00:00
|
|
|
pkName = params.primaryKey || modelTo.dataSource.idName(modelTo.modelName) || 'id';
|
2014-07-28 21:22:45 +00:00
|
|
|
relationName = params.as || i8n.camelize(modelTo.modelName, true);
|
|
|
|
fk = params.foreignKey || relationName + 'Id';
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2015-08-31 20:24:47 +00:00
|
|
|
modelFrom.dataSource.defineForeignKey(modelFrom.modelName, fk, modelTo.modelName, pkName);
|
2014-07-26 10:47:55 +00:00
|
|
|
}
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
const definition = modelFrom.relations[relationName] = new RelationDefinition({
|
2014-06-15 22:53:58 +00:00
|
|
|
name: relationName,
|
|
|
|
type: RelationTypes.belongsTo,
|
|
|
|
modelFrom: modelFrom,
|
|
|
|
keyFrom: fk,
|
2015-08-31 20:24:47 +00:00
|
|
|
keyTo: pkName,
|
2014-07-21 20:39:06 +00:00
|
|
|
modelTo: modelTo,
|
2014-11-24 09:31:28 +00:00
|
|
|
multiple: false,
|
2014-07-22 20:09:29 +00:00
|
|
|
properties: params.properties,
|
|
|
|
scope: params.scope,
|
2014-08-15 10:55:10 +00:00
|
|
|
options: params.options,
|
2015-03-19 15:50:26 +00:00
|
|
|
polymorphic: polymorphic,
|
2016-04-01 11:48:17 +00:00
|
|
|
methods: params.methods,
|
2014-06-15 22:53:58 +00:00
|
|
|
});
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2014-06-15 22:53:58 +00:00
|
|
|
// Define a property for the scope so that we have 'this' for the scoped methods
|
|
|
|
Object.defineProperty(modelFrom.prototype, relationName, {
|
|
|
|
enumerable: true,
|
|
|
|
configurable: true,
|
|
|
|
get: function() {
|
2018-12-07 14:54:29 +00:00
|
|
|
const relation = new BelongsTo(definition, this);
|
|
|
|
const relationMethod = relation.related.bind(relation);
|
2017-07-19 13:31:21 +00:00
|
|
|
relationMethod.get = relation.get.bind(relation);
|
|
|
|
relationMethod.getAsync = function() {
|
|
|
|
deprecated(g.f('BelongsTo method "getAsync()" is deprecated, use "get()" instead.'));
|
|
|
|
return this.get.apply(this, arguments);
|
|
|
|
};
|
2014-08-20 14:46:54 +00:00
|
|
|
relationMethod.update = relation.update.bind(relation);
|
|
|
|
relationMethod.destroy = relation.destroy.bind(relation);
|
2015-02-24 10:36:13 +00:00
|
|
|
if (!polymorphic) {
|
2014-08-20 14:46:54 +00:00
|
|
|
relationMethod.create = relation.create.bind(relation);
|
|
|
|
relationMethod.build = relation.build.bind(relation);
|
2014-08-13 09:28:23 +00:00
|
|
|
relationMethod._targetClass = definition.modelTo.modelName;
|
2014-07-26 10:47:55 +00:00
|
|
|
}
|
2015-03-19 15:50:26 +00:00
|
|
|
bindRelationMethods(relation, relationMethod, definition);
|
2014-06-15 22:53:58 +00:00
|
|
|
return relationMethod;
|
2016-04-01 11:48:17 +00:00
|
|
|
},
|
2014-06-15 22:53:58 +00:00
|
|
|
});
|
2014-06-16 20:46:17 +00:00
|
|
|
|
2014-06-21 06:54:46 +00:00
|
|
|
// FIXME: [rfeng] Wrap the property into a function for remoting
|
2014-06-16 20:46:17 +00:00
|
|
|
// so that it can be accessed as /api/<model>/<id>/<belongsToRelationName>
|
|
|
|
// For example, /api/orders/1/customer
|
2018-12-07 14:54:29 +00:00
|
|
|
const fn = function() {
|
|
|
|
const f = this[relationName];
|
2014-06-16 20:46:17 +00:00
|
|
|
f.apply(this, arguments);
|
|
|
|
};
|
|
|
|
modelFrom.prototype['__get__' + relationName] = fn;
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2014-08-13 09:28:23 +00:00
|
|
|
return definition;
|
2014-06-15 22:53:58 +00:00
|
|
|
};
|
|
|
|
|
2015-05-16 17:11:17 +00:00
|
|
|
BelongsTo.prototype.create = function(targetModelData, options, cb) {
|
|
|
|
if (typeof options === 'function' && cb === undefined) {
|
|
|
|
cb = options;
|
|
|
|
options = {};
|
|
|
|
}
|
2018-12-07 14:54:29 +00:00
|
|
|
const self = this;
|
|
|
|
const modelTo = this.definition.modelTo;
|
|
|
|
const fk = this.definition.keyFrom;
|
|
|
|
const pk = this.definition.keyTo;
|
|
|
|
const modelInstance = this.modelInstance;
|
2014-06-15 22:53:58 +00:00
|
|
|
|
2014-06-27 06:38:04 +00:00
|
|
|
if (typeof targetModelData === 'function' && !cb) {
|
|
|
|
cb = targetModelData;
|
|
|
|
targetModelData = {};
|
|
|
|
}
|
2015-02-18 05:04:31 +00:00
|
|
|
cb = cb || utils.createPromiseCallback();
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2014-07-22 20:09:29 +00:00
|
|
|
this.definition.applyProperties(modelInstance, targetModelData || {});
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2015-05-16 17:11:17 +00:00
|
|
|
modelTo.create(targetModelData, options, function(err, targetModel) {
|
2016-04-01 11:48:17 +00:00
|
|
|
if (!err) {
|
2014-06-15 22:53:58 +00:00
|
|
|
modelInstance[fk] = targetModel[pk];
|
2014-11-24 11:20:38 +00:00
|
|
|
if (modelInstance.isNewRecord()) {
|
2014-08-15 09:28:25 +00:00
|
|
|
self.resetCache(targetModel);
|
|
|
|
cb && cb(err, targetModel);
|
2014-11-24 11:20:38 +00:00
|
|
|
} else {
|
2015-05-16 17:11:17 +00:00
|
|
|
modelInstance.save(options, function(err, inst) {
|
2014-11-24 11:20:38 +00:00
|
|
|
if (cb && err) return cb && cb(err);
|
|
|
|
self.resetCache(targetModel);
|
|
|
|
cb && cb(err, targetModel);
|
|
|
|
});
|
|
|
|
}
|
2014-06-15 22:53:58 +00:00
|
|
|
} else {
|
|
|
|
cb && cb(err);
|
|
|
|
}
|
|
|
|
});
|
2015-02-18 05:04:31 +00:00
|
|
|
return cb.promise;
|
2014-06-15 22:53:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
BelongsTo.prototype.build = function(targetModelData) {
|
2018-12-07 14:54:29 +00:00
|
|
|
const modelTo = this.definition.modelTo;
|
2014-07-22 20:09:29 +00:00
|
|
|
this.definition.applyProperties(this.modelInstance, targetModelData || {});
|
2014-06-15 22:53:58 +00:00
|
|
|
return new modelTo(targetModelData);
|
|
|
|
};
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
BelongsTo.prototype.update = function(targetModelData, options, cb) {
|
2015-05-16 17:11:17 +00:00
|
|
|
if (typeof options === 'function' && cb === undefined) {
|
|
|
|
cb = options;
|
|
|
|
options = {};
|
|
|
|
}
|
2015-02-18 05:04:31 +00:00
|
|
|
cb = cb || utils.createPromiseCallback();
|
2018-12-07 14:54:29 +00:00
|
|
|
const definition = this.definition;
|
|
|
|
const fk = definition.keyTo;
|
2018-01-17 18:34:37 +00:00
|
|
|
|
2015-05-16 17:11:17 +00:00
|
|
|
this.fetch(options, function(err, inst) {
|
2014-08-20 14:46:54 +00:00
|
|
|
if (inst instanceof ModelBaseClass) {
|
2018-01-17 18:34:37 +00:00
|
|
|
// Ensures Foreign Key cannot be changed!
|
2018-12-07 14:54:29 +00:00
|
|
|
const fkErr = preventFkOverride(inst, targetModelData, fk);
|
2018-01-17 18:34:37 +00:00
|
|
|
if (fkErr) return cb(fkErr);
|
2015-05-16 17:11:17 +00:00
|
|
|
inst.updateAttributes(targetModelData, options, cb);
|
2014-08-20 14:46:54 +00:00
|
|
|
} else {
|
2016-07-22 19:26:07 +00:00
|
|
|
cb(new Error(g.f('{{BelongsTo}} relation %s is empty', definition.name)));
|
2014-08-20 14:46:54 +00:00
|
|
|
}
|
|
|
|
});
|
2015-02-18 05:04:31 +00:00
|
|
|
return cb.promise;
|
2014-08-20 14:46:54 +00:00
|
|
|
};
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
BelongsTo.prototype.destroy = function(options, cb) {
|
2015-05-16 17:11:17 +00:00
|
|
|
if (typeof options === 'function' && cb === undefined) {
|
|
|
|
cb = options;
|
|
|
|
options = {};
|
|
|
|
}
|
2016-07-22 19:29:52 +00:00
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
const definition = this.definition;
|
|
|
|
const modelInstance = this.modelInstance;
|
|
|
|
const fk = definition.keyFrom;
|
2015-02-18 05:04:31 +00:00
|
|
|
|
|
|
|
cb = cb || utils.createPromiseCallback();
|
|
|
|
|
2015-05-16 17:11:17 +00:00
|
|
|
this.fetch(options, function(err, targetModel) {
|
2014-08-20 14:46:54 +00:00
|
|
|
if (targetModel instanceof ModelBaseClass) {
|
|
|
|
modelInstance[fk] = null;
|
2015-05-16 17:11:17 +00:00
|
|
|
modelInstance.save(options, function(err, targetModel) {
|
2014-08-20 14:46:54 +00:00
|
|
|
if (cb && err) return cb && cb(err);
|
|
|
|
cb && cb(err, targetModel);
|
|
|
|
});
|
|
|
|
} else {
|
2016-07-22 19:26:07 +00:00
|
|
|
cb(new Error(g.f('{{BelongsTo}} relation %s is empty', definition.name)));
|
2014-08-20 14:46:54 +00:00
|
|
|
}
|
|
|
|
});
|
2015-02-18 05:04:31 +00:00
|
|
|
return cb.promise;
|
2014-08-20 14:46:54 +00:00
|
|
|
};
|
|
|
|
|
2014-06-15 22:53:58 +00:00
|
|
|
/**
|
|
|
|
* Define the method for the belongsTo relation itself
|
|
|
|
* It will support one of the following styles:
|
2015-05-16 17:11:17 +00:00
|
|
|
* - order.customer(refresh, options, callback): Load the target model instance asynchronously
|
2014-06-15 22:53:58 +00:00
|
|
|
* - order.customer(customer): Synchronous setter of the target model instance
|
|
|
|
* - order.customer(): Synchronous getter of the target model instance
|
|
|
|
*
|
|
|
|
* @param refresh
|
|
|
|
* @param params
|
|
|
|
* @returns {*}
|
|
|
|
*/
|
2016-04-01 11:48:17 +00:00
|
|
|
BelongsTo.prototype.related = function(condOrRefresh, options, cb) {
|
2018-12-07 14:54:29 +00:00
|
|
|
const self = this;
|
|
|
|
const modelFrom = this.definition.modelFrom;
|
|
|
|
let modelTo = this.definition.modelTo;
|
|
|
|
const pk = this.definition.keyTo;
|
|
|
|
const fk = this.definition.keyFrom;
|
|
|
|
const modelInstance = this.modelInstance;
|
|
|
|
let discriminator;
|
|
|
|
let scopeQuery = null;
|
|
|
|
let newValue;
|
2015-05-16 17:11:17 +00:00
|
|
|
|
|
|
|
if ((condOrRefresh instanceof ModelBaseClass) &&
|
|
|
|
options === undefined && cb === undefined) {
|
|
|
|
// order.customer(customer)
|
|
|
|
newValue = condOrRefresh;
|
|
|
|
condOrRefresh = false;
|
|
|
|
} else if (typeof condOrRefresh === 'function' &&
|
|
|
|
options === undefined && cb === undefined) {
|
|
|
|
// order.customer(cb)
|
|
|
|
cb = condOrRefresh;
|
|
|
|
condOrRefresh = false;
|
|
|
|
} else if (typeof options === 'function' && cb === undefined) {
|
2015-05-18 16:34:25 +00:00
|
|
|
// order.customer(condOrRefresh, cb)
|
2015-05-16 17:11:17 +00:00
|
|
|
cb = options;
|
|
|
|
options = {};
|
|
|
|
}
|
|
|
|
if (!newValue) {
|
|
|
|
scopeQuery = condOrRefresh;
|
2014-06-15 22:53:58 +00:00
|
|
|
}
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2014-08-15 10:55:10 +00:00
|
|
|
if (typeof this.definition.polymorphic === 'object') {
|
|
|
|
discriminator = this.definition.polymorphic.discriminator;
|
|
|
|
}
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
let cachedValue;
|
2015-05-16 17:11:17 +00:00
|
|
|
if (!condOrRefresh) {
|
2014-06-21 18:44:33 +00:00
|
|
|
cachedValue = self.getCache();
|
2014-06-15 22:53:58 +00:00
|
|
|
}
|
2015-05-16 17:11:17 +00:00
|
|
|
if (newValue) { // acts as setter
|
|
|
|
modelInstance[fk] = newValue[pk];
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2014-08-15 10:55:10 +00:00
|
|
|
if (discriminator) {
|
2015-05-16 17:11:17 +00:00
|
|
|
modelInstance[discriminator] = newValue.constructor.modelName;
|
2014-08-15 10:55:10 +00:00
|
|
|
}
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2015-05-16 17:11:17 +00:00
|
|
|
this.definition.applyProperties(modelInstance, newValue);
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2015-05-16 17:11:17 +00:00
|
|
|
self.resetCache(newValue);
|
|
|
|
} else if (typeof cb === 'function') { // acts as async getter
|
2014-08-16 08:23:32 +00:00
|
|
|
if (discriminator) {
|
2018-12-07 14:54:29 +00:00
|
|
|
let modelToName = modelInstance[discriminator];
|
2014-07-26 10:47:55 +00:00
|
|
|
if (typeof modelToName !== 'string') {
|
2016-07-22 19:26:07 +00:00
|
|
|
throw new Error(g.f('{{Polymorphic}} model not found: `%s` not set', discriminator));
|
2014-07-26 10:47:55 +00:00
|
|
|
}
|
|
|
|
modelToName = modelToName.toLowerCase();
|
|
|
|
modelTo = lookupModel(modelFrom.dataSource.modelBuilder.models, modelToName);
|
|
|
|
if (!modelTo) {
|
2016-07-22 19:26:07 +00:00
|
|
|
throw new Error(g.f('{{Polymorphic}} model not found: `%s`', modelToName));
|
2014-07-26 10:47:55 +00:00
|
|
|
}
|
|
|
|
}
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2015-01-19 14:49:20 +00:00
|
|
|
if (cachedValue === undefined || !(cachedValue instanceof ModelBaseClass)) {
|
2018-12-07 14:54:29 +00:00
|
|
|
const query = {where: {}};
|
2014-07-22 20:09:29 +00:00
|
|
|
query.where[pk] = modelInstance[fk];
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2016-04-01 13:23:42 +00:00
|
|
|
if (query.where[pk] === undefined || query.where[pk] === null) {
|
2014-07-27 07:38:50 +00:00
|
|
|
// Foreign key is undefined
|
|
|
|
return process.nextTick(cb);
|
|
|
|
}
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2014-07-22 20:09:29 +00:00
|
|
|
this.definition.applyScope(modelInstance, query);
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2014-10-14 20:47:59 +00:00
|
|
|
if (scopeQuery) mergeQuery(query, scopeQuery);
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2014-10-14 20:47:59 +00:00
|
|
|
if (Array.isArray(query.fields) && query.fields.indexOf(pk) === -1) {
|
2016-04-01 11:48:17 +00:00
|
|
|
query.fields.push(pk); // always include the pk
|
2014-10-14 20:47:59 +00:00
|
|
|
}
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
modelTo.findOne(query, options, function(err, inst) {
|
2014-06-15 22:53:58 +00:00
|
|
|
if (err) {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
if (!inst) {
|
|
|
|
return cb(null, null);
|
|
|
|
}
|
|
|
|
// Check if the foreign key matches the primary key
|
2016-04-01 13:23:42 +00:00
|
|
|
if (inst[pk] != null && modelInstance[fk] != null &&
|
|
|
|
inst[pk].toString() === modelInstance[fk].toString()) {
|
2014-06-21 18:44:33 +00:00
|
|
|
self.resetCache(inst);
|
2014-06-15 22:53:58 +00:00
|
|
|
cb(null, inst);
|
|
|
|
} else {
|
2016-07-22 19:26:07 +00:00
|
|
|
err = new Error(g.f('Key mismatch: %s.%s: %s, %s.%s: %s',
|
|
|
|
self.definition.modelFrom.modelName, fk, modelInstance[fk],
|
|
|
|
modelTo.modelName, pk, inst[pk]));
|
2014-07-15 15:50:34 +00:00
|
|
|
err.statusCode = 400;
|
|
|
|
cb(err);
|
2014-06-15 22:53:58 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
return modelInstance[fk];
|
|
|
|
} else {
|
|
|
|
cb(null, cachedValue);
|
|
|
|
return cachedValue;
|
|
|
|
}
|
2015-05-16 17:11:17 +00:00
|
|
|
} else if (condOrRefresh === undefined) { // acts as sync getter
|
2014-06-27 06:38:04 +00:00
|
|
|
return cachedValue;
|
2014-06-15 22:53:58 +00:00
|
|
|
} else { // setter
|
2015-05-16 17:11:17 +00:00
|
|
|
modelInstance[fk] = newValue;
|
2014-06-21 18:44:33 +00:00
|
|
|
self.resetCache();
|
2014-06-15 22:53:58 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-02-18 05:04:31 +00:00
|
|
|
/**
|
|
|
|
* Define a Promise-based method for the belongsTo relation itself
|
|
|
|
* - order.customer.get(cb): Load the target model instance asynchronously
|
|
|
|
*
|
|
|
|
* @param {Function} cb Callback of the form function (err, inst)
|
|
|
|
* @returns {Promise | Undefined} returns promise if callback is omitted
|
|
|
|
*/
|
2017-07-19 13:31:21 +00:00
|
|
|
BelongsTo.prototype.get = function(options, cb) {
|
2015-05-16 17:11:17 +00:00
|
|
|
if (typeof options === 'function' && cb === undefined) {
|
|
|
|
cb = options;
|
|
|
|
options = {};
|
|
|
|
}
|
2015-02-18 05:04:31 +00:00
|
|
|
cb = cb || utils.createPromiseCallback();
|
2015-05-16 17:11:17 +00:00
|
|
|
this.related(true, options, cb);
|
2015-02-18 05:04:31 +00:00
|
|
|
return cb.promise;
|
2016-04-01 11:48:17 +00:00
|
|
|
};
|
2015-02-18 05:04:31 +00:00
|
|
|
|
2014-06-15 22:53:58 +00:00
|
|
|
/**
|
2014-06-19 18:13:24 +00:00
|
|
|
* A hasAndBelongsToMany relation creates a direct many-to-many connection with
|
|
|
|
* another model, with no intervening model. For example, if your application
|
|
|
|
* includes users and groups, with each group having many users and each user
|
|
|
|
* appearing in many groups, you could declare the models this way:
|
2014-06-15 22:53:58 +00:00
|
|
|
* ```
|
|
|
|
* User.hasAndBelongsToMany('groups', {model: Group, foreignKey: 'groupId'});
|
|
|
|
* ```
|
2015-02-24 10:36:13 +00:00
|
|
|
*
|
2017-04-01 09:13:22 +00:00
|
|
|
* @param {Object|String} modelToRef Reference to Model object to which you are
|
|
|
|
* creating the relation: model instance, model name, or name of relation to model.
|
2014-06-15 22:53:58 +00:00
|
|
|
* @options {Object} params Configuration parameters; see below.
|
2014-06-19 18:13:24 +00:00
|
|
|
* @property {String} as Name of the property in the referring model that
|
|
|
|
* corresponds to the foreign key field in the related model.
|
2014-06-15 22:53:58 +00:00
|
|
|
* @property {String} foreignKey Property name of foreign key field.
|
|
|
|
* @property {Object} model Model object
|
|
|
|
*/
|
2017-04-01 09:13:22 +00:00
|
|
|
RelationDefinition.hasAndBelongsToMany = function hasAndBelongsToMany(modelFrom, modelToRef, params) {
|
2014-06-15 22:53:58 +00:00
|
|
|
params = params || {};
|
2017-04-01 09:13:22 +00:00
|
|
|
normalizeRelationAs(params, modelToRef);
|
2018-12-07 14:54:29 +00:00
|
|
|
const modelTo = lookupModelTo(modelFrom, modelToRef, params, true);
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
const models = modelFrom.dataSource.modelBuilder.models;
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2014-06-15 22:53:58 +00:00
|
|
|
if (!params.through) {
|
2016-07-22 19:26:07 +00:00
|
|
|
if (params.polymorphic) throw new Error(g.f('{{Polymorphic}} relations need a through model'));
|
2017-04-05 16:42:21 +00:00
|
|
|
|
|
|
|
if (params.throughTable) {
|
|
|
|
params.through = modelFrom.dataSource.define(params.throughTable);
|
|
|
|
} else {
|
2018-12-07 14:54:29 +00:00
|
|
|
const name1 = modelFrom.modelName + modelTo.modelName;
|
|
|
|
const name2 = modelTo.modelName + modelFrom.modelName;
|
2017-04-05 16:42:21 +00:00
|
|
|
params.through = lookupModel(models, name1) || lookupModel(models, name2) ||
|
|
|
|
modelFrom.dataSource.define(name1);
|
|
|
|
}
|
2014-06-15 22:53:58 +00:00
|
|
|
}
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
const options = {as: params.as, through: params.through};
|
2014-07-26 19:32:24 +00:00
|
|
|
options.properties = params.properties;
|
|
|
|
options.scope = params.scope;
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2016-06-03 22:00:21 +00:00
|
|
|
// Forward relation options like "disableInclude"
|
|
|
|
options.options = params.options;
|
|
|
|
|
2014-07-28 09:12:20 +00:00
|
|
|
if (params.polymorphic) {
|
2018-12-07 14:54:29 +00:00
|
|
|
const relationName = params.as || i8n.camelize(modelTo.pluralModelName, true);
|
|
|
|
const polymorphic = normalizePolymorphic(params.polymorphic, relationName);
|
2014-07-28 09:12:20 +00:00
|
|
|
options.polymorphic = polymorphic; // pass through
|
2018-12-07 14:54:29 +00:00
|
|
|
const accessor = params.through.prototype[polymorphic.selector];
|
2014-07-26 12:54:54 +00:00
|
|
|
if (typeof accessor !== 'function') { // declare once
|
2017-04-01 09:13:22 +00:00
|
|
|
// use the name of the polymorphic selector, not modelTo
|
|
|
|
params.through.belongsTo(polymorphic.selector, {polymorphic: true});
|
2014-07-26 12:54:54 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
params.through.belongsTo(modelFrom);
|
|
|
|
}
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2014-06-15 22:53:58 +00:00
|
|
|
params.through.belongsTo(modelTo);
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2014-08-13 09:28:23 +00:00
|
|
|
return this.hasMany(modelFrom, modelTo, options);
|
2014-06-15 22:53:58 +00:00
|
|
|
};
|
2014-06-16 08:17:37 +00:00
|
|
|
|
|
|
|
/**
|
2014-06-19 18:13:24 +00:00
|
|
|
* A HasOne relation creates a one-to-one connection from modelFrom to modelTo.
|
|
|
|
* This relation indicates that each instance of a model contains or possesses
|
|
|
|
* one instance of another model. For example, each supplier in your application
|
|
|
|
* has only one account.
|
|
|
|
*
|
|
|
|
* @param {Function} modelFrom The declaring model class
|
2017-04-01 09:13:22 +00:00
|
|
|
* @param {Object|String} modelToRef Reference to Model object to which you are
|
|
|
|
* creating the relation: model instance, model name, or name of relation to model.
|
2014-06-19 18:13:24 +00:00
|
|
|
* @options {Object} params Configuration parameters; see below.
|
|
|
|
* @property {String} as Name of the property in the referring model that
|
|
|
|
* corresponds to the foreign key field in the related model.
|
|
|
|
* @property {String} foreignKey Property name of foreign key field.
|
|
|
|
* @property {Object} model Model object
|
2014-06-16 08:17:37 +00:00
|
|
|
*/
|
2017-04-01 09:13:22 +00:00
|
|
|
RelationDefinition.hasOne = function(modelFrom, modelToRef, params) {
|
2014-06-16 08:17:37 +00:00
|
|
|
params = params || {};
|
2017-04-01 09:13:22 +00:00
|
|
|
normalizeRelationAs(params, modelToRef);
|
2018-12-07 14:54:29 +00:00
|
|
|
const modelTo = lookupModelTo(modelFrom, modelToRef, params);
|
2014-06-16 08:17:37 +00:00
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
const pk = params.primaryKey || modelFrom.dataSource.idName(modelFrom.modelName) || 'id';
|
|
|
|
const relationName = params.as || i8n.camelize(modelTo.modelName, true);
|
2014-06-16 08:17:37 +00:00
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
let fk = params.foreignKey || i8n.camelize(modelFrom.modelName + '_id', true);
|
|
|
|
let discriminator, polymorphic;
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2014-07-28 09:12:20 +00:00
|
|
|
if (params.polymorphic) {
|
2017-04-01 09:13:22 +00:00
|
|
|
polymorphic = normalizePolymorphic(params.polymorphic, relationName);
|
2014-07-28 09:12:20 +00:00
|
|
|
fk = polymorphic.foreignKey;
|
|
|
|
discriminator = polymorphic.discriminator;
|
2014-07-26 13:20:46 +00:00
|
|
|
if (!params.through) {
|
2016-08-19 17:46:59 +00:00
|
|
|
modelTo.dataSource.defineProperty(modelTo.modelName, discriminator, {type: 'string', index: true});
|
2014-07-26 13:20:46 +00:00
|
|
|
}
|
|
|
|
}
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
const definition = modelFrom.relations[relationName] = new RelationDefinition({
|
2014-06-16 08:17:37 +00:00
|
|
|
name: relationName,
|
|
|
|
type: RelationTypes.hasOne,
|
|
|
|
modelFrom: modelFrom,
|
|
|
|
keyFrom: pk,
|
|
|
|
keyTo: fk,
|
2014-07-11 13:29:47 +00:00
|
|
|
modelTo: modelTo,
|
2014-11-24 09:31:28 +00:00
|
|
|
multiple: false,
|
2014-07-21 20:39:06 +00:00
|
|
|
properties: params.properties,
|
2014-11-20 11:27:04 +00:00
|
|
|
scope: params.scope,
|
2014-08-15 10:55:10 +00:00
|
|
|
options: params.options,
|
2015-03-19 15:50:26 +00:00
|
|
|
polymorphic: polymorphic,
|
2016-04-01 11:48:17 +00:00
|
|
|
methods: params.methods,
|
2014-06-16 08:17:37 +00:00
|
|
|
});
|
|
|
|
|
2015-08-11 20:30:15 +00:00
|
|
|
modelTo.dataSource.defineForeignKey(modelTo.modelName, fk, modelFrom.modelName, pk);
|
2014-06-16 08:17:37 +00:00
|
|
|
|
|
|
|
// Define a property for the scope so that we have 'this' for the scoped methods
|
|
|
|
Object.defineProperty(modelFrom.prototype, relationName, {
|
|
|
|
enumerable: true,
|
|
|
|
configurable: true,
|
|
|
|
get: function() {
|
2018-12-07 14:54:29 +00:00
|
|
|
const relation = new HasOne(definition, this);
|
|
|
|
const relationMethod = relation.related.bind(relation);
|
2017-07-19 13:31:21 +00:00
|
|
|
relationMethod.get = relation.get.bind(relation);
|
|
|
|
relationMethod.getAsync = function() {
|
|
|
|
deprecated(g.f('HasOne method "getAsync()" is deprecated, use "get()" instead.'));
|
|
|
|
return this.get.apply(this, arguments);
|
|
|
|
};
|
2014-06-16 08:17:37 +00:00
|
|
|
relationMethod.create = relation.create.bind(relation);
|
|
|
|
relationMethod.build = relation.build.bind(relation);
|
2014-08-20 13:54:47 +00:00
|
|
|
relationMethod.update = relation.update.bind(relation);
|
|
|
|
relationMethod.destroy = relation.destroy.bind(relation);
|
2014-08-13 09:28:23 +00:00
|
|
|
relationMethod._targetClass = definition.modelTo.modelName;
|
2015-03-19 15:50:26 +00:00
|
|
|
bindRelationMethods(relation, relationMethod, definition);
|
2014-06-16 08:17:37 +00:00
|
|
|
return relationMethod;
|
2016-04-01 11:48:17 +00:00
|
|
|
},
|
2014-06-16 08:17:37 +00:00
|
|
|
});
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2014-08-01 09:24:41 +00:00
|
|
|
// FIXME: [rfeng] Wrap the property into a function for remoting
|
|
|
|
// so that it can be accessed as /api/<model>/<id>/<hasOneRelationName>
|
|
|
|
// For example, /api/orders/1/customer
|
2015-01-30 07:26:11 +00:00
|
|
|
modelFrom.prototype['__get__' + relationName] = function() {
|
2018-12-07 14:54:29 +00:00
|
|
|
const f = this[relationName];
|
2014-08-01 09:24:41 +00:00
|
|
|
f.apply(this, arguments);
|
|
|
|
};
|
2015-01-30 07:26:11 +00:00
|
|
|
|
|
|
|
modelFrom.prototype['__create__' + relationName] = function() {
|
2018-12-07 14:54:29 +00:00
|
|
|
const f = this[relationName].create;
|
2015-01-30 07:26:11 +00:00
|
|
|
f.apply(this, arguments);
|
|
|
|
};
|
|
|
|
|
|
|
|
modelFrom.prototype['__update__' + relationName] = function() {
|
2018-12-07 14:54:29 +00:00
|
|
|
const f = this[relationName].update;
|
2015-01-30 07:26:11 +00:00
|
|
|
f.apply(this, arguments);
|
|
|
|
};
|
|
|
|
|
|
|
|
modelFrom.prototype['__destroy__' + relationName] = function() {
|
2018-12-07 14:54:29 +00:00
|
|
|
const f = this[relationName].destroy;
|
2015-01-30 07:26:11 +00:00
|
|
|
f.apply(this, arguments);
|
|
|
|
};
|
|
|
|
|
2014-08-13 09:28:23 +00:00
|
|
|
return definition;
|
2014-06-16 08:17:37 +00:00
|
|
|
};
|
|
|
|
|
2014-06-19 18:13:24 +00:00
|
|
|
/**
|
|
|
|
* Create a target model instance
|
|
|
|
* @param {Object} targetModelData The target model data
|
|
|
|
* @callback {Function} [cb] Callback function
|
|
|
|
* @param {String|Object} err Error string or object
|
|
|
|
* @param {Object} The newly created target model instance
|
|
|
|
*/
|
2016-04-01 11:48:17 +00:00
|
|
|
HasOne.prototype.create = function(targetModelData, options, cb) {
|
2015-05-16 17:11:17 +00:00
|
|
|
if (typeof options === 'function' && cb === undefined) {
|
2015-05-18 16:34:25 +00:00
|
|
|
// customer.profile.create(options, cb)
|
2015-05-16 17:11:17 +00:00
|
|
|
cb = options;
|
|
|
|
options = {};
|
|
|
|
}
|
2018-12-07 14:54:29 +00:00
|
|
|
const self = this;
|
|
|
|
const modelTo = this.definition.modelTo;
|
|
|
|
const fk = this.definition.keyTo;
|
|
|
|
const pk = this.definition.keyFrom;
|
|
|
|
const modelInstance = this.modelInstance;
|
2014-06-16 08:17:37 +00:00
|
|
|
|
2014-06-27 06:38:04 +00:00
|
|
|
if (typeof targetModelData === 'function' && !cb) {
|
|
|
|
cb = targetModelData;
|
|
|
|
targetModelData = {};
|
|
|
|
}
|
2014-06-16 08:17:37 +00:00
|
|
|
targetModelData = targetModelData || {};
|
2015-02-18 05:04:31 +00:00
|
|
|
cb = cb || utils.createPromiseCallback();
|
|
|
|
|
2014-06-16 08:17:37 +00:00
|
|
|
targetModelData[fk] = modelInstance[pk];
|
2018-12-07 14:54:29 +00:00
|
|
|
const query = {where: {}};
|
2014-07-11 13:29:47 +00:00
|
|
|
query.where[fk] = targetModelData[fk];
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2014-07-11 13:29:47 +00:00
|
|
|
this.definition.applyScope(modelInstance, query);
|
2014-07-11 22:02:16 +00:00
|
|
|
this.definition.applyProperties(modelInstance, targetModelData);
|
2015-02-04 06:54:28 +00:00
|
|
|
|
2015-05-16 17:11:17 +00:00
|
|
|
modelTo.findOrCreate(query, targetModelData, options,
|
|
|
|
function(err, targetModel, created) {
|
|
|
|
if (err) {
|
|
|
|
return cb && cb(err);
|
|
|
|
}
|
|
|
|
if (created) {
|
|
|
|
// Refresh the cache
|
|
|
|
self.resetCache(targetModel);
|
|
|
|
cb && cb(err, targetModel);
|
|
|
|
} else {
|
2016-07-22 19:26:07 +00:00
|
|
|
cb && cb(new Error(g.f(
|
|
|
|
'{{HasOne}} relation cannot create more than one instance of %s',
|
2019-12-03 09:09:16 +00:00
|
|
|
modelTo.modelName,
|
2018-07-16 06:46:25 +00:00
|
|
|
)));
|
2015-05-16 17:11:17 +00:00
|
|
|
}
|
|
|
|
});
|
2015-02-18 05:04:31 +00:00
|
|
|
return cb.promise;
|
2014-06-27 06:38:04 +00:00
|
|
|
};
|
|
|
|
|
2015-05-16 17:11:17 +00:00
|
|
|
HasOne.prototype.update = function(targetModelData, options, cb) {
|
|
|
|
if (typeof options === 'function' && cb === undefined) {
|
2015-05-18 16:34:25 +00:00
|
|
|
// customer.profile.update(data, cb)
|
2015-05-16 17:11:17 +00:00
|
|
|
cb = options;
|
|
|
|
options = {};
|
|
|
|
}
|
2015-02-18 05:04:31 +00:00
|
|
|
cb = cb || utils.createPromiseCallback();
|
2018-12-07 14:54:29 +00:00
|
|
|
const definition = this.definition;
|
|
|
|
const fk = this.definition.keyTo;
|
2014-08-20 14:46:54 +00:00
|
|
|
this.fetch(function(err, targetModel) {
|
|
|
|
if (targetModel instanceof ModelBaseClass) {
|
2018-01-17 18:34:37 +00:00
|
|
|
// Ensures Foreign Key cannot be changed!
|
2018-12-07 14:54:29 +00:00
|
|
|
const fkErr = preventFkOverride(targetModel, targetModelData, fk);
|
2018-01-17 18:34:37 +00:00
|
|
|
if (fkErr) return cb(fkErr);
|
2016-12-20 10:40:22 +00:00
|
|
|
targetModel.updateAttributes(targetModelData, options, cb);
|
2014-08-20 13:54:47 +00:00
|
|
|
} else {
|
2016-07-22 19:26:07 +00:00
|
|
|
cb(new Error(g.f('{{HasOne}} relation %s is empty', definition.name)));
|
2014-08-20 13:54:47 +00:00
|
|
|
}
|
|
|
|
});
|
2015-02-18 05:04:31 +00:00
|
|
|
return cb.promise;
|
2014-08-20 13:54:47 +00:00
|
|
|
};
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
HasOne.prototype.destroy = function(options, cb) {
|
2015-05-16 17:11:17 +00:00
|
|
|
if (typeof options === 'function' && cb === undefined) {
|
2015-05-18 16:34:25 +00:00
|
|
|
// customer.profile.destroy(cb)
|
2015-05-16 17:11:17 +00:00
|
|
|
cb = options;
|
|
|
|
options = {};
|
|
|
|
}
|
2015-02-18 05:04:31 +00:00
|
|
|
cb = cb || utils.createPromiseCallback();
|
2018-12-07 14:54:29 +00:00
|
|
|
const definition = this.definition;
|
2014-08-20 14:46:54 +00:00
|
|
|
this.fetch(function(err, targetModel) {
|
|
|
|
if (targetModel instanceof ModelBaseClass) {
|
2015-05-16 17:11:17 +00:00
|
|
|
targetModel.destroy(options, cb);
|
2014-08-20 13:58:45 +00:00
|
|
|
} else {
|
2016-07-22 19:26:07 +00:00
|
|
|
cb(new Error(g.f('{{HasOne}} relation %s is empty', definition.name)));
|
2014-08-20 13:58:45 +00:00
|
|
|
}
|
|
|
|
});
|
2015-02-18 05:04:31 +00:00
|
|
|
return cb.promise;
|
2014-08-20 13:54:47 +00:00
|
|
|
};
|
|
|
|
|
2014-06-27 06:38:04 +00:00
|
|
|
/**
|
|
|
|
* Create a target model instance
|
|
|
|
* @param {Object} targetModelData The target model data
|
|
|
|
* @callback {Function} [cb] Callback function
|
|
|
|
* @param {String|Object} err Error string or object
|
|
|
|
* @param {Object} The newly created target model instance
|
|
|
|
*/
|
2016-04-01 11:48:17 +00:00
|
|
|
HasMany.prototype.create = function(targetModelData, options, cb) {
|
2015-05-16 17:11:17 +00:00
|
|
|
if (typeof options === 'function' && cb === undefined) {
|
2015-05-18 16:34:25 +00:00
|
|
|
// customer.orders.create(data, cb)
|
2015-05-16 17:11:17 +00:00
|
|
|
cb = options;
|
|
|
|
options = {};
|
|
|
|
}
|
2018-12-07 14:54:29 +00:00
|
|
|
const self = this;
|
|
|
|
const modelTo = this.definition.modelTo;
|
|
|
|
const fk = this.definition.keyTo;
|
|
|
|
const pk = this.definition.keyFrom;
|
|
|
|
const modelInstance = this.modelInstance;
|
2014-11-08 02:01:18 +00:00
|
|
|
|
2014-06-27 06:38:04 +00:00
|
|
|
if (typeof targetModelData === 'function' && !cb) {
|
|
|
|
cb = targetModelData;
|
|
|
|
targetModelData = {};
|
|
|
|
}
|
2014-06-16 08:17:37 +00:00
|
|
|
targetModelData = targetModelData || {};
|
2015-02-18 05:04:31 +00:00
|
|
|
cb = cb || utils.createPromiseCallback();
|
2014-11-08 02:01:18 +00:00
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
const fkAndProps = function(item) {
|
2014-11-08 02:01:18 +00:00
|
|
|
item[fk] = modelInstance[pk];
|
|
|
|
self.definition.applyProperties(modelInstance, item);
|
|
|
|
};
|
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
const apply = function(data, fn) {
|
2014-11-08 02:01:18 +00:00
|
|
|
if (Array.isArray(data)) {
|
|
|
|
data.forEach(fn);
|
|
|
|
} else {
|
|
|
|
fn(data);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
apply(targetModelData, fkAndProps);
|
|
|
|
|
2015-05-16 17:11:17 +00:00
|
|
|
modelTo.create(targetModelData, options, function(err, targetModel) {
|
2016-04-01 11:48:17 +00:00
|
|
|
if (!err) {
|
2017-01-06 12:33:54 +00:00
|
|
|
// Refresh the cache
|
2014-11-08 02:01:18 +00:00
|
|
|
apply(targetModel, self.addToCache.bind(self));
|
2014-06-16 08:17:37 +00:00
|
|
|
cb && cb(err, targetModel);
|
|
|
|
} else {
|
|
|
|
cb && cb(err);
|
|
|
|
}
|
|
|
|
});
|
2015-02-18 05:04:31 +00:00
|
|
|
return cb.promise;
|
2014-06-16 08:17:37 +00:00
|
|
|
};
|
2014-06-19 18:13:24 +00:00
|
|
|
/**
|
|
|
|
* Build a target model instance
|
|
|
|
* @param {Object} targetModelData The target model data
|
|
|
|
* @returns {Object} The newly built target model instance
|
|
|
|
*/
|
2014-06-27 06:38:04 +00:00
|
|
|
HasMany.prototype.build = HasOne.prototype.build = function(targetModelData) {
|
2018-12-07 14:54:29 +00:00
|
|
|
const modelTo = this.definition.modelTo;
|
|
|
|
const pk = this.definition.keyFrom;
|
|
|
|
const fk = this.definition.keyTo;
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2014-06-16 08:17:37 +00:00
|
|
|
targetModelData = targetModelData || {};
|
|
|
|
targetModelData[fk] = this.modelInstance[pk];
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2014-07-11 22:02:16 +00:00
|
|
|
this.definition.applyProperties(this.modelInstance, targetModelData);
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2014-06-16 08:17:37 +00:00
|
|
|
return new modelTo(targetModelData);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Define the method for the hasOne relation itself
|
|
|
|
* It will support one of the following styles:
|
|
|
|
* - order.customer(refresh, callback): Load the target model instance asynchronously
|
|
|
|
* - order.customer(customer): Synchronous setter of the target model instance
|
|
|
|
* - order.customer(): Synchronous getter of the target model instance
|
|
|
|
*
|
2014-06-19 18:13:24 +00:00
|
|
|
* @param {Boolean} refresh Reload from the data source
|
|
|
|
* @param {Object|Function} params Query parameters
|
|
|
|
* @returns {Object}
|
2014-06-16 08:17:37 +00:00
|
|
|
*/
|
2016-04-01 11:48:17 +00:00
|
|
|
HasOne.prototype.related = function(condOrRefresh, options, cb) {
|
2018-12-07 14:54:29 +00:00
|
|
|
const self = this;
|
|
|
|
const modelTo = this.definition.modelTo;
|
|
|
|
const fk = this.definition.keyTo;
|
|
|
|
const pk = this.definition.keyFrom;
|
|
|
|
const definition = this.definition;
|
|
|
|
const modelInstance = this.modelInstance;
|
|
|
|
let newValue;
|
2015-05-16 17:11:17 +00:00
|
|
|
|
|
|
|
if ((condOrRefresh instanceof ModelBaseClass) &&
|
|
|
|
options === undefined && cb === undefined) {
|
|
|
|
// order.customer(customer)
|
|
|
|
newValue = condOrRefresh;
|
|
|
|
condOrRefresh = false;
|
|
|
|
} else if (typeof condOrRefresh === 'function' &&
|
|
|
|
options === undefined && cb === undefined) {
|
2015-05-18 16:34:25 +00:00
|
|
|
// customer.profile(cb)
|
2015-05-16 17:11:17 +00:00
|
|
|
cb = condOrRefresh;
|
|
|
|
condOrRefresh = false;
|
|
|
|
} else if (typeof options === 'function' && cb === undefined) {
|
2015-05-18 16:34:25 +00:00
|
|
|
// customer.profile(condOrRefresh, cb)
|
2015-05-16 17:11:17 +00:00
|
|
|
cb = options;
|
|
|
|
options = {};
|
2014-06-16 08:17:37 +00:00
|
|
|
}
|
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
let cachedValue;
|
2015-05-16 17:11:17 +00:00
|
|
|
if (!condOrRefresh) {
|
2014-06-21 18:44:33 +00:00
|
|
|
cachedValue = self.getCache();
|
2014-06-16 08:17:37 +00:00
|
|
|
}
|
2015-05-16 17:11:17 +00:00
|
|
|
if (newValue) { // acts as setter
|
|
|
|
newValue[fk] = modelInstance[pk];
|
|
|
|
self.resetCache(newValue);
|
|
|
|
} else if (typeof cb === 'function') { // acts as async getter
|
2014-06-16 08:17:37 +00:00
|
|
|
if (cachedValue === undefined) {
|
2018-12-07 14:54:29 +00:00
|
|
|
const query = {where: {}};
|
2014-06-16 08:17:37 +00:00
|
|
|
query.where[fk] = modelInstance[pk];
|
2014-07-11 13:29:47 +00:00
|
|
|
definition.applyScope(modelInstance, query);
|
2016-04-01 11:48:17 +00:00
|
|
|
modelTo.findOne(query, options, function(err, inst) {
|
2014-06-16 08:17:37 +00:00
|
|
|
if (err) {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
if (!inst) {
|
|
|
|
return cb(null, null);
|
|
|
|
}
|
|
|
|
// Check if the foreign key matches the primary key
|
2016-04-01 13:23:42 +00:00
|
|
|
if (inst[fk] != null && modelInstance[pk] != null &&
|
|
|
|
inst[fk].toString() === modelInstance[pk].toString()) {
|
2014-06-21 18:44:33 +00:00
|
|
|
self.resetCache(inst);
|
2014-06-16 08:17:37 +00:00
|
|
|
cb(null, inst);
|
|
|
|
} else {
|
2016-07-22 19:26:07 +00:00
|
|
|
err = new Error(g.f('Key mismatch: %s.%s: %s, %s.%s: %s',
|
|
|
|
self.definition.modelFrom.modelName, pk, modelInstance[pk],
|
|
|
|
modelTo.modelName, fk, inst[fk]));
|
2014-07-15 15:50:34 +00:00
|
|
|
err.statusCode = 400;
|
|
|
|
cb(err);
|
2014-06-16 08:17:37 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
return modelInstance[pk];
|
|
|
|
} else {
|
|
|
|
cb(null, cachedValue);
|
|
|
|
return cachedValue;
|
|
|
|
}
|
2015-05-16 17:11:17 +00:00
|
|
|
} else if (condOrRefresh === undefined) { // acts as sync getter
|
2014-06-27 06:38:04 +00:00
|
|
|
return cachedValue;
|
2014-06-16 08:17:37 +00:00
|
|
|
} else { // setter
|
2015-05-16 17:11:17 +00:00
|
|
|
newValue[fk] = modelInstance[pk];
|
2014-06-21 18:44:33 +00:00
|
|
|
self.resetCache();
|
2014-06-16 08:17:37 +00:00
|
|
|
}
|
|
|
|
};
|
2014-07-27 14:30:45 +00:00
|
|
|
|
2015-02-18 05:04:31 +00:00
|
|
|
/**
|
|
|
|
* Define a Promise-based method for the hasOne relation itself
|
2017-07-19 13:31:21 +00:00
|
|
|
* - order.customer.get(cb): Load the target model instance asynchronously
|
2015-02-18 05:04:31 +00:00
|
|
|
*
|
|
|
|
* @param {Function} cb Callback of the form function (err, inst)
|
|
|
|
* @returns {Promise | Undefined} Returns promise if cb is omitted
|
|
|
|
*/
|
2017-07-19 13:31:21 +00:00
|
|
|
HasOne.prototype.get = function(options, cb) {
|
2015-05-16 17:11:17 +00:00
|
|
|
if (typeof options === 'function' && cb === undefined) {
|
|
|
|
cb = options;
|
|
|
|
options = {};
|
|
|
|
}
|
2015-02-18 05:04:31 +00:00
|
|
|
cb = cb || utils.createPromiseCallback();
|
|
|
|
this.related(true, cb);
|
|
|
|
return cb.promise;
|
|
|
|
};
|
|
|
|
|
2017-04-01 09:13:22 +00:00
|
|
|
RelationDefinition.embedsOne = function(modelFrom, modelToRef, params) {
|
2014-08-19 20:10:35 +00:00
|
|
|
params = params || {};
|
2017-04-01 09:13:22 +00:00
|
|
|
normalizeRelationAs(params, modelToRef);
|
2018-12-07 14:54:29 +00:00
|
|
|
const modelTo = lookupModelTo(modelFrom, modelToRef, params);
|
2014-08-19 20:10:35 +00:00
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
const thisClassName = modelFrom.modelName;
|
|
|
|
const relationName = params.as || (i8n.camelize(modelTo.modelName, true) + 'Item');
|
|
|
|
let propertyName = params.property || i8n.camelize(modelTo.modelName, true);
|
|
|
|
const idName = modelTo.dataSource.idName(modelTo.modelName) || 'id';
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2014-08-19 20:10:35 +00:00
|
|
|
if (relationName === propertyName) {
|
|
|
|
propertyName = '_' + propertyName;
|
|
|
|
debug('EmbedsOne property cannot be equal to relation name: ' +
|
|
|
|
'forcing property %s for relation %s', propertyName, relationName);
|
|
|
|
}
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
const definition = modelFrom.relations[relationName] = new RelationDefinition({
|
2014-08-19 20:10:35 +00:00
|
|
|
name: relationName,
|
|
|
|
type: RelationTypes.embedsOne,
|
|
|
|
modelFrom: modelFrom,
|
|
|
|
keyFrom: propertyName,
|
|
|
|
keyTo: idName,
|
|
|
|
modelTo: modelTo,
|
|
|
|
multiple: false,
|
|
|
|
properties: params.properties,
|
|
|
|
scope: params.scope,
|
|
|
|
options: params.options,
|
2015-03-19 15:50:26 +00:00
|
|
|
embed: true,
|
2016-04-01 11:48:17 +00:00
|
|
|
methods: params.methods,
|
2014-08-19 20:10:35 +00:00
|
|
|
});
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
const opts = Object.assign(
|
2017-10-31 14:55:54 +00:00
|
|
|
params.options && params.options.property ? params.options.property : {},
|
2019-12-03 09:09:16 +00:00
|
|
|
{type: modelTo},
|
2017-10-31 14:55:54 +00:00
|
|
|
);
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2014-08-19 20:10:35 +00:00
|
|
|
if (params.default === true) {
|
|
|
|
opts.default = function() { return new modelTo(); };
|
|
|
|
} else if (typeof params.default === 'object') {
|
|
|
|
opts.default = (function(def) {
|
2014-08-20 21:22:47 +00:00
|
|
|
return function() {
|
|
|
|
return new modelTo(def);
|
|
|
|
};
|
2014-08-19 20:10:35 +00:00
|
|
|
}(params.default));
|
|
|
|
}
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2014-08-19 20:10:35 +00:00
|
|
|
modelFrom.dataSource.defineProperty(modelFrom.modelName, propertyName, opts);
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2014-08-19 20:10:35 +00:00
|
|
|
// validate the embedded instance
|
2014-08-19 20:15:30 +00:00
|
|
|
if (definition.options.validate !== false) {
|
2014-08-19 20:10:35 +00:00
|
|
|
modelFrom.validate(relationName, function(err) {
|
2018-12-07 14:54:29 +00:00
|
|
|
const inst = this[propertyName];
|
2014-08-19 20:10:35 +00:00
|
|
|
if (inst instanceof modelTo) {
|
|
|
|
if (!inst.isValid()) {
|
2018-12-07 14:54:29 +00:00
|
|
|
const first = Object.keys(inst.errors)[0];
|
|
|
|
const msg = 'is invalid: `' + first + '` ' + inst.errors[first];
|
2014-08-19 20:10:35 +00:00
|
|
|
this.errors.add(relationName, msg, 'invalid');
|
|
|
|
err(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2014-08-19 20:10:35 +00:00
|
|
|
// Define a property for the scope so that we have 'this' for the scoped methods
|
|
|
|
Object.defineProperty(modelFrom.prototype, relationName, {
|
|
|
|
enumerable: true,
|
|
|
|
configurable: true,
|
|
|
|
get: function() {
|
2018-12-07 14:54:29 +00:00
|
|
|
const relation = new EmbedsOne(definition, this);
|
|
|
|
const relationMethod = relation.related.bind(relation);
|
2014-08-19 20:10:35 +00:00
|
|
|
relationMethod.create = relation.create.bind(relation);
|
|
|
|
relationMethod.build = relation.build.bind(relation);
|
2014-08-20 13:37:40 +00:00
|
|
|
relationMethod.update = relation.update.bind(relation);
|
2014-08-19 20:10:35 +00:00
|
|
|
relationMethod.destroy = relation.destroy.bind(relation);
|
2014-09-04 19:51:59 +00:00
|
|
|
relationMethod.value = relation.embeddedValue.bind(relation);
|
2014-08-19 20:10:35 +00:00
|
|
|
relationMethod._targetClass = definition.modelTo.modelName;
|
2015-03-19 15:50:26 +00:00
|
|
|
bindRelationMethods(relation, relationMethod, definition);
|
2014-08-19 20:10:35 +00:00
|
|
|
return relationMethod;
|
2016-04-01 11:48:17 +00:00
|
|
|
},
|
2014-08-19 20:10:35 +00:00
|
|
|
});
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2014-08-19 20:10:35 +00:00
|
|
|
// FIXME: [rfeng] Wrap the property into a function for remoting
|
|
|
|
// so that it can be accessed as /api/<model>/<id>/<embedsOneRelationName>
|
|
|
|
// For example, /api/orders/1/customer
|
2015-03-21 15:14:47 +00:00
|
|
|
modelFrom.prototype['__get__' + relationName] = function() {
|
2018-12-07 14:54:29 +00:00
|
|
|
const f = this[relationName];
|
2014-08-19 20:10:35 +00:00
|
|
|
f.apply(this, arguments);
|
|
|
|
};
|
2015-03-21 15:14:47 +00:00
|
|
|
|
|
|
|
modelFrom.prototype['__create__' + relationName] = function() {
|
2018-12-07 14:54:29 +00:00
|
|
|
const f = this[relationName].create;
|
2015-03-21 15:14:47 +00:00
|
|
|
f.apply(this, arguments);
|
|
|
|
};
|
|
|
|
|
|
|
|
modelFrom.prototype['__update__' + relationName] = function() {
|
2018-12-07 14:54:29 +00:00
|
|
|
const f = this[relationName].update;
|
2015-03-21 15:14:47 +00:00
|
|
|
f.apply(this, arguments);
|
|
|
|
};
|
|
|
|
|
|
|
|
modelFrom.prototype['__destroy__' + relationName] = function() {
|
2018-12-07 14:54:29 +00:00
|
|
|
const f = this[relationName].destroy;
|
2015-03-21 15:14:47 +00:00
|
|
|
f.apply(this, arguments);
|
|
|
|
};
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2014-08-19 20:10:35 +00:00
|
|
|
return definition;
|
|
|
|
};
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
EmbedsOne.prototype.related = function(condOrRefresh, options, cb) {
|
2018-12-07 14:54:29 +00:00
|
|
|
const modelTo = this.definition.modelTo;
|
|
|
|
const modelInstance = this.modelInstance;
|
|
|
|
const propertyName = this.definition.keyFrom;
|
|
|
|
let newValue;
|
2015-05-16 17:11:17 +00:00
|
|
|
|
|
|
|
if ((condOrRefresh instanceof ModelBaseClass) &&
|
|
|
|
options === undefined && cb === undefined) {
|
|
|
|
// order.customer(customer)
|
|
|
|
newValue = condOrRefresh;
|
|
|
|
condOrRefresh = false;
|
|
|
|
} else if (typeof condOrRefresh === 'function' &&
|
|
|
|
options === undefined && cb === undefined) {
|
|
|
|
// order.customer(cb)
|
|
|
|
cb = condOrRefresh;
|
|
|
|
condOrRefresh = false;
|
|
|
|
} else if (typeof options === 'function' && cb === undefined) {
|
2015-05-18 16:34:25 +00:00
|
|
|
// order.customer(condOrRefresh, cb)
|
2015-05-16 17:11:17 +00:00
|
|
|
cb = options;
|
|
|
|
options = {};
|
2014-08-19 20:10:35 +00:00
|
|
|
}
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2015-05-16 17:11:17 +00:00
|
|
|
if (newValue) { // acts as setter
|
|
|
|
if (newValue instanceof modelTo) {
|
|
|
|
this.definition.applyProperties(modelInstance, newValue);
|
|
|
|
modelInstance.setAttribute(propertyName, newValue);
|
2014-08-19 20:10:35 +00:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
const embeddedInstance = this.embeddedValue();
|
2014-11-28 05:45:47 +00:00
|
|
|
|
|
|
|
if (embeddedInstance) {
|
|
|
|
embeddedInstance.__persisted = true;
|
|
|
|
}
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2015-05-16 17:11:17 +00:00
|
|
|
if (typeof cb === 'function') { // acts as async getter
|
2015-02-24 10:36:13 +00:00
|
|
|
process.nextTick(function() {
|
2014-08-19 20:10:35 +00:00
|
|
|
cb(null, embeddedInstance);
|
|
|
|
});
|
2015-05-16 17:11:17 +00:00
|
|
|
} else if (condOrRefresh === undefined) { // acts as sync getter
|
2014-08-19 20:10:35 +00:00
|
|
|
return embeddedInstance;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-05-31 07:44:17 +00:00
|
|
|
EmbedsOne.prototype.prepareEmbeddedInstance = function(inst) {
|
|
|
|
if (inst && inst.triggerParent !== 'function') {
|
2018-12-07 14:54:29 +00:00
|
|
|
const self = this;
|
|
|
|
const propertyName = this.definition.keyFrom;
|
|
|
|
const modelInstance = this.modelInstance;
|
2016-05-31 07:44:17 +00:00
|
|
|
if (this.definition.options.persistent) {
|
2018-12-07 14:54:29 +00:00
|
|
|
const pk = this.definition.keyTo;
|
2016-05-31 07:44:17 +00:00
|
|
|
inst.__persisted = !!inst[pk];
|
|
|
|
} else {
|
|
|
|
inst.__persisted = true;
|
|
|
|
}
|
|
|
|
inst.triggerParent = function(actionName, callback) {
|
|
|
|
if (actionName === 'save') {
|
2018-12-07 14:54:29 +00:00
|
|
|
const embeddedValue = self.embeddedValue();
|
2016-05-31 07:44:17 +00:00
|
|
|
modelInstance.updateAttribute(propertyName,
|
|
|
|
embeddedValue, function(err, modelInst) {
|
|
|
|
callback(err, err ? null : modelInst);
|
|
|
|
});
|
|
|
|
} else if (actionName === 'destroy') {
|
|
|
|
modelInstance.unsetAttribute(propertyName, true);
|
|
|
|
// cannot delete property completely the way save works. operator $unset needed like mongo
|
|
|
|
modelInstance.save(function(err, modelInst) {
|
|
|
|
callback(err, modelInst);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
process.nextTick(callback);
|
|
|
|
}
|
|
|
|
};
|
2018-12-07 14:54:29 +00:00
|
|
|
const originalTrigger = inst.trigger;
|
2016-05-31 07:44:17 +00:00
|
|
|
inst.trigger = function(actionName, work, data, callback) {
|
|
|
|
if (typeof work === 'function') {
|
2018-12-07 14:54:29 +00:00
|
|
|
const originalWork = work;
|
2016-05-31 07:44:17 +00:00
|
|
|
work = function(next) {
|
|
|
|
originalWork.call(this, function(done) {
|
|
|
|
inst.triggerParent(actionName, function(err, inst) {
|
|
|
|
next(done); // TODO [fabien] - error handling?
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
originalTrigger.call(this, actionName, work, data, callback);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-09-04 19:51:59 +00:00
|
|
|
EmbedsOne.prototype.embeddedValue = function(modelInstance) {
|
|
|
|
modelInstance = modelInstance || this.modelInstance;
|
2018-12-07 14:54:29 +00:00
|
|
|
const embeddedValue = modelInstance[this.definition.keyFrom];
|
2016-05-31 07:44:17 +00:00
|
|
|
this.prepareEmbeddedInstance(embeddedValue);
|
|
|
|
return embeddedValue;
|
2014-09-04 19:51:59 +00:00
|
|
|
};
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
EmbedsOne.prototype.create = function(targetModelData, options, cb) {
|
2015-05-16 17:11:17 +00:00
|
|
|
if (typeof options === 'function' && cb === undefined) {
|
2015-05-18 16:34:25 +00:00
|
|
|
// order.customer.create(data, cb)
|
2015-05-16 17:11:17 +00:00
|
|
|
cb = options;
|
|
|
|
options = {};
|
|
|
|
}
|
2018-12-07 14:54:29 +00:00
|
|
|
const modelTo = this.definition.modelTo;
|
|
|
|
const propertyName = this.definition.keyFrom;
|
|
|
|
const modelInstance = this.modelInstance;
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2014-08-19 20:10:35 +00:00
|
|
|
if (typeof targetModelData === 'function' && !cb) {
|
|
|
|
cb = targetModelData;
|
|
|
|
targetModelData = {};
|
|
|
|
}
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2014-08-19 20:10:35 +00:00
|
|
|
targetModelData = targetModelData || {};
|
2015-02-18 05:04:31 +00:00
|
|
|
cb = cb || utils.createPromiseCallback();
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
const inst = this.callScopeMethod('build', targetModelData);
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
const updateEmbedded = function(callback) {
|
2015-04-01 14:59:21 +00:00
|
|
|
if (modelInstance.isNewRecord()) {
|
|
|
|
modelInstance.setAttribute(propertyName, inst);
|
2015-05-16 17:11:17 +00:00
|
|
|
modelInstance.save(options, function(err) {
|
2016-04-12 12:20:25 +00:00
|
|
|
callback(err, err ? null : inst);
|
2015-04-01 14:59:21 +00:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
modelInstance.updateAttribute(propertyName,
|
2015-05-16 17:11:17 +00:00
|
|
|
inst, options, function(err) {
|
2016-04-12 12:20:25 +00:00
|
|
|
callback(err, err ? null : inst);
|
2016-04-01 11:48:17 +00:00
|
|
|
});
|
2015-04-01 14:59:21 +00:00
|
|
|
}
|
2014-09-07 11:10:23 +00:00
|
|
|
};
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2014-09-07 11:10:23 +00:00
|
|
|
if (this.definition.options.persistent) {
|
2015-05-16 17:11:17 +00:00
|
|
|
inst.save(options, function(err) { // will validate
|
2014-09-07 11:10:23 +00:00
|
|
|
if (err) return cb(err, inst);
|
2016-04-12 12:20:25 +00:00
|
|
|
updateEmbedded(cb);
|
2014-08-19 20:10:35 +00:00
|
|
|
});
|
2014-09-07 11:10:23 +00:00
|
|
|
} else {
|
2018-12-07 14:54:29 +00:00
|
|
|
const context = {
|
2016-04-12 12:20:25 +00:00
|
|
|
Model: modelTo,
|
|
|
|
instance: inst,
|
|
|
|
options: options || {},
|
|
|
|
hookState: {},
|
|
|
|
};
|
|
|
|
modelTo.notifyObserversOf('before save', context, function(err) {
|
|
|
|
if (err) {
|
|
|
|
return process.nextTick(function() {
|
|
|
|
cb(err);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-01-31 15:14:53 +00:00
|
|
|
err = inst.isValid() ? null : new ValidationError(inst);
|
2016-04-12 12:20:25 +00:00
|
|
|
if (err) {
|
|
|
|
process.nextTick(function() {
|
|
|
|
cb(err);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
updateEmbedded(function(err, inst) {
|
|
|
|
if (err) return cb(err);
|
|
|
|
context.instance = inst;
|
|
|
|
modelTo.notifyObserversOf('after save', context, function(err) {
|
|
|
|
cb(err, err ? null : inst);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2014-08-19 20:10:35 +00:00
|
|
|
}
|
2015-02-18 05:04:31 +00:00
|
|
|
return cb.promise;
|
2014-08-19 20:10:35 +00:00
|
|
|
};
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
EmbedsOne.prototype.build = function(targetModelData) {
|
2018-12-07 14:54:29 +00:00
|
|
|
const modelTo = this.definition.modelTo;
|
|
|
|
const modelInstance = this.modelInstance;
|
|
|
|
const propertyName = this.definition.keyFrom;
|
|
|
|
const forceId = this.definition.options.forceId;
|
|
|
|
const persistent = this.definition.options.persistent;
|
|
|
|
const connector = modelTo.dataSource.connector;
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2014-08-19 20:10:35 +00:00
|
|
|
targetModelData = targetModelData || {};
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2014-08-19 20:10:35 +00:00
|
|
|
this.definition.applyProperties(modelInstance, targetModelData);
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
const pk = this.definition.keyTo;
|
|
|
|
const pkProp = modelTo.definition.properties[pk];
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
let assignId = (forceId || targetModelData[pk] === undefined);
|
2014-09-07 11:24:05 +00:00
|
|
|
assignId = assignId && !persistent && (pkProp && pkProp.generated);
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2014-09-07 11:24:05 +00:00
|
|
|
if (assignId && typeof connector.generateId === 'function') {
|
2018-12-07 14:54:29 +00:00
|
|
|
const id = connector.generateId(modelTo.modelName, targetModelData, pk);
|
2016-04-01 11:48:17 +00:00
|
|
|
targetModelData[pk] = id;
|
2014-09-07 11:24:05 +00:00
|
|
|
}
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
const embeddedInstance = new modelTo(targetModelData);
|
2014-08-20 13:37:40 +00:00
|
|
|
modelInstance[propertyName] = embeddedInstance;
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2016-05-31 07:44:17 +00:00
|
|
|
this.prepareEmbeddedInstance(embeddedInstance);
|
|
|
|
|
2014-08-20 13:37:40 +00:00
|
|
|
return embeddedInstance;
|
|
|
|
};
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
EmbedsOne.prototype.update = function(targetModelData, options, cb) {
|
2015-05-16 17:11:17 +00:00
|
|
|
if (typeof options === 'function' && cb === undefined) {
|
2015-05-18 16:34:25 +00:00
|
|
|
// order.customer.update(data, cb)
|
2015-05-16 17:11:17 +00:00
|
|
|
cb = options;
|
|
|
|
options = {};
|
|
|
|
}
|
2016-04-12 12:20:25 +00:00
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
const modelTo = this.definition.modelTo;
|
|
|
|
const modelInstance = this.modelInstance;
|
|
|
|
const propertyName = this.definition.keyFrom;
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
const isInst = targetModelData instanceof ModelBaseClass;
|
|
|
|
const data = isInst ? targetModelData.toObject() : targetModelData;
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
const embeddedInstance = this.embeddedValue();
|
2014-08-20 13:37:40 +00:00
|
|
|
if (embeddedInstance instanceof modelTo) {
|
2015-02-18 05:04:31 +00:00
|
|
|
cb = cb || utils.createPromiseCallback();
|
2018-12-07 14:54:29 +00:00
|
|
|
const hookState = {};
|
|
|
|
let context = {
|
2016-04-12 12:20:25 +00:00
|
|
|
Model: modelTo,
|
|
|
|
currentInstance: embeddedInstance,
|
|
|
|
data: data,
|
|
|
|
options: options || {},
|
|
|
|
hookState: hookState,
|
|
|
|
};
|
|
|
|
modelTo.notifyObserversOf('before save', context, function(err) {
|
|
|
|
if (err) return cb(err);
|
|
|
|
|
|
|
|
embeddedInstance.setAttributes(context.data);
|
|
|
|
|
|
|
|
// TODO support async validations
|
|
|
|
if (!embeddedInstance.isValid()) {
|
|
|
|
return cb(new ValidationError(embeddedInstance));
|
|
|
|
}
|
|
|
|
|
|
|
|
modelInstance.save(function(err, inst) {
|
|
|
|
if (err) return cb(err);
|
|
|
|
|
|
|
|
context = {
|
|
|
|
Model: modelTo,
|
|
|
|
instance: inst ? inst[propertyName] : embeddedInstance,
|
|
|
|
options: options || {},
|
|
|
|
hookState: hookState,
|
|
|
|
};
|
|
|
|
modelTo.notifyObserversOf('after save', context, function(err) {
|
|
|
|
cb(err, context.instance);
|
|
|
|
});
|
2014-08-20 13:37:40 +00:00
|
|
|
});
|
2016-04-12 12:20:25 +00:00
|
|
|
});
|
2014-08-20 13:37:40 +00:00
|
|
|
} else if (!embeddedInstance && cb) {
|
2015-02-18 05:04:31 +00:00
|
|
|
return this.callScopeMethod('create', data, cb);
|
2014-08-20 13:37:40 +00:00
|
|
|
} else if (!embeddedInstance) {
|
2015-02-18 05:04:31 +00:00
|
|
|
return this.callScopeMethod('build', data);
|
2014-08-20 13:37:40 +00:00
|
|
|
}
|
2015-02-18 05:04:31 +00:00
|
|
|
return cb.promise;
|
2014-08-19 20:10:35 +00:00
|
|
|
};
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
EmbedsOne.prototype.destroy = function(options, cb) {
|
2015-05-16 17:11:17 +00:00
|
|
|
if (typeof options === 'function' && cb === undefined) {
|
2015-05-18 16:34:25 +00:00
|
|
|
// order.customer.destroy(cb)
|
2015-05-16 17:11:17 +00:00
|
|
|
cb = options;
|
|
|
|
options = {};
|
|
|
|
}
|
2016-04-12 12:20:25 +00:00
|
|
|
cb = cb || utils.createPromiseCallback();
|
2018-12-07 14:54:29 +00:00
|
|
|
const modelTo = this.definition.modelTo;
|
|
|
|
const modelInstance = this.modelInstance;
|
|
|
|
const propertyName = this.definition.keyFrom;
|
|
|
|
const embeddedInstance = modelInstance[propertyName];
|
2016-04-12 12:20:25 +00:00
|
|
|
|
|
|
|
if (!embeddedInstance) {
|
|
|
|
cb();
|
|
|
|
return cb.promise;
|
|
|
|
}
|
|
|
|
|
2014-08-19 20:10:35 +00:00
|
|
|
modelInstance.unsetAttribute(propertyName, true);
|
2016-04-12 12:20:25 +00:00
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
const context = {
|
2016-04-12 12:20:25 +00:00
|
|
|
Model: modelTo,
|
|
|
|
instance: embeddedInstance,
|
|
|
|
options: options || {},
|
|
|
|
hookState: {},
|
|
|
|
};
|
|
|
|
modelTo.notifyObserversOf('before delete', context, function(err) {
|
|
|
|
if (err) return cb(err);
|
|
|
|
modelInstance.save(function(err, result) {
|
|
|
|
if (err) return cb(err);
|
|
|
|
modelTo.notifyObserversOf('after delete', context, cb);
|
|
|
|
});
|
2014-08-20 21:22:47 +00:00
|
|
|
});
|
2016-04-12 12:20:25 +00:00
|
|
|
|
2015-02-18 05:04:31 +00:00
|
|
|
return cb.promise;
|
2014-08-19 20:10:35 +00:00
|
|
|
};
|
|
|
|
|
2017-04-01 09:13:22 +00:00
|
|
|
RelationDefinition.embedsMany = function embedsMany(modelFrom, modelToRef, params) {
|
2014-07-27 14:30:45 +00:00
|
|
|
params = params || {};
|
2017-04-01 09:13:22 +00:00
|
|
|
normalizeRelationAs(params, modelToRef);
|
2018-12-07 14:54:29 +00:00
|
|
|
const modelTo = lookupModelTo(modelFrom, modelToRef, params, true);
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
const thisClassName = modelFrom.modelName;
|
|
|
|
const relationName = params.as || (i8n.camelize(modelTo.modelName, true) + 'List');
|
|
|
|
let propertyName = params.property || i8n.camelize(modelTo.pluralModelName, true);
|
|
|
|
const idName = modelTo.dataSource.idName(modelTo.modelName) || 'id';
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2014-08-11 13:33:41 +00:00
|
|
|
if (relationName === propertyName) {
|
|
|
|
propertyName = '_' + propertyName;
|
|
|
|
debug('EmbedsMany property cannot be equal to relation name: ' +
|
|
|
|
'forcing property %s for relation %s', propertyName, relationName);
|
|
|
|
}
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
const definition = modelFrom.relations[relationName] = new RelationDefinition({
|
2014-07-27 14:30:45 +00:00
|
|
|
name: relationName,
|
|
|
|
type: RelationTypes.embedsMany,
|
|
|
|
modelFrom: modelFrom,
|
2014-08-11 13:33:41 +00:00
|
|
|
keyFrom: propertyName,
|
|
|
|
keyTo: idName,
|
2014-07-27 14:30:45 +00:00
|
|
|
modelTo: modelTo,
|
|
|
|
multiple: true,
|
|
|
|
properties: params.properties,
|
|
|
|
scope: params.scope,
|
|
|
|
options: params.options,
|
2016-04-01 11:48:17 +00:00
|
|
|
embed: true,
|
2014-07-27 14:30:45 +00:00
|
|
|
});
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
const opts = Object.assign(
|
2017-10-31 14:55:54 +00:00
|
|
|
params.options && params.options.property ? params.options.property : {},
|
2018-04-13 17:17:40 +00:00
|
|
|
params.options && params.options.omitDefaultEmbeddedItem ? {type: [modelTo]} :
|
2018-06-12 07:13:32 +00:00
|
|
|
{
|
|
|
|
type: [modelTo],
|
|
|
|
default: function() { return []; },
|
2019-12-03 09:09:16 +00:00
|
|
|
},
|
2017-10-31 14:55:54 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
modelFrom.dataSource.defineProperty(modelFrom.modelName, propertyName, opts);
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2014-08-30 09:43:36 +00:00
|
|
|
if (typeof modelTo.dataSource.connector.generateId !== 'function') {
|
2014-09-07 10:17:42 +00:00
|
|
|
modelFrom.validate(propertyName, function(err) {
|
2018-12-07 14:54:29 +00:00
|
|
|
const self = this;
|
|
|
|
const embeddedList = this[propertyName] || [];
|
|
|
|
let hasErrors = false;
|
2014-09-07 10:17:42 +00:00
|
|
|
embeddedList.forEach(function(item, idx) {
|
|
|
|
if (item instanceof modelTo && item[idName] == undefined) {
|
|
|
|
hasErrors = true;
|
2018-12-07 14:54:29 +00:00
|
|
|
let msg = 'contains invalid item at index `' + idx + '`:';
|
2014-09-07 10:17:42 +00:00
|
|
|
msg += ' `' + idName + '` is blank';
|
|
|
|
self.errors.add(propertyName, msg, 'invalid');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
if (hasErrors) err(false);
|
|
|
|
});
|
2014-08-30 09:43:36 +00:00
|
|
|
}
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2014-07-29 11:57:49 +00:00
|
|
|
if (!params.polymorphic) {
|
2014-08-11 13:33:41 +00:00
|
|
|
modelFrom.validate(propertyName, function(err) {
|
2018-12-07 14:54:29 +00:00
|
|
|
const embeddedList = this[propertyName] || [];
|
|
|
|
const ids = embeddedList.map(function(m) { return m[idName] && m[idName].toString(); }); // mongodb
|
2018-10-03 22:17:08 +00:00
|
|
|
if (idsHaveDuplicates(ids)) {
|
2014-08-29 19:30:42 +00:00
|
|
|
this.errors.add(propertyName, 'contains duplicate `' + idName + '`', 'uniqueness');
|
2014-07-29 11:57:49 +00:00
|
|
|
err(false);
|
|
|
|
}
|
2016-08-19 17:46:59 +00:00
|
|
|
}, {code: 'uniqueness'});
|
2014-07-29 11:57:49 +00:00
|
|
|
}
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2014-07-27 14:54:01 +00:00
|
|
|
// validate all embedded items
|
2014-08-19 20:15:30 +00:00
|
|
|
if (definition.options.validate !== false) {
|
2014-08-11 13:33:41 +00:00
|
|
|
modelFrom.validate(propertyName, function(err) {
|
2018-12-07 14:54:29 +00:00
|
|
|
const self = this;
|
|
|
|
const embeddedList = this[propertyName] || [];
|
|
|
|
let hasErrors = false;
|
2014-09-07 10:17:42 +00:00
|
|
|
embeddedList.forEach(function(item, idx) {
|
2014-07-27 14:54:01 +00:00
|
|
|
if (item instanceof modelTo) {
|
|
|
|
if (!item.isValid()) {
|
|
|
|
hasErrors = true;
|
2018-12-07 14:54:29 +00:00
|
|
|
const id = item[idName];
|
|
|
|
const first = Object.keys(item.errors)[0];
|
2017-01-31 15:14:53 +00:00
|
|
|
let msg = id ?
|
|
|
|
'contains invalid item: `' + id + '`' :
|
|
|
|
'contains invalid item at index `' + idx + '`';
|
2014-08-19 20:10:35 +00:00
|
|
|
msg += ' (`' + first + '` ' + item.errors[first] + ')';
|
2014-08-11 13:33:41 +00:00
|
|
|
self.errors.add(propertyName, msg, 'invalid');
|
2014-07-27 14:54:01 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
hasErrors = true;
|
2014-08-29 19:30:42 +00:00
|
|
|
self.errors.add(propertyName, 'contains invalid item', 'invalid');
|
2014-07-27 14:54:01 +00:00
|
|
|
}
|
2014-07-30 13:01:55 +00:00
|
|
|
});
|
2014-07-27 14:54:01 +00:00
|
|
|
if (hasErrors) err(false);
|
|
|
|
});
|
|
|
|
}
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
const scopeMethods = {
|
2014-07-27 14:30:45 +00:00
|
|
|
findById: scopeMethod(definition, 'findById'),
|
|
|
|
destroy: scopeMethod(definition, 'destroyById'),
|
|
|
|
updateById: scopeMethod(definition, 'updateById'),
|
2014-07-29 08:51:33 +00:00
|
|
|
exists: scopeMethod(definition, 'exists'),
|
2014-07-29 15:43:30 +00:00
|
|
|
add: scopeMethod(definition, 'add'),
|
|
|
|
remove: scopeMethod(definition, 'remove'),
|
2014-07-29 08:51:33 +00:00
|
|
|
get: scopeMethod(definition, 'get'),
|
|
|
|
set: scopeMethod(definition, 'set'),
|
2014-07-29 15:43:30 +00:00
|
|
|
unset: scopeMethod(definition, 'unset'),
|
2014-09-04 19:51:59 +00:00
|
|
|
at: scopeMethod(definition, 'at'),
|
2016-04-01 11:48:17 +00:00
|
|
|
value: scopeMethod(definition, 'embeddedValue'),
|
2014-07-27 14:30:45 +00:00
|
|
|
};
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
const findByIdFunc = scopeMethods.findById;
|
2014-08-11 13:33:41 +00:00
|
|
|
modelFrom.prototype['__findById__' + relationName] = findByIdFunc;
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
const destroyByIdFunc = scopeMethods.destroy;
|
2014-08-11 13:33:41 +00:00
|
|
|
modelFrom.prototype['__destroyById__' + relationName] = destroyByIdFunc;
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
const updateByIdFunc = scopeMethods.updateById;
|
2014-08-11 13:33:41 +00:00
|
|
|
modelFrom.prototype['__updateById__' + relationName] = updateByIdFunc;
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
const addFunc = scopeMethods.add;
|
2014-08-11 13:33:41 +00:00
|
|
|
modelFrom.prototype['__link__' + relationName] = addFunc;
|
2014-07-29 15:43:30 +00:00
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
const removeFunc = scopeMethods.remove;
|
2014-08-11 13:33:41 +00:00
|
|
|
modelFrom.prototype['__unlink__' + relationName] = removeFunc;
|
2014-07-27 14:30:45 +00:00
|
|
|
|
|
|
|
scopeMethods.create = scopeMethod(definition, 'create');
|
|
|
|
scopeMethods.build = scopeMethod(definition, 'build');
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2014-07-29 20:59:44 +00:00
|
|
|
scopeMethods.related = scopeMethod(definition, 'related'); // bound to definition
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2015-03-24 14:35:56 +00:00
|
|
|
if (!definition.options.persistent) {
|
|
|
|
scopeMethods.destroyAll = scopeMethod(definition, 'destroyAll');
|
|
|
|
}
|
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
const customMethods = extendScopeMethods(definition, scopeMethods, params.scopeMethods);
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
for (let i = 0; i < customMethods.length; i++) {
|
|
|
|
const methodName = customMethods[i];
|
|
|
|
const method = scopeMethods[methodName];
|
2014-07-30 14:46:05 +00:00
|
|
|
if (typeof method === 'function' && method.shared === true) {
|
2014-08-11 13:33:41 +00:00
|
|
|
modelFrom.prototype['__' + methodName + '__' + relationName] = method;
|
2014-07-30 14:46:05 +00:00
|
|
|
}
|
2018-12-07 14:43:40 +00:00
|
|
|
}
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2014-07-27 14:30:45 +00:00
|
|
|
// Mix the property and scoped methods into the prototype class
|
2018-12-07 14:54:29 +00:00
|
|
|
const scopeDefinition = defineScope(modelFrom.prototype, modelTo, relationName, function() {
|
2014-07-27 14:30:45 +00:00
|
|
|
return {};
|
2014-08-08 16:25:49 +00:00
|
|
|
}, scopeMethods, definition.options);
|
2014-07-27 14:30:45 +00:00
|
|
|
|
2014-07-29 20:59:44 +00:00
|
|
|
scopeDefinition.related = scopeMethods.related;
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2014-08-13 09:28:23 +00:00
|
|
|
return definition;
|
2014-07-27 14:30:45 +00:00
|
|
|
};
|
|
|
|
|
2014-08-15 13:12:02 +00:00
|
|
|
EmbedsMany.prototype.prepareEmbeddedInstance = function(inst) {
|
|
|
|
if (inst && inst.triggerParent !== 'function') {
|
2018-12-07 14:54:29 +00:00
|
|
|
const self = this;
|
|
|
|
const propertyName = this.definition.keyFrom;
|
|
|
|
const modelInstance = this.modelInstance;
|
2014-09-12 22:03:08 +00:00
|
|
|
if (this.definition.options.persistent) {
|
2018-12-07 14:54:29 +00:00
|
|
|
const pk = this.definition.keyTo;
|
2016-04-01 11:48:17 +00:00
|
|
|
inst.__persisted = !!inst[pk];
|
2014-09-12 22:03:08 +00:00
|
|
|
} else {
|
2016-04-01 11:48:17 +00:00
|
|
|
inst.__persisted = true;
|
2014-09-12 22:03:08 +00:00
|
|
|
}
|
2014-08-15 13:12:02 +00:00
|
|
|
inst.triggerParent = function(actionName, callback) {
|
|
|
|
if (actionName === 'save' || actionName === 'destroy') {
|
2018-12-07 14:54:29 +00:00
|
|
|
const embeddedList = self.embeddedList();
|
2014-08-15 13:12:02 +00:00
|
|
|
if (actionName === 'destroy') {
|
2018-12-07 14:54:29 +00:00
|
|
|
const index = embeddedList.indexOf(inst);
|
2014-08-15 13:12:02 +00:00
|
|
|
if (index > -1) embeddedList.splice(index, 1);
|
|
|
|
}
|
|
|
|
modelInstance.updateAttribute(propertyName,
|
|
|
|
embeddedList, function(err, modelInst) {
|
2016-04-01 11:48:17 +00:00
|
|
|
callback(err, err ? null : modelInst);
|
|
|
|
});
|
2014-08-15 13:12:02 +00:00
|
|
|
} else {
|
|
|
|
process.nextTick(callback);
|
|
|
|
}
|
|
|
|
};
|
2018-12-07 14:54:29 +00:00
|
|
|
const originalTrigger = inst.trigger;
|
2014-08-15 13:12:02 +00:00
|
|
|
inst.trigger = function(actionName, work, data, callback) {
|
|
|
|
if (typeof work === 'function') {
|
2018-12-07 14:54:29 +00:00
|
|
|
const originalWork = work;
|
2014-08-15 13:12:02 +00:00
|
|
|
work = function(next) {
|
|
|
|
originalWork.call(this, function(done) {
|
|
|
|
inst.triggerParent(actionName, function(err, inst) {
|
|
|
|
next(done); // TODO [fabien] - error handling?
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
originalTrigger.call(this, actionName, work, data, callback);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-02-24 10:36:13 +00:00
|
|
|
EmbedsMany.prototype.embeddedList =
|
2016-04-01 13:23:42 +00:00
|
|
|
EmbedsMany.prototype.embeddedValue = function(modelInstance) {
|
|
|
|
modelInstance = modelInstance || this.modelInstance;
|
2018-12-07 14:54:29 +00:00
|
|
|
const embeddedList = modelInstance[this.definition.keyFrom] || [];
|
2016-04-01 13:23:42 +00:00
|
|
|
embeddedList.forEach(this.prepareEmbeddedInstance.bind(this));
|
|
|
|
return embeddedList;
|
|
|
|
};
|
2014-08-15 13:12:02 +00:00
|
|
|
|
2015-05-16 17:11:17 +00:00
|
|
|
EmbedsMany.prototype.related = function(receiver, scopeParams, condOrRefresh, options, cb) {
|
2018-12-07 14:54:29 +00:00
|
|
|
const modelTo = this.definition.modelTo;
|
|
|
|
const modelInstance = this.modelInstance;
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
let actualCond = {};
|
2015-05-16 17:11:17 +00:00
|
|
|
|
|
|
|
if (typeof condOrRefresh === 'function' &&
|
|
|
|
options === undefined && cb === undefined) {
|
2015-05-18 16:34:25 +00:00
|
|
|
// customer.emails(receiver, scopeParams, cb)
|
2014-07-27 14:30:45 +00:00
|
|
|
cb = condOrRefresh;
|
2015-05-16 17:11:17 +00:00
|
|
|
condOrRefresh = false;
|
|
|
|
} else if (typeof options === 'function' && cb === undefined) {
|
2015-05-18 16:34:25 +00:00
|
|
|
// customer.emails(receiver, scopeParams, condOrRefresh, cb)
|
2015-05-16 17:11:17 +00:00
|
|
|
cb = options;
|
|
|
|
options = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof condOrRefresh === 'object') {
|
|
|
|
actualCond = condOrRefresh;
|
2014-07-27 14:30:45 +00:00
|
|
|
}
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
let embeddedList = this.embeddedList(receiver);
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2014-08-15 13:12:02 +00:00
|
|
|
this.definition.applyScope(receiver, actualCond);
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
const params = mergeQuery(actualCond, scopeParams);
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2014-09-04 19:51:59 +00:00
|
|
|
if (params.where && Object.keys(params.where).length > 0) { // TODO [fabien] Support order/sorting
|
2014-07-27 14:30:45 +00:00
|
|
|
embeddedList = embeddedList ? embeddedList.filter(applyFilter(params)) : embeddedList;
|
|
|
|
}
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
const returnRelated = function(list) {
|
2014-07-29 08:54:28 +00:00
|
|
|
if (params.include) {
|
2015-05-16 17:11:17 +00:00
|
|
|
modelTo.include(list, params.include, options, cb);
|
2014-07-29 08:54:28 +00:00
|
|
|
} else {
|
|
|
|
process.nextTick(function() { cb(null, list); });
|
|
|
|
}
|
|
|
|
};
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2014-07-29 08:54:28 +00:00
|
|
|
returnRelated(embeddedList);
|
2014-07-27 14:30:45 +00:00
|
|
|
};
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
EmbedsMany.prototype.findById = function(fkId, options, cb) {
|
2015-05-16 17:11:17 +00:00
|
|
|
if (typeof options === 'function' && cb === undefined) {
|
2015-05-18 16:34:25 +00:00
|
|
|
// order.emails(fkId, cb)
|
2015-05-16 17:11:17 +00:00
|
|
|
cb = options;
|
|
|
|
options = {};
|
|
|
|
}
|
2018-12-07 14:54:29 +00:00
|
|
|
const pk = this.definition.keyTo;
|
|
|
|
const modelTo = this.definition.modelTo;
|
|
|
|
const modelInstance = this.modelInstance;
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
const embeddedList = this.embeddedList();
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
const find = function(id) {
|
|
|
|
for (let i = 0; i < embeddedList.length; i++) {
|
|
|
|
const item = embeddedList[i];
|
2016-04-01 11:48:17 +00:00
|
|
|
if (idEquals(item[pk], id)) return item;
|
2014-07-27 14:30:45 +00:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
};
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
let item = find(fkId.toString()); // in case of explicit id
|
2014-07-27 14:30:45 +00:00
|
|
|
item = (item instanceof modelTo) ? item : null;
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2014-07-27 14:30:45 +00:00
|
|
|
if (typeof cb === 'function') {
|
|
|
|
process.nextTick(function() {
|
|
|
|
cb(null, item);
|
|
|
|
});
|
2018-12-07 14:43:40 +00:00
|
|
|
}
|
2015-02-24 10:36:13 +00:00
|
|
|
|
|
|
|
return item; // sync
|
2014-07-27 14:30:45 +00:00
|
|
|
};
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
EmbedsMany.prototype.exists = function(fkId, options, cb) {
|
2015-05-16 17:11:17 +00:00
|
|
|
if (typeof options === 'function' && cb === undefined) {
|
2015-05-18 16:34:25 +00:00
|
|
|
// customer.emails.exists(fkId, cb)
|
2015-05-16 17:11:17 +00:00
|
|
|
cb = options;
|
|
|
|
options = {};
|
|
|
|
}
|
2018-12-07 14:54:29 +00:00
|
|
|
const modelTo = this.definition.modelTo;
|
|
|
|
const inst = this.findById(fkId, options, function(err, inst) {
|
2014-07-27 14:30:45 +00:00
|
|
|
if (cb) cb(err, inst instanceof modelTo);
|
|
|
|
});
|
2015-02-24 10:36:13 +00:00
|
|
|
return inst instanceof modelTo; // sync
|
2014-07-27 14:30:45 +00:00
|
|
|
};
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
EmbedsMany.prototype.updateById = function(fkId, data, options, cb) {
|
2015-05-16 17:11:17 +00:00
|
|
|
if (typeof options === 'function' && cb === undefined) {
|
2015-05-18 16:34:25 +00:00
|
|
|
// customer.emails.updateById(fkId, data, cb)
|
2015-05-16 17:11:17 +00:00
|
|
|
cb = options;
|
|
|
|
options = {};
|
|
|
|
}
|
2014-07-27 14:30:45 +00:00
|
|
|
if (typeof data === 'function') {
|
2015-05-18 16:34:25 +00:00
|
|
|
// customer.emails.updateById(fkId, cb)
|
2014-07-27 14:30:45 +00:00
|
|
|
cb = data;
|
|
|
|
data = {};
|
|
|
|
}
|
2016-04-20 11:52:06 +00:00
|
|
|
options = options || {};
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
const modelTo = this.definition.modelTo;
|
|
|
|
const propertyName = this.definition.keyFrom;
|
|
|
|
const modelInstance = this.modelInstance;
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
const embeddedList = this.embeddedList();
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
const inst = this.findById(fkId);
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2014-07-27 14:30:45 +00:00
|
|
|
if (inst instanceof modelTo) {
|
2018-12-07 14:54:29 +00:00
|
|
|
const hookState = {};
|
|
|
|
let context = {
|
2016-04-20 11:52:06 +00:00
|
|
|
Model: modelTo,
|
|
|
|
currentInstance: inst,
|
|
|
|
data: data,
|
|
|
|
options: options,
|
|
|
|
hookState: hookState,
|
|
|
|
};
|
|
|
|
modelTo.notifyObserversOf('before save', context, function(err) {
|
|
|
|
if (err) return cb && cb(err);
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
inst.setAttributes(data);
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2017-01-31 15:14:53 +00:00
|
|
|
err = inst.isValid() ? null : new ValidationError(inst);
|
2016-04-20 11:52:06 +00:00
|
|
|
if (err && typeof cb === 'function') {
|
|
|
|
return process.nextTick(function() {
|
|
|
|
cb(err, inst);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
context = {
|
|
|
|
Model: modelTo,
|
|
|
|
instance: inst,
|
|
|
|
options: options,
|
|
|
|
hookState: hookState,
|
|
|
|
};
|
|
|
|
|
|
|
|
if (typeof cb === 'function') {
|
|
|
|
modelInstance.updateAttribute(propertyName, embeddedList, options,
|
2018-06-12 07:13:32 +00:00
|
|
|
function(err) {
|
|
|
|
if (err) return cb(err, inst);
|
|
|
|
modelTo.notifyObserversOf('after save', context, function(err) {
|
|
|
|
cb(err, inst);
|
|
|
|
});
|
2016-04-20 11:52:06 +00:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
modelTo.notifyObserversOf('after save', context, function(err) {
|
|
|
|
if (!err) return;
|
|
|
|
debug('Unhandled error in "after save" hooks: %s', err.stack || err);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2014-07-27 14:30:45 +00:00
|
|
|
} else if (typeof cb === 'function') {
|
2015-02-24 10:36:13 +00:00
|
|
|
process.nextTick(function() {
|
2014-07-27 14:30:45 +00:00
|
|
|
cb(null, null); // not found
|
|
|
|
});
|
|
|
|
}
|
2015-02-24 10:36:13 +00:00
|
|
|
return inst; // sync
|
2014-07-27 14:30:45 +00:00
|
|
|
};
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
EmbedsMany.prototype.destroyById = function(fkId, options, cb) {
|
2015-05-16 17:11:17 +00:00
|
|
|
if (typeof options === 'function' && cb === undefined) {
|
2015-05-18 16:34:25 +00:00
|
|
|
// customer.emails.destroyById(fkId, cb)
|
2015-05-16 17:11:17 +00:00
|
|
|
cb = options;
|
|
|
|
options = {};
|
|
|
|
}
|
2018-12-07 14:54:29 +00:00
|
|
|
const modelTo = this.definition.modelTo;
|
|
|
|
const propertyName = this.definition.keyFrom;
|
|
|
|
const modelInstance = this.modelInstance;
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
const embeddedList = this.embeddedList();
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
const inst = (fkId instanceof modelTo) ? fkId : this.findById(fkId);
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2014-07-27 14:30:45 +00:00
|
|
|
if (inst instanceof modelTo) {
|
2018-12-07 14:54:29 +00:00
|
|
|
const context = {
|
2016-04-20 11:52:06 +00:00
|
|
|
Model: modelTo,
|
|
|
|
instance: inst,
|
|
|
|
options: options || {},
|
|
|
|
hookState: {},
|
|
|
|
};
|
|
|
|
modelTo.notifyObserversOf('before delete', context, function(err) {
|
|
|
|
if (err) return cb(err);
|
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
const index = embeddedList.indexOf(inst);
|
2016-04-20 11:52:06 +00:00
|
|
|
if (index > -1) embeddedList.splice(index, 1);
|
|
|
|
if (typeof cb !== 'function') return;
|
2015-02-24 10:36:13 +00:00
|
|
|
modelInstance.updateAttribute(propertyName,
|
2019-05-27 13:49:46 +00:00
|
|
|
embeddedList, context.options, function(err) {
|
2016-04-20 11:52:06 +00:00
|
|
|
if (err) return cb(err);
|
|
|
|
modelTo.notifyObserversOf('after delete', context, function(err) {
|
|
|
|
cb(err);
|
|
|
|
});
|
2016-04-01 11:48:17 +00:00
|
|
|
});
|
2016-04-20 11:52:06 +00:00
|
|
|
});
|
2014-07-27 14:30:45 +00:00
|
|
|
} else if (typeof cb === 'function') {
|
2014-07-30 11:22:20 +00:00
|
|
|
process.nextTick(cb); // not found
|
2014-07-27 14:30:45 +00:00
|
|
|
}
|
|
|
|
return inst; // sync
|
|
|
|
};
|
|
|
|
|
2015-05-16 17:11:17 +00:00
|
|
|
EmbedsMany.prototype.destroyAll = function(where, options, cb) {
|
|
|
|
if (typeof options === 'function' && cb === undefined) {
|
2015-05-18 16:34:25 +00:00
|
|
|
// customer.emails.destroyAll(where, cb);
|
2015-05-16 17:11:17 +00:00
|
|
|
cb = options;
|
|
|
|
options = {};
|
2015-05-18 16:34:25 +00:00
|
|
|
} else if (typeof where === 'function' &&
|
|
|
|
options === undefined && cb === undefined) {
|
|
|
|
// customer.emails.destroyAll(cb);
|
|
|
|
cb = where;
|
|
|
|
where = {};
|
2015-05-16 17:11:17 +00:00
|
|
|
}
|
2018-12-07 14:54:29 +00:00
|
|
|
const propertyName = this.definition.keyFrom;
|
|
|
|
const modelInstance = this.modelInstance;
|
2015-03-24 14:35:56 +00:00
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
let embeddedList = this.embeddedList();
|
2015-03-24 14:35:56 +00:00
|
|
|
|
|
|
|
if (where && Object.keys(where).length > 0) {
|
2018-12-07 14:54:29 +00:00
|
|
|
const filter = applyFilter({where: where});
|
|
|
|
const reject = function(v) { return !filter(v); };
|
2015-03-24 14:35:56 +00:00
|
|
|
embeddedList = embeddedList ? embeddedList.filter(reject) : embeddedList;
|
|
|
|
} else {
|
|
|
|
embeddedList = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof cb === 'function') {
|
|
|
|
modelInstance.updateAttribute(propertyName,
|
2019-05-27 13:49:46 +00:00
|
|
|
embeddedList, options || {}, function(err) {
|
2016-04-01 11:48:17 +00:00
|
|
|
cb(err);
|
|
|
|
});
|
2015-03-24 14:35:56 +00:00
|
|
|
} else {
|
2019-05-27 13:49:46 +00:00
|
|
|
modelInstance.setAttribute(propertyName, embeddedList, options || {});
|
2015-03-24 14:35:56 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-07-29 08:51:33 +00:00
|
|
|
EmbedsMany.prototype.get = EmbedsMany.prototype.findById;
|
|
|
|
EmbedsMany.prototype.set = EmbedsMany.prototype.updateById;
|
2014-07-29 15:43:30 +00:00
|
|
|
EmbedsMany.prototype.unset = EmbedsMany.prototype.destroyById;
|
2014-07-29 08:51:33 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
EmbedsMany.prototype.at = function(index, cb) {
|
2018-12-07 14:54:29 +00:00
|
|
|
const modelTo = this.definition.modelTo;
|
|
|
|
const modelInstance = this.modelInstance;
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
const embeddedList = this.embeddedList();
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
let item = embeddedList[parseInt(index)];
|
2014-07-29 08:51:33 +00:00
|
|
|
item = (item instanceof modelTo) ? item : null;
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2014-07-29 08:51:33 +00:00
|
|
|
if (typeof cb === 'function') {
|
|
|
|
process.nextTick(function() {
|
|
|
|
cb(null, item);
|
|
|
|
});
|
2018-12-07 14:43:40 +00:00
|
|
|
}
|
2015-02-24 10:36:13 +00:00
|
|
|
|
|
|
|
return item; // sync
|
2014-07-29 08:51:33 +00:00
|
|
|
};
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
EmbedsMany.prototype.create = function(targetModelData, options, cb) {
|
2018-12-07 14:54:29 +00:00
|
|
|
const pk = this.definition.keyTo;
|
|
|
|
const modelTo = this.definition.modelTo;
|
|
|
|
const propertyName = this.definition.keyFrom;
|
|
|
|
const modelInstance = this.modelInstance;
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2015-05-16 17:11:17 +00:00
|
|
|
if (typeof options === 'function' && cb === undefined) {
|
2015-05-18 16:34:25 +00:00
|
|
|
// customer.emails.create(cb)
|
2015-05-16 17:11:17 +00:00
|
|
|
cb = options;
|
|
|
|
options = {};
|
|
|
|
}
|
|
|
|
|
2014-07-27 14:30:45 +00:00
|
|
|
if (typeof targetModelData === 'function' && !cb) {
|
|
|
|
cb = targetModelData;
|
|
|
|
targetModelData = {};
|
|
|
|
}
|
|
|
|
targetModelData = targetModelData || {};
|
2015-02-18 05:04:31 +00:00
|
|
|
cb = cb || utils.createPromiseCallback();
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
const inst = this.callScopeMethod('build', targetModelData);
|
|
|
|
const embeddedList = this.embeddedList();
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
const updateEmbedded = function(callback) {
|
2015-04-01 14:59:21 +00:00
|
|
|
if (modelInstance.isNewRecord()) {
|
|
|
|
modelInstance.setAttribute(propertyName, embeddedList);
|
2015-05-16 17:11:17 +00:00
|
|
|
modelInstance.save(options, function(err) {
|
2016-04-20 11:52:06 +00:00
|
|
|
callback(err, err ? null : inst);
|
2015-04-01 14:59:21 +00:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
modelInstance.updateAttribute(propertyName,
|
2015-05-16 17:11:17 +00:00
|
|
|
embeddedList, options, function(err) {
|
2016-04-20 11:52:06 +00:00
|
|
|
callback(err, err ? null : inst);
|
2016-04-01 11:48:17 +00:00
|
|
|
});
|
2015-04-01 14:59:21 +00:00
|
|
|
}
|
2014-09-07 11:10:23 +00:00
|
|
|
};
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2014-09-07 10:59:47 +00:00
|
|
|
if (this.definition.options.persistent) {
|
|
|
|
inst.save(function(err) { // will validate
|
|
|
|
if (err) return cb(err, inst);
|
2016-04-20 11:52:06 +00:00
|
|
|
updateEmbedded(cb);
|
2014-07-27 14:30:45 +00:00
|
|
|
});
|
2014-09-07 10:59:47 +00:00
|
|
|
} else {
|
2017-01-31 15:14:53 +00:00
|
|
|
const err = inst.isValid() ? null : new ValidationError(inst);
|
2014-09-07 10:59:47 +00:00
|
|
|
if (err) {
|
2014-09-07 11:10:23 +00:00
|
|
|
process.nextTick(function() {
|
2015-02-24 10:36:13 +00:00
|
|
|
cb(err);
|
2014-09-07 10:59:47 +00:00
|
|
|
});
|
2014-09-07 11:10:23 +00:00
|
|
|
} else {
|
2018-12-07 14:54:29 +00:00
|
|
|
const context = {
|
2016-04-20 11:52:06 +00:00
|
|
|
Model: modelTo,
|
|
|
|
instance: inst,
|
|
|
|
options: options || {},
|
|
|
|
hookState: {},
|
|
|
|
};
|
|
|
|
modelTo.notifyObserversOf('before save', context, function(err) {
|
|
|
|
if (err) return cb(err);
|
|
|
|
updateEmbedded(function(err, inst) {
|
|
|
|
if (err) return cb(err, null);
|
|
|
|
modelTo.notifyObserversOf('after save', context, function(err) {
|
|
|
|
cb(err, err ? null : inst);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2014-09-07 10:59:47 +00:00
|
|
|
}
|
2014-07-30 11:22:20 +00:00
|
|
|
}
|
2015-02-18 05:04:31 +00:00
|
|
|
return cb.promise;
|
2014-07-27 14:30:45 +00:00
|
|
|
};
|
|
|
|
|
2014-07-29 15:43:30 +00:00
|
|
|
EmbedsMany.prototype.build = function(targetModelData) {
|
2018-12-07 14:54:29 +00:00
|
|
|
const modelTo = this.definition.modelTo;
|
|
|
|
const modelInstance = this.modelInstance;
|
|
|
|
const forceId = this.definition.options.forceId;
|
|
|
|
const persistent = this.definition.options.persistent;
|
|
|
|
const propertyName = this.definition.keyFrom;
|
|
|
|
const connector = modelTo.dataSource.connector;
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
const pk = this.definition.keyTo;
|
|
|
|
const pkProp = modelTo.definition.properties[pk];
|
|
|
|
const pkType = pkProp && pkProp.type;
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
const embeddedList = this.embeddedList();
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2014-07-27 14:30:45 +00:00
|
|
|
targetModelData = targetModelData || {};
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
let assignId = (forceId || targetModelData[pk] === undefined);
|
2014-09-07 10:59:47 +00:00
|
|
|
assignId = assignId && !persistent;
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2014-09-04 15:31:53 +00:00
|
|
|
if (assignId && pkType === Number) {
|
2018-12-07 14:54:29 +00:00
|
|
|
const ids = embeddedList.map(function(m) {
|
2014-07-27 15:16:25 +00:00
|
|
|
return (typeof m[pk] === 'number' ? m[pk] : 0);
|
|
|
|
});
|
2014-07-30 11:22:20 +00:00
|
|
|
if (ids.length > 0) {
|
|
|
|
targetModelData[pk] = Math.max.apply(null, ids) + 1;
|
|
|
|
} else {
|
|
|
|
targetModelData[pk] = 1;
|
|
|
|
}
|
2014-09-04 15:31:53 +00:00
|
|
|
} else if (assignId && typeof connector.generateId === 'function') {
|
2018-12-07 14:54:29 +00:00
|
|
|
const id = connector.generateId(modelTo.modelName, targetModelData, pk);
|
2016-04-01 11:48:17 +00:00
|
|
|
targetModelData[pk] = id;
|
2014-07-27 15:16:25 +00:00
|
|
|
}
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2014-08-19 20:10:35 +00:00
|
|
|
this.definition.applyProperties(modelInstance, targetModelData);
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
const inst = new modelTo(targetModelData);
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2014-07-27 14:30:45 +00:00
|
|
|
if (this.definition.options.prepend) {
|
|
|
|
embeddedList.unshift(inst);
|
2018-04-13 17:17:40 +00:00
|
|
|
modelInstance[propertyName] = embeddedList;
|
2014-07-27 14:30:45 +00:00
|
|
|
} else {
|
|
|
|
embeddedList.push(inst);
|
2018-04-13 17:17:40 +00:00
|
|
|
modelInstance[propertyName] = embeddedList;
|
2014-07-27 14:30:45 +00:00
|
|
|
}
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2014-08-15 13:12:02 +00:00
|
|
|
this.prepareEmbeddedInstance(inst);
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2014-07-27 14:30:45 +00:00
|
|
|
return inst;
|
|
|
|
};
|
2014-07-29 13:01:47 +00:00
|
|
|
|
2014-07-29 15:43:30 +00:00
|
|
|
/**
|
|
|
|
* Add the target model instance to the 'embedsMany' relation
|
|
|
|
* @param {Object|ID} acInst The actual instance or id value
|
|
|
|
*/
|
2016-04-01 11:48:17 +00:00
|
|
|
EmbedsMany.prototype.add = function(acInst, data, options, cb) {
|
2015-05-16 17:11:17 +00:00
|
|
|
if (typeof options === 'function' && cb === undefined) {
|
2015-05-18 16:34:25 +00:00
|
|
|
// customer.emails.add(acInst, data, cb)
|
2015-05-16 17:11:17 +00:00
|
|
|
cb = options;
|
|
|
|
options = {};
|
2015-05-18 16:34:25 +00:00
|
|
|
} else if (typeof data === 'function' &&
|
|
|
|
options === undefined && cb === undefined) {
|
|
|
|
// customer.emails.add(acInst, cb)
|
2014-07-29 19:46:12 +00:00
|
|
|
cb = data;
|
|
|
|
data = {};
|
|
|
|
}
|
2015-02-18 05:04:31 +00:00
|
|
|
cb = cb || utils.createPromiseCallback();
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
const self = this;
|
|
|
|
const definition = this.definition;
|
|
|
|
const modelTo = this.definition.modelTo;
|
|
|
|
const modelInstance = this.modelInstance;
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
const defOpts = definition.options;
|
|
|
|
const belongsTo = defOpts.belongsTo && modelTo.relations[defOpts.belongsTo];
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2014-07-29 19:56:59 +00:00
|
|
|
if (!belongsTo) {
|
2017-01-31 15:10:56 +00:00
|
|
|
throw new Error('Invalid reference: ' + defOpts.belongsTo || '(none)');
|
2014-07-29 15:43:30 +00:00
|
|
|
}
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
const fk2 = belongsTo.keyTo;
|
|
|
|
const pk2 = belongsTo.modelTo.definition.idName() || 'id';
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
const query = {};
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2014-07-29 19:56:59 +00:00
|
|
|
query[fk2] = (acInst instanceof belongsTo.modelTo) ? acInst[pk2] : acInst;
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
const filter = {where: query};
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2014-07-29 19:56:59 +00:00
|
|
|
belongsTo.applyScope(modelInstance, filter);
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2015-05-16 17:11:17 +00:00
|
|
|
belongsTo.modelTo.findOne(filter, options, function(err, ref) {
|
2014-07-29 19:56:59 +00:00
|
|
|
if (ref instanceof belongsTo.modelTo) {
|
2018-12-07 14:54:29 +00:00
|
|
|
const inst = self.build(data || {});
|
2017-01-31 15:10:56 +00:00
|
|
|
inst[defOpts.belongsTo](ref);
|
2014-07-29 15:43:30 +00:00
|
|
|
modelInstance.save(function(err) {
|
|
|
|
cb(err, err ? null : inst);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
cb(null, null);
|
|
|
|
}
|
|
|
|
});
|
2015-02-18 05:04:31 +00:00
|
|
|
return cb.promise;
|
2014-07-29 15:43:30 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove the target model instance from the 'embedsMany' relation
|
|
|
|
* @param {Object|ID) acInst The actual instance or id value
|
|
|
|
*/
|
2016-04-01 11:48:17 +00:00
|
|
|
EmbedsMany.prototype.remove = function(acInst, options, cb) {
|
2015-05-16 17:11:17 +00:00
|
|
|
if (typeof options === 'function' && cb === undefined) {
|
2015-05-18 16:34:25 +00:00
|
|
|
// customer.emails.remove(acInst, cb)
|
2015-05-16 17:11:17 +00:00
|
|
|
cb = options;
|
|
|
|
options = {};
|
|
|
|
}
|
2018-12-07 14:54:29 +00:00
|
|
|
const self = this;
|
|
|
|
const definition = this.definition;
|
|
|
|
const modelTo = this.definition.modelTo;
|
|
|
|
const modelInstance = this.modelInstance;
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
const defOpts = definition.options;
|
|
|
|
const belongsTo = defOpts.belongsTo && modelTo.relations[defOpts.belongsTo];
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2014-07-29 19:56:59 +00:00
|
|
|
if (!belongsTo) {
|
2017-01-31 15:10:56 +00:00
|
|
|
throw new Error('Invalid reference: ' + defOpts.belongsTo || '(none)');
|
2014-07-29 15:43:30 +00:00
|
|
|
}
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
const fk2 = belongsTo.keyTo;
|
|
|
|
const pk2 = belongsTo.modelTo.definition.idName() || 'id';
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
const query = {};
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2014-07-29 19:56:59 +00:00
|
|
|
query[fk2] = (acInst instanceof belongsTo.modelTo) ? acInst[pk2] : acInst;
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
const filter = {where: query};
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2014-07-29 19:56:59 +00:00
|
|
|
belongsTo.applyScope(modelInstance, filter);
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2015-02-18 05:04:31 +00:00
|
|
|
cb = cb || utils.createPromiseCallback();
|
|
|
|
|
2015-05-16 17:11:17 +00:00
|
|
|
modelInstance[definition.name](filter, options, function(err, items) {
|
2014-07-29 15:43:30 +00:00
|
|
|
if (err) return cb(err);
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2014-07-29 15:43:30 +00:00
|
|
|
items.forEach(function(item) {
|
|
|
|
self.unset(item);
|
|
|
|
});
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2015-05-16 17:11:17 +00:00
|
|
|
modelInstance.save(options, function(err) {
|
2014-07-30 11:22:20 +00:00
|
|
|
cb(err);
|
2014-07-29 15:43:30 +00:00
|
|
|
});
|
|
|
|
});
|
2015-02-18 05:04:31 +00:00
|
|
|
return cb.promise;
|
2014-07-29 15:43:30 +00:00
|
|
|
};
|
|
|
|
|
2017-04-01 09:13:22 +00:00
|
|
|
RelationDefinition.referencesMany = function referencesMany(modelFrom, modelToRef, params) {
|
2014-07-29 19:46:12 +00:00
|
|
|
params = params || {};
|
2017-04-01 09:13:22 +00:00
|
|
|
normalizeRelationAs(params, modelToRef);
|
2018-12-07 14:54:29 +00:00
|
|
|
const modelTo = lookupModelTo(modelFrom, modelToRef, params, true);
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
const thisClassName = modelFrom.modelName;
|
|
|
|
const relationName = params.as || i8n.camelize(modelTo.pluralModelName, true);
|
|
|
|
const fk = params.foreignKey || i8n.camelize(modelTo.modelName + '_ids', true);
|
|
|
|
const idName = modelTo.dataSource.idName(modelTo.modelName) || 'id';
|
|
|
|
const idType = modelTo.definition.properties[idName].type;
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
const definition = modelFrom.relations[relationName] = new RelationDefinition({
|
2014-07-29 19:46:12 +00:00
|
|
|
name: relationName,
|
|
|
|
type: RelationTypes.referencesMany,
|
|
|
|
modelFrom: modelFrom,
|
|
|
|
keyFrom: fk,
|
|
|
|
keyTo: idName,
|
|
|
|
modelTo: modelTo,
|
|
|
|
multiple: true,
|
|
|
|
properties: params.properties,
|
|
|
|
scope: params.scope,
|
2016-04-01 11:48:17 +00:00
|
|
|
options: params.options,
|
2014-07-29 19:46:12 +00:00
|
|
|
});
|
2015-02-24 10:36:13 +00:00
|
|
|
|
|
|
|
modelFrom.dataSource.defineProperty(modelFrom.modelName, fk, {
|
2016-04-01 11:48:17 +00:00
|
|
|
type: [idType], default: function() { return []; },
|
2014-07-29 19:46:12 +00:00
|
|
|
});
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2014-07-29 19:46:12 +00:00
|
|
|
modelFrom.validate(relationName, function(err) {
|
2018-12-07 14:54:29 +00:00
|
|
|
const ids = this[fk] || [];
|
2018-10-03 22:17:08 +00:00
|
|
|
if (idsHaveDuplicates(ids)) {
|
2018-12-07 14:54:29 +00:00
|
|
|
const msg = 'contains duplicate `' + modelTo.modelName + '` instance';
|
2014-07-29 19:46:12 +00:00
|
|
|
this.errors.add(relationName, msg, 'uniqueness');
|
|
|
|
err(false);
|
|
|
|
}
|
2016-08-19 17:46:59 +00:00
|
|
|
}, {code: 'uniqueness'});
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
const scopeMethods = {
|
2014-07-29 19:46:12 +00:00
|
|
|
findById: scopeMethod(definition, 'findById'),
|
|
|
|
destroy: scopeMethod(definition, 'destroyById'),
|
|
|
|
updateById: scopeMethod(definition, 'updateById'),
|
|
|
|
exists: scopeMethod(definition, 'exists'),
|
|
|
|
add: scopeMethod(definition, 'add'),
|
|
|
|
remove: scopeMethod(definition, 'remove'),
|
2016-04-01 11:48:17 +00:00
|
|
|
at: scopeMethod(definition, 'at'),
|
2014-07-29 19:46:12 +00:00
|
|
|
};
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
const findByIdFunc = scopeMethods.findById;
|
2014-07-29 19:46:12 +00:00
|
|
|
modelFrom.prototype['__findById__' + relationName] = findByIdFunc;
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
const destroyByIdFunc = scopeMethods.destroy;
|
2014-07-29 19:46:12 +00:00
|
|
|
modelFrom.prototype['__destroyById__' + relationName] = destroyByIdFunc;
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
const updateByIdFunc = scopeMethods.updateById;
|
2014-07-29 19:46:12 +00:00
|
|
|
modelFrom.prototype['__updateById__' + relationName] = updateByIdFunc;
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
const addFunc = scopeMethods.add;
|
2014-07-30 13:01:55 +00:00
|
|
|
modelFrom.prototype['__link__' + relationName] = addFunc;
|
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
const removeFunc = scopeMethods.remove;
|
2014-07-30 13:01:55 +00:00
|
|
|
modelFrom.prototype['__unlink__' + relationName] = removeFunc;
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2014-07-29 19:46:12 +00:00
|
|
|
scopeMethods.create = scopeMethod(definition, 'create');
|
|
|
|
scopeMethods.build = scopeMethod(definition, 'build');
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2014-07-29 20:59:44 +00:00
|
|
|
scopeMethods.related = scopeMethod(definition, 'related');
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
const customMethods = extendScopeMethods(definition, scopeMethods, params.scopeMethods);
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
for (let i = 0; i < customMethods.length; i++) {
|
|
|
|
const methodName = customMethods[i];
|
|
|
|
const method = scopeMethods[methodName];
|
2014-07-30 14:46:05 +00:00
|
|
|
if (typeof method === 'function' && method.shared === true) {
|
|
|
|
modelFrom.prototype['__' + methodName + '__' + relationName] = method;
|
|
|
|
}
|
2018-12-07 14:43:40 +00:00
|
|
|
}
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2014-07-29 19:46:12 +00:00
|
|
|
// Mix the property and scoped methods into the prototype class
|
2018-12-07 14:54:29 +00:00
|
|
|
const scopeDefinition = defineScope(modelFrom.prototype, modelTo, relationName, function() {
|
2014-07-29 19:46:12 +00:00
|
|
|
return {};
|
2014-08-08 16:25:49 +00:00
|
|
|
}, scopeMethods, definition.options);
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2014-07-29 20:59:44 +00:00
|
|
|
scopeDefinition.related = scopeMethods.related; // bound to definition
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2014-08-13 09:28:23 +00:00
|
|
|
return definition;
|
2014-07-29 19:46:12 +00:00
|
|
|
};
|
2014-07-29 13:01:47 +00:00
|
|
|
|
2015-05-16 17:11:17 +00:00
|
|
|
ReferencesMany.prototype.related = function(receiver, scopeParams, condOrRefresh, options, cb) {
|
2018-12-07 14:54:29 +00:00
|
|
|
const fk = this.definition.keyFrom;
|
|
|
|
const modelTo = this.definition.modelTo;
|
|
|
|
const relationName = this.definition.name;
|
|
|
|
const modelInstance = this.modelInstance;
|
|
|
|
const self = receiver;
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
let actualCond = {};
|
|
|
|
let actualRefresh = false;
|
2015-02-18 05:04:31 +00:00
|
|
|
|
2015-05-16 17:11:17 +00:00
|
|
|
if (typeof condOrRefresh === 'function' &&
|
|
|
|
options === undefined && cb === undefined) {
|
2015-05-18 16:34:25 +00:00
|
|
|
// customer.orders(receiver, scopeParams, cb)
|
2014-07-29 19:46:12 +00:00
|
|
|
cb = condOrRefresh;
|
2015-05-16 17:11:17 +00:00
|
|
|
condOrRefresh = undefined;
|
|
|
|
} else if (typeof options === 'function' && cb === undefined) {
|
2015-05-18 16:34:25 +00:00
|
|
|
// customer.orders(receiver, scopeParams, condOrRefresh, cb)
|
2015-05-16 17:11:17 +00:00
|
|
|
cb = options;
|
|
|
|
options = {};
|
2014-07-29 19:46:12 +00:00
|
|
|
if (typeof condOrRefresh === 'boolean') {
|
|
|
|
actualRefresh = condOrRefresh;
|
2015-05-16 17:11:17 +00:00
|
|
|
condOrRefresh = {};
|
2014-07-29 19:46:12 +00:00
|
|
|
} else {
|
|
|
|
actualRefresh = true;
|
|
|
|
}
|
|
|
|
}
|
2015-05-16 17:11:17 +00:00
|
|
|
actualCond = condOrRefresh || {};
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
const ids = self[fk] || [];
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2014-07-29 19:46:12 +00:00
|
|
|
this.definition.applyScope(modelInstance, actualCond);
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
const params = mergeQuery(actualCond, scopeParams);
|
2015-05-16 17:11:17 +00:00
|
|
|
return modelTo.findByIds(ids, params, options, cb);
|
2014-07-29 13:01:47 +00:00
|
|
|
};
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
ReferencesMany.prototype.findById = function(fkId, options, cb) {
|
2015-05-16 17:11:17 +00:00
|
|
|
if (typeof options === 'function' && cb === undefined) {
|
2015-05-18 16:34:25 +00:00
|
|
|
// customer.orders.findById(fkId, cb)
|
2015-05-16 17:11:17 +00:00
|
|
|
cb = options;
|
|
|
|
options = {};
|
|
|
|
}
|
2018-12-07 14:54:29 +00:00
|
|
|
const modelTo = this.definition.modelTo;
|
|
|
|
const modelFrom = this.definition.modelFrom;
|
|
|
|
const relationName = this.definition.name;
|
|
|
|
const modelInstance = this.modelInstance;
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
const pk = this.definition.keyTo;
|
|
|
|
const fk = this.definition.keyFrom;
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2014-07-29 19:46:12 +00:00
|
|
|
if (typeof fkId === 'object') {
|
|
|
|
fkId = fkId.toString(); // mongodb
|
|
|
|
}
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
const ids = modelInstance[fk] || [];
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
const filter = {};
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2014-07-29 19:46:12 +00:00
|
|
|
this.definition.applyScope(modelInstance, filter);
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2015-02-18 05:04:31 +00:00
|
|
|
cb = cb || utils.createPromiseCallback();
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
modelTo.findByIds([fkId], filter, options, function(err, instances) {
|
2014-07-29 19:46:12 +00:00
|
|
|
if (err) {
|
|
|
|
return cb(err);
|
|
|
|
}
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
const inst = instances[0];
|
2014-07-29 19:46:12 +00:00
|
|
|
if (!inst) {
|
2016-07-22 19:26:07 +00:00
|
|
|
err = new Error(g.f('No instance with {{id}} %s found for %s', fkId, modelTo.modelName));
|
2014-07-29 19:46:12 +00:00
|
|
|
err.statusCode = 404;
|
|
|
|
return cb(err);
|
|
|
|
}
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2014-07-29 19:46:12 +00:00
|
|
|
// Check if the foreign key is amongst the ids
|
2015-08-18 14:22:36 +00:00
|
|
|
if (utils.findIndexOf(ids, inst[pk], idEquals) > -1) {
|
2014-07-29 19:46:12 +00:00
|
|
|
cb(null, inst);
|
|
|
|
} else {
|
2016-07-22 19:26:07 +00:00
|
|
|
err = new Error(g.f('Key mismatch: %s.%s: %s, %s.%s: %s',
|
|
|
|
modelFrom.modelName, fk, modelInstance[fk],
|
|
|
|
modelTo.modelName, pk, inst[pk]));
|
2014-07-29 19:46:12 +00:00
|
|
|
err.statusCode = 400;
|
|
|
|
cb(err);
|
|
|
|
}
|
|
|
|
});
|
2015-02-18 05:04:31 +00:00
|
|
|
return cb.promise;
|
2014-07-29 19:46:12 +00:00
|
|
|
};
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
ReferencesMany.prototype.exists = function(fkId, options, cb) {
|
2015-05-16 17:11:17 +00:00
|
|
|
if (typeof options === 'function' && cb === undefined) {
|
2015-05-18 16:34:25 +00:00
|
|
|
// customer.orders.exists(fkId, cb)
|
2015-05-16 17:11:17 +00:00
|
|
|
cb = options;
|
|
|
|
options = {};
|
|
|
|
}
|
2018-12-07 14:54:29 +00:00
|
|
|
const fk = this.definition.keyFrom;
|
|
|
|
const ids = this.modelInstance[fk] || [];
|
2015-02-18 05:04:31 +00:00
|
|
|
|
|
|
|
cb = cb || utils.createPromiseCallback();
|
2016-04-01 11:48:17 +00:00
|
|
|
process.nextTick(function() { cb(null, utils.findIndexOf(ids, fkId, idEquals) > -1); });
|
2015-02-18 05:04:31 +00:00
|
|
|
return cb.promise;
|
2014-07-29 19:46:12 +00:00
|
|
|
};
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
ReferencesMany.prototype.updateById = function(fkId, data, options, cb) {
|
2015-05-16 17:11:17 +00:00
|
|
|
if (typeof options === 'function' && cb === undefined) {
|
2015-05-18 16:34:25 +00:00
|
|
|
// customer.orders.updateById(fkId, data, cb)
|
2015-05-16 17:11:17 +00:00
|
|
|
cb = options;
|
|
|
|
options = {};
|
2015-05-18 16:34:25 +00:00
|
|
|
} else if (typeof data === 'function' &&
|
|
|
|
options === undefined && cb === undefined) {
|
|
|
|
// customer.orders.updateById(fkId, cb)
|
2014-07-29 19:46:12 +00:00
|
|
|
cb = data;
|
|
|
|
data = {};
|
|
|
|
}
|
2015-02-18 05:04:31 +00:00
|
|
|
cb = cb || utils.createPromiseCallback();
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2015-05-16 17:11:17 +00:00
|
|
|
this.findById(fkId, options, function(err, inst) {
|
2014-07-29 19:46:12 +00:00
|
|
|
if (err) return cb(err);
|
2015-05-16 17:11:17 +00:00
|
|
|
inst.updateAttributes(data, options, cb);
|
2014-07-29 19:46:12 +00:00
|
|
|
});
|
2015-02-18 05:04:31 +00:00
|
|
|
return cb.promise;
|
2014-07-29 19:46:12 +00:00
|
|
|
};
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
ReferencesMany.prototype.destroyById = function(fkId, options, cb) {
|
2015-05-16 17:11:17 +00:00
|
|
|
if (typeof options === 'function' && cb === undefined) {
|
2015-05-18 16:34:25 +00:00
|
|
|
// customer.orders.destroyById(fkId, cb)
|
2015-05-16 17:11:17 +00:00
|
|
|
cb = options;
|
|
|
|
options = {};
|
|
|
|
}
|
2018-12-07 14:54:29 +00:00
|
|
|
const self = this;
|
2015-02-18 05:04:31 +00:00
|
|
|
cb = cb || utils.createPromiseCallback();
|
2014-07-29 19:46:12 +00:00
|
|
|
this.findById(fkId, function(err, inst) {
|
|
|
|
if (err) return cb(err);
|
|
|
|
self.remove(inst, function(err, ids) {
|
|
|
|
inst.destroy(cb);
|
|
|
|
});
|
|
|
|
});
|
2015-02-18 05:04:31 +00:00
|
|
|
return cb.promise;
|
2014-07-29 19:46:12 +00:00
|
|
|
};
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
ReferencesMany.prototype.at = function(index, options, cb) {
|
2015-05-16 17:11:17 +00:00
|
|
|
if (typeof options === 'function' && cb === undefined) {
|
2015-05-18 16:34:25 +00:00
|
|
|
// customer.orders.at(index, cb)
|
2015-05-16 17:11:17 +00:00
|
|
|
cb = options;
|
|
|
|
options = {};
|
|
|
|
}
|
2018-12-07 14:54:29 +00:00
|
|
|
const fk = this.definition.keyFrom;
|
|
|
|
const ids = this.modelInstance[fk] || [];
|
2015-02-18 05:04:31 +00:00
|
|
|
cb = cb || utils.createPromiseCallback();
|
2015-05-16 17:11:17 +00:00
|
|
|
this.findById(ids[index], options, cb);
|
2015-02-18 05:04:31 +00:00
|
|
|
return cb.promise;
|
2014-07-29 19:46:12 +00:00
|
|
|
};
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
ReferencesMany.prototype.create = function(targetModelData, options, cb) {
|
2015-05-16 17:11:17 +00:00
|
|
|
if (typeof options === 'function' && cb === undefined) {
|
2015-05-18 16:34:25 +00:00
|
|
|
// customer.orders.create(data, cb)
|
2015-05-16 17:11:17 +00:00
|
|
|
cb = options;
|
|
|
|
options = {};
|
|
|
|
}
|
2018-12-07 14:54:29 +00:00
|
|
|
const definition = this.definition;
|
|
|
|
const modelTo = this.definition.modelTo;
|
|
|
|
const relationName = this.definition.name;
|
|
|
|
const modelInstance = this.modelInstance;
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
const pk = this.definition.keyTo;
|
|
|
|
const fk = this.definition.keyFrom;
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2014-07-29 19:46:12 +00:00
|
|
|
if (typeof targetModelData === 'function' && !cb) {
|
|
|
|
cb = targetModelData;
|
|
|
|
targetModelData = {};
|
|
|
|
}
|
|
|
|
targetModelData = targetModelData || {};
|
2015-02-18 05:04:31 +00:00
|
|
|
cb = cb || utils.createPromiseCallback();
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
const ids = modelInstance[fk] || [];
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
const inst = this.callScopeMethod('build', targetModelData);
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2015-05-16 17:11:17 +00:00
|
|
|
inst.save(options, function(err, inst) {
|
2014-07-29 19:46:12 +00:00
|
|
|
if (err) return cb(err, inst);
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
let id = inst[pk];
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2014-07-29 19:46:12 +00:00
|
|
|
if (typeof id === 'object') {
|
|
|
|
id = id.toString(); // mongodb
|
|
|
|
}
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2014-07-29 19:46:12 +00:00
|
|
|
if (definition.options.prepend) {
|
|
|
|
ids.unshift(id);
|
|
|
|
} else {
|
|
|
|
ids.push(id);
|
|
|
|
}
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2014-07-29 19:46:12 +00:00
|
|
|
modelInstance.updateAttribute(fk,
|
2015-05-16 17:11:17 +00:00
|
|
|
ids, options, function(err, modelInst) {
|
2016-04-01 11:48:17 +00:00
|
|
|
cb(err, inst);
|
|
|
|
});
|
2014-07-29 19:46:12 +00:00
|
|
|
});
|
2015-02-18 05:04:31 +00:00
|
|
|
return cb.promise;
|
2014-07-29 19:46:12 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
ReferencesMany.prototype.build = function(targetModelData) {
|
2018-12-07 14:54:29 +00:00
|
|
|
const modelTo = this.definition.modelTo;
|
2014-07-29 19:46:12 +00:00
|
|
|
targetModelData = targetModelData || {};
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2014-07-29 19:46:12 +00:00
|
|
|
this.definition.applyProperties(this.modelInstance, targetModelData);
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2014-07-29 19:46:12 +00:00
|
|
|
return new modelTo(targetModelData);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add the target model instance to the 'embedsMany' relation
|
|
|
|
* @param {Object|ID} acInst The actual instance or id value
|
|
|
|
*/
|
2016-04-01 11:48:17 +00:00
|
|
|
ReferencesMany.prototype.add = function(acInst, options, cb) {
|
2015-05-16 17:11:17 +00:00
|
|
|
if (typeof options === 'function' && cb === undefined) {
|
2015-05-18 16:34:25 +00:00
|
|
|
// customer.orders.add(acInst, cb)
|
2015-05-16 17:11:17 +00:00
|
|
|
cb = options;
|
|
|
|
options = {};
|
|
|
|
}
|
2018-12-07 14:54:29 +00:00
|
|
|
const self = this;
|
|
|
|
const definition = this.definition;
|
|
|
|
const modelTo = this.definition.modelTo;
|
|
|
|
const modelInstance = this.modelInstance;
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
const pk = this.definition.keyTo;
|
|
|
|
const fk = this.definition.keyFrom;
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
const insert = function(inst, done) {
|
|
|
|
let id = inst[pk];
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2014-07-29 19:46:12 +00:00
|
|
|
if (typeof id === 'object') {
|
|
|
|
id = id.toString(); // mongodb
|
|
|
|
}
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
const ids = modelInstance[fk] || [];
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2014-07-29 19:46:12 +00:00
|
|
|
if (definition.options.prepend) {
|
|
|
|
ids.unshift(id);
|
|
|
|
} else {
|
|
|
|
ids.push(id);
|
|
|
|
}
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2015-05-16 17:11:17 +00:00
|
|
|
modelInstance.updateAttribute(fk, ids, options, function(err) {
|
2014-07-30 13:01:55 +00:00
|
|
|
done(err, err ? null : inst);
|
2014-07-29 19:46:12 +00:00
|
|
|
});
|
|
|
|
};
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2015-02-18 05:04:31 +00:00
|
|
|
cb = cb || utils.createPromiseCallback();
|
|
|
|
|
2014-07-29 19:46:12 +00:00
|
|
|
if (acInst instanceof modelTo) {
|
2014-07-30 13:01:55 +00:00
|
|
|
insert(acInst, cb);
|
2014-07-29 19:46:12 +00:00
|
|
|
} else {
|
2018-12-07 14:54:29 +00:00
|
|
|
const filter = {where: {}};
|
2014-07-29 19:46:12 +00:00
|
|
|
filter.where[pk] = acInst;
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2014-07-29 19:46:12 +00:00
|
|
|
definition.applyScope(modelInstance, filter);
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
modelTo.findOne(filter, options, function(err, inst) {
|
2014-07-30 13:01:55 +00:00
|
|
|
if (err || !inst) return cb(err, null);
|
|
|
|
insert(inst, cb);
|
2014-07-29 19:46:12 +00:00
|
|
|
});
|
|
|
|
}
|
2015-02-18 05:04:31 +00:00
|
|
|
return cb.promise;
|
2014-07-29 19:46:12 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove the target model instance from the 'embedsMany' relation
|
|
|
|
* @param {Object|ID) acInst The actual instance or id value
|
|
|
|
*/
|
2016-04-01 11:48:17 +00:00
|
|
|
ReferencesMany.prototype.remove = function(acInst, options, cb) {
|
2015-05-16 17:11:17 +00:00
|
|
|
if (typeof options === 'function' && cb === undefined) {
|
2015-05-18 16:34:25 +00:00
|
|
|
// customer.orders.remove(acInst, cb)
|
2015-05-16 17:11:17 +00:00
|
|
|
cb = options;
|
|
|
|
options = {};
|
|
|
|
}
|
2018-12-07 14:54:29 +00:00
|
|
|
const definition = this.definition;
|
|
|
|
const modelInstance = this.modelInstance;
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
const pk = this.definition.keyTo;
|
|
|
|
const fk = this.definition.keyFrom;
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
const ids = modelInstance[fk] || [];
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
const id = (acInst instanceof definition.modelTo) ? acInst[pk] : acInst;
|
2015-02-24 10:36:13 +00:00
|
|
|
|
2015-02-18 05:04:31 +00:00
|
|
|
cb = cb || utils.createPromiseCallback();
|
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
const index = utils.findIndexOf(ids, id, idEquals);
|
2014-07-29 19:46:12 +00:00
|
|
|
if (index > -1) {
|
|
|
|
ids.splice(index, 1);
|
2015-05-16 17:11:17 +00:00
|
|
|
modelInstance.updateAttribute(fk, ids, options, function(err, inst) {
|
2014-07-29 19:46:12 +00:00
|
|
|
cb(err, inst[fk] || []);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
process.nextTick(function() { cb(null, ids); });
|
|
|
|
}
|
2015-02-18 05:04:31 +00:00
|
|
|
return cb.promise;
|
2014-10-17 13:44:24 +00:00
|
|
|
};
|