mirror of
https://github.com/strapi/strapi.git
synced 2025-12-27 07:03:38 +00:00
Fix findOne user, many-to-many relationship loss and remove relation business logic from CM
This commit is contained in:
parent
7e7b0f1d2f
commit
c7f871745a
@ -27,7 +27,7 @@ const transformToArrayID = (array) => {
|
||||
|
||||
module.exports = {
|
||||
getModel: function (model, plugin) {
|
||||
return _.get(strapi.plugins, [plugin, 'models', model]) || get(strapi, ['models', model]) || undefined;
|
||||
return _.get(strapi.plugins, [plugin, 'models', model]) || _.get(strapi, ['models', model]) || undefined;
|
||||
},
|
||||
|
||||
findOne: async function (params, populate, raw = true) {
|
||||
@ -164,7 +164,9 @@ module.exports = {
|
||||
toRemove.forEach(value => {
|
||||
value = _.isString(value) || _.isNumber(value) ? { [this.primaryKey]: value } : value;
|
||||
|
||||
value[details.via] = null;
|
||||
value[details.via] = association.nature !== 'manyToMany' ?
|
||||
null :
|
||||
params.values[this.primaryKey] || params[this.primaryKey];
|
||||
|
||||
virtualFields.push(
|
||||
module.exports.removeRelation.call(model, {
|
||||
|
||||
@ -9,7 +9,7 @@ const _ = require('lodash');
|
||||
|
||||
module.exports = {
|
||||
getModel: function (model, plugin) {
|
||||
return _.get(strapi.plugins, [plugin, 'models', model]) || get(strapi, ['models', model]) || undefined;
|
||||
return _.get(strapi.plugins, [plugin, 'models', model]) || _.get(strapi, ['models', model]) || undefined;
|
||||
},
|
||||
|
||||
update: async function (params) {
|
||||
|
||||
@ -1 +1 @@
|
||||
{}
|
||||
{}
|
||||
@ -106,223 +106,9 @@ module.exports = {
|
||||
},
|
||||
|
||||
update: async function (params) {
|
||||
const virtualFields = [];
|
||||
const response = await module.exports.findOne.call(this, params);
|
||||
|
||||
// Only update fields which are on this document.
|
||||
const values = params.parseRelationships === false ? params.values : Object.keys(JSON.parse(JSON.stringify(params.values))).reduce((acc, current) => {
|
||||
const association = this.associations.filter(x => x.alias === current)[0];
|
||||
const details = this._attributes[current];
|
||||
|
||||
if (_.get(this._attributes, `${current}.isVirtual`) !== true && _.isUndefined(association)) {
|
||||
acc[current] = params.values[current];
|
||||
} else {
|
||||
switch (association.nature) {
|
||||
case 'oneWay':
|
||||
acc[current] = _.get(params.values[current], this.primaryKey, params.values[current]) || null;
|
||||
|
||||
break;
|
||||
case 'oneToOne':
|
||||
if (response[current] !== params.values[current]) {
|
||||
const value = _.isNull(params.values[current]) ? response[current] : params.values;
|
||||
const recordId = _.isNull(params.values[current]) ? value[this.primaryKey] || value.id || value._id : value[current];
|
||||
|
||||
if (response[current] && _.isObject(response[current]) && response[current][this.primaryKey] !== value[current]) {
|
||||
virtualFields.push(
|
||||
strapi.query(details.collection || details.model, details.plugin).update({
|
||||
id: response[current][this.primaryKey],
|
||||
values: {
|
||||
[details.via]: null
|
||||
},
|
||||
parseRelationships: false
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
// Remove previous relationship asynchronously if it exists.
|
||||
virtualFields.push(
|
||||
strapi.query(details.model || details.collection, details.plugin).findOne({ id : recordId })
|
||||
.then(record => {
|
||||
if (record && _.isObject(record[details.via]) && record[details.via][current] !== value[current]) {
|
||||
return module.exports.update.call(this, {
|
||||
id: record[details.via][this.primaryKey] || record[details.via].id,
|
||||
values: {
|
||||
[current]: null
|
||||
},
|
||||
parseRelationships: false
|
||||
});
|
||||
}
|
||||
|
||||
return Promise.resolve();
|
||||
})
|
||||
);
|
||||
|
||||
// Update the record on the other side.
|
||||
// When params.values[current] is null this means that we are removing the relation.
|
||||
virtualFields.push(strapi.query(details.model || details.collection, details.plugin).update({
|
||||
id: recordId,
|
||||
values: {
|
||||
[details.via]: _.isNull(params.values[current]) ? null : value[this.primaryKey] || value.id || value._id
|
||||
},
|
||||
parseRelationships: false
|
||||
}));
|
||||
|
||||
acc[current] = _.isNull(params.values[current]) ? null : value[current];
|
||||
}
|
||||
|
||||
break;
|
||||
case 'oneToMany':
|
||||
case 'manyToOne':
|
||||
case 'manyToMany':
|
||||
if (response[current] && _.isArray(response[current]) && current !== 'id') {
|
||||
// Records to add in the relation.
|
||||
const toAdd = _.differenceWith(params.values[current], response[current], (a, b) =>
|
||||
a[this.primaryKey].toString() === b[this.primaryKey].toString()
|
||||
);
|
||||
// Records to remove in the relation.
|
||||
const toRemove = _.differenceWith(response[current], params.values[current], (a, b) =>
|
||||
a[this.primaryKey].toString() === b[this.primaryKey].toString()
|
||||
)
|
||||
.filter(x => toAdd.find(y => x.id === y.id) === undefined);
|
||||
|
||||
// Push the work into the flow process.
|
||||
toAdd.forEach(value => {
|
||||
value[details.via] = params.values[this.primaryKey] || params[this.primaryKey];
|
||||
|
||||
virtualFields.push(strapi.query(details.model || details.collection, details.plugin).addRelation({
|
||||
id: value[this.primaryKey] || value.id || value._id,
|
||||
values: association.nature === 'manyToMany' ? params.values : value,
|
||||
foreignKey: current
|
||||
}));
|
||||
});
|
||||
|
||||
toRemove.forEach(value => {
|
||||
value[details.via] = null;
|
||||
|
||||
virtualFields.push(strapi.query(details.model || details.collection, details.plugin).removeRelation({
|
||||
id: value[this.primaryKey] || value.id || value._id,
|
||||
values: association.nature === 'manyToMany' ? params.values : value,
|
||||
foreignKey: current
|
||||
}));
|
||||
});
|
||||
} else if (_.get(this._attributes, `${current}.isVirtual`) !== true) {
|
||||
acc[current] = params.values[current];
|
||||
}
|
||||
|
||||
break;
|
||||
case 'manyMorphToMany':
|
||||
case 'manyMorphToOne':
|
||||
// Update the relational array.
|
||||
params.values[current].forEach(obj => {
|
||||
const model = obj.source && obj.source !== 'content-manager' ?
|
||||
strapi.plugins[obj.source].models[obj.ref]:
|
||||
strapi.models[obj.ref];
|
||||
|
||||
// Remove existing relationship because only one file
|
||||
// can be related to this field.
|
||||
if (association.nature === 'manyMorphToOne') {
|
||||
virtualFields.push(
|
||||
module.exports.removeRelationMorph.call(this, {
|
||||
alias: association.alias,
|
||||
ref: model.collectionName,
|
||||
refId: obj.refId,
|
||||
field: obj.field
|
||||
})
|
||||
.then(() =>
|
||||
module.exports.addRelationMorph.call(this, {
|
||||
id: response[this.primaryKey],
|
||||
alias: association.alias,
|
||||
ref: model.collectionName,
|
||||
refId: obj.refId,
|
||||
field: obj.field
|
||||
})
|
||||
)
|
||||
);
|
||||
} else {
|
||||
virtualFields.push(module.exports.addRelationMorph.call(this, {
|
||||
id: response[this.primaryKey],
|
||||
alias: association.alias,
|
||||
ref: model.collectionName,
|
||||
refId: obj.refId,
|
||||
field: obj.field
|
||||
}));
|
||||
}
|
||||
});
|
||||
break;
|
||||
case 'oneToManyMorph':
|
||||
case 'manyToManyMorph':
|
||||
const transformToArrayID = (array) => {
|
||||
if(_.isArray(array)) {
|
||||
return array.map(value => {
|
||||
if (_.isPlainObject(value)) {
|
||||
return value._id || value.id;
|
||||
}
|
||||
|
||||
return value;
|
||||
})
|
||||
}
|
||||
|
||||
if (association.type === 'model' || (association.type === 'collection' && _.isObject(array))) {
|
||||
return _.isEmpty(_.toString(array)) ? [] : transformToArrayID([array]);
|
||||
}
|
||||
|
||||
return [];
|
||||
};
|
||||
|
||||
// Compare array of ID to find deleted files.
|
||||
const currentValue = transformToArrayID(response[current]).map(id => id.toString());
|
||||
const storedValue = transformToArrayID(params.values[current]).map(id => id.toString());
|
||||
|
||||
const toAdd = _.difference(storedValue, currentValue);
|
||||
const toRemove = _.difference(currentValue, storedValue);
|
||||
|
||||
toAdd.forEach(id => {
|
||||
virtualFields.push(strapi.query(details.model || details.collection, details.plugin).addRelationMorph({
|
||||
id,
|
||||
alias: association.via,
|
||||
ref: this.collectionName,
|
||||
refId: response.id,
|
||||
field: association.alias
|
||||
}));
|
||||
});
|
||||
|
||||
// Update the relational array.
|
||||
toRemove.forEach(id => {
|
||||
virtualFields.push(strapi.query(details.model || details.collection, details.plugin).removeRelationMorph({
|
||||
id,
|
||||
alias: association.via,
|
||||
ref: this.collectionName,
|
||||
refId: response.id,
|
||||
field: association.alias
|
||||
}));
|
||||
});
|
||||
break;
|
||||
case 'oneMorphToOne':
|
||||
case 'oneMorphToMany':
|
||||
break;
|
||||
default:
|
||||
}
|
||||
}
|
||||
|
||||
return acc;
|
||||
}, {});
|
||||
|
||||
if (!_.isEmpty(values)) {
|
||||
virtualFields.push(this
|
||||
.forge({
|
||||
[this.primaryKey]: params[this.primaryKey]
|
||||
})
|
||||
.save(values, {
|
||||
patch: true
|
||||
}));
|
||||
} else {
|
||||
virtualFields.push(Promise.resolve(_.assign(response, params.values)));
|
||||
}
|
||||
|
||||
// Update virtuals fields.
|
||||
const process = await Promise.all(virtualFields);
|
||||
|
||||
return process[process.length - 1];
|
||||
// Call the business logic located in the hook.
|
||||
// This function updates no-relational and relational data.
|
||||
return this.updateRelations(params);
|
||||
},
|
||||
|
||||
delete: async function (params) {
|
||||
@ -331,89 +117,5 @@ module.exports = {
|
||||
[this.primaryKey]: params.id
|
||||
})
|
||||
.destroy();
|
||||
},
|
||||
|
||||
addRelation: async function (params) {
|
||||
const association = this.associations.filter(x => x.via === params.foreignKey)[0];
|
||||
|
||||
if (!association) {
|
||||
// Resolve silently.
|
||||
return Promise.resolve();
|
||||
}
|
||||
|
||||
switch (association.nature) {
|
||||
case 'oneToOne':
|
||||
case 'oneToMany':
|
||||
case 'manyToOne':
|
||||
return module.exports.update.call(this, params);
|
||||
case 'manyToMany':
|
||||
return this.forge({
|
||||
[this.primaryKey]: params[this.primaryKey]
|
||||
})[association.alias]().attach(params.values[this.primaryKey]);
|
||||
default:
|
||||
// Resolve silently.
|
||||
return Promise.resolve();
|
||||
}
|
||||
},
|
||||
|
||||
removeRelation: async function (params) {
|
||||
const association = this.associations.filter(x => x.via === params.foreignKey)[0];
|
||||
|
||||
if (!association) {
|
||||
// Resolve silently.
|
||||
return Promise.resolve();
|
||||
}
|
||||
|
||||
switch (association.nature) {
|
||||
case 'oneToOne':
|
||||
case 'oneToMany':
|
||||
case 'manyToOne':
|
||||
return module.exports.update.call(this, params);
|
||||
case 'manyToMany':
|
||||
return this.forge({
|
||||
[this.primaryKey]: params[this.primaryKey]
|
||||
})[association.alias]().detach(params.values[this.primaryKey]);
|
||||
default:
|
||||
// Resolve silently.
|
||||
return Promise.resolve();
|
||||
}
|
||||
},
|
||||
|
||||
addRelationMorph: async function (params) {
|
||||
const record = await this.morph.forge()
|
||||
.where({
|
||||
[`${this.collectionName}_id`]: params.id,
|
||||
[`${params.alias}_id`]: params.refId,
|
||||
[`${params.alias}_type`]: params.ref,
|
||||
field: params.field
|
||||
})
|
||||
.fetch({
|
||||
withRelated: this.associations.map(x => x.alias)
|
||||
});
|
||||
|
||||
const entry = record ? record.toJSON() : record;
|
||||
|
||||
if (entry) {
|
||||
return Promise.resolve();
|
||||
}
|
||||
|
||||
return await this.morph.forge({
|
||||
[`${this.collectionName}_id`]: params.id,
|
||||
[`${params.alias}_id`]: params.refId,
|
||||
[`${params.alias}_type`]: params.ref,
|
||||
field: params.field
|
||||
})
|
||||
.save();
|
||||
},
|
||||
|
||||
removeRelationMorph: async function (params) {
|
||||
return await this.morph.forge()
|
||||
.where(_.omitBy({
|
||||
[`${this.collectionName}_id`]: params.id,
|
||||
[`${params.alias}_id`]: params.refId,
|
||||
[`${params.alias}_type`]: params.ref,
|
||||
field: params.field
|
||||
}, _.isEmpty))
|
||||
.destroy();
|
||||
}
|
||||
};
|
||||
|
||||
@ -56,244 +56,18 @@ module.exports = {
|
||||
return acc;
|
||||
}, {});
|
||||
|
||||
// Create relationships.
|
||||
return strapi.hook[this.orm].manageRelations(this, _.assign({
|
||||
id: entry[this.primaryKey]
|
||||
}, relations));
|
||||
|
||||
// return module.exports.update.call(this, {
|
||||
// [this.primaryKey]: entry[this.primaryKey],
|
||||
// values: _.assign({
|
||||
// id: entry[this.primaryKey]
|
||||
// }, relations)
|
||||
// });
|
||||
return module.exports.update.call(this, {
|
||||
[this.primaryKey]: entry[this.primaryKey],
|
||||
values: _.assign({
|
||||
id: entry[this.primaryKey]
|
||||
}, relations)
|
||||
});
|
||||
},
|
||||
|
||||
update: async function (params) {
|
||||
const virtualFields = [];
|
||||
const response = await module.exports.findOne.call(this, params);
|
||||
|
||||
// Only update fields which are on this document.
|
||||
const values = params.parseRelationships === false ? params.values : Object.keys(JSON.parse(JSON.stringify(params.values))).reduce((acc, current) => {
|
||||
const association = this.associations.filter(x => x.alias === current)[0];
|
||||
const details = this._attributes[current];
|
||||
|
||||
if (_.get(this._attributes, `${current}.isVirtual`) !== true && _.isUndefined(association)) {
|
||||
acc[current] = params.values[current];
|
||||
} else {
|
||||
switch (association.nature) {
|
||||
case 'oneWay':
|
||||
acc[current] = _.get(params.values[current], this.primaryKey, params.values[current]) || null;
|
||||
|
||||
break;
|
||||
case 'oneToOne':
|
||||
if (response[current] !== params.values[current]) {
|
||||
const value = _.isNull(params.values[current]) ? response[current] : params.values;
|
||||
const recordId = _.isNull(params.values[current]) ? value[this.primaryKey] || value.id || value._id : value[current];
|
||||
|
||||
if (response[current] && _.isObject(response[current]) && response[current][this.primaryKey] !== value[current]) {
|
||||
virtualFields.push(
|
||||
strapi.query(details.collection || details.model, details.plugin).update({
|
||||
id: response[current][this.primaryKey],
|
||||
values: {
|
||||
[details.via]: null
|
||||
},
|
||||
parseRelationships: false
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
// Remove previous relationship asynchronously if it exists.
|
||||
virtualFields.push(
|
||||
strapi.query(details.model || details.collection, details.plugin).findOne({ id : recordId })
|
||||
.then(record => {
|
||||
if (record && _.isObject(record[details.via])) {
|
||||
virtualFields.push(
|
||||
module.exports.update.call(this, {
|
||||
id: record[details.via][this.primaryKey] || record[details.via].id,
|
||||
values: {
|
||||
[current]: null
|
||||
},
|
||||
parseRelationships: false
|
||||
})
|
||||
);
|
||||
}
|
||||
return Promise.resolve();
|
||||
})
|
||||
.then(response => {
|
||||
// Updating the new relations
|
||||
// When params.values[current] is null this means that we are removing the relation.
|
||||
// Recreate relation on the first side
|
||||
virtualFields.push(
|
||||
strapi.query(details.model || details.collection, details.plugin).update({
|
||||
id: recordId,
|
||||
values: {
|
||||
[details.via]: _.isNull(params.values[current]) ? null : value[this.primaryKey] || value.id || value._id
|
||||
},
|
||||
parseRelationships: false
|
||||
})
|
||||
);
|
||||
|
||||
// Recreate relation on the other side if the value is not null
|
||||
if (!_.isNull(params.values[current])) {
|
||||
virtualFields.push(
|
||||
module.exports.update.call(this, {
|
||||
id: value[this.primaryKey] || value.id || value._id,
|
||||
values: {
|
||||
[current]: recordId
|
||||
},
|
||||
parseRelationships: false
|
||||
})
|
||||
);
|
||||
}
|
||||
})
|
||||
);
|
||||
|
||||
acc[current] = _.isNull(params.values[current]) ? null : value[current];
|
||||
}
|
||||
|
||||
break;
|
||||
case 'oneToMany':
|
||||
case 'manyToOne':
|
||||
case 'manyToMany':
|
||||
if (association.nature === 'manyToMany' && details.dominant === true) {
|
||||
acc[current] = params.values[current];
|
||||
} else if (response[current] && _.isArray(response[current]) && current !== 'id') {
|
||||
// Records to add in the relation.
|
||||
const toAdd = _.differenceWith(params.values[current], response[current], (a, b) =>
|
||||
a[this.primaryKey].toString() === b[this.primaryKey].toString()
|
||||
);
|
||||
|
||||
// Records to remove in the relation.
|
||||
const toRemove = _.differenceWith(response[current], params.values[current], (a, b) =>
|
||||
a[this.primaryKey].toString() === b[this.primaryKey].toString()
|
||||
)
|
||||
.filter(x => toAdd.find(y => x.id === y.id) === undefined);
|
||||
|
||||
// Push the work into the flow process.
|
||||
toAdd.forEach(value => {
|
||||
if (association.nature === 'manyToMany' && !_.isArray(params.values[this.primaryKey] || params[this.primaryKey])) {
|
||||
value[details.via] = (value[details.via] || [])
|
||||
.concat([(params.values[this.primaryKey] || params[this.primaryKey])])
|
||||
.filter(x => {
|
||||
return x !== null && x !== undefined;
|
||||
});
|
||||
} else {
|
||||
value[details.via] = params[this.primaryKey] || params.id;
|
||||
}
|
||||
|
||||
virtualFields.push(strapi.query(details.model || details.collection, details.plugin).addRelation({
|
||||
id: value[this.primaryKey] || value.id || value._id,
|
||||
values: _.pick(value, [this.primaryKey, details.via]),
|
||||
foreignKey: current
|
||||
}));
|
||||
});
|
||||
|
||||
toRemove.forEach(value => {
|
||||
if (association.nature === 'manyToMany' && !_.isArray(params.values[this.primaryKey])) {
|
||||
value[details.via] = value[details.via].filter(x => x.toString() !== params.values[this.primaryKey].toString());
|
||||
} else {
|
||||
value[details.via] = null;
|
||||
}
|
||||
|
||||
virtualFields.push(strapi.query(details.model || details.collection, details.plugin).removeRelation({
|
||||
id: value[this.primaryKey] || value.id || value._id,
|
||||
values: _.pick(value, [this.primaryKey, details.via]),
|
||||
foreignKey: current
|
||||
}));
|
||||
});
|
||||
} else if (_.get(this._attributes, `${current}.isVirtual`) !== true) {
|
||||
acc[current] = params.values[current];
|
||||
}
|
||||
|
||||
break;
|
||||
case 'manyMorphToMany':
|
||||
case 'manyMorphToOne':
|
||||
// Update the relational array.
|
||||
acc[current] = params.values[current].map(obj => {
|
||||
const globalId = obj.source && obj.source !== 'content-manager' ?
|
||||
strapi.plugins[obj.source].models[_.toLower(obj.ref)].globalId:
|
||||
strapi.models[_.toLower(obj.ref)].globalId;
|
||||
|
||||
// Define the object stored in database.
|
||||
// The shape is this object is defined by the strapi-mongoose connector.
|
||||
return {
|
||||
ref: obj.refId,
|
||||
kind: globalId,
|
||||
[association.filter]: obj.field
|
||||
}
|
||||
});
|
||||
break;
|
||||
case 'oneToManyMorph':
|
||||
case 'manyToManyMorph':
|
||||
const transformToArrayID = (array) => {
|
||||
if (_.isArray(array)) {
|
||||
return array.map(value => {
|
||||
if (_.isPlainObject(value)) {
|
||||
return value._id || value.id;
|
||||
}
|
||||
|
||||
return value;
|
||||
})
|
||||
}
|
||||
|
||||
if (association.type === 'model' || (association.type === 'collection' && _.isObject(array))) {
|
||||
return _.isEmpty(array) ? [] : transformToArrayID([array]);
|
||||
}
|
||||
|
||||
return [];
|
||||
};
|
||||
|
||||
// Compare array of ID to find deleted files.
|
||||
const currentValue = transformToArrayID(response[current]).map(id => id.toString());
|
||||
const storedValue = transformToArrayID(params.values[current]).map(id => id.toString());
|
||||
|
||||
const toAdd = _.difference(storedValue, currentValue);
|
||||
const toRemove = _.difference(currentValue, storedValue);
|
||||
|
||||
// Remove relations in the other side.
|
||||
toAdd.forEach(id => {
|
||||
virtualFields.push(strapi.query(details.model || details.collection, details.plugin).addRelationMorph({
|
||||
id,
|
||||
alias: association.via,
|
||||
ref: this.globalId,
|
||||
refId: response._id,
|
||||
field: association.alias
|
||||
}));
|
||||
});
|
||||
|
||||
// Remove relations in the other side.
|
||||
toRemove.forEach(id => {
|
||||
virtualFields.push(strapi.query(details.model || details.collection, details.plugin).removeRelationMorph({
|
||||
id,
|
||||
alias: association.via,
|
||||
ref: this.globalId,
|
||||
refId: response._id,
|
||||
field: association.alias
|
||||
}));
|
||||
});
|
||||
break;
|
||||
case 'oneMorphToOne':
|
||||
case 'oneMorphToMany':
|
||||
break;
|
||||
default:
|
||||
}
|
||||
}
|
||||
|
||||
return acc;
|
||||
}, {});
|
||||
|
||||
virtualFields.push(this
|
||||
.update({
|
||||
[this.primaryKey]: params[this.primaryKey] || params.id
|
||||
}, values, {
|
||||
strict: false
|
||||
}));
|
||||
|
||||
// Update virtuals fields.
|
||||
await Promise.all(virtualFields);
|
||||
|
||||
return await module.exports.findOne.call(this, params);
|
||||
// Call the business logic located in the hook.
|
||||
// This function updates no-relational and relational data.
|
||||
return this.updateRelations(params);
|
||||
},
|
||||
|
||||
delete: async function (params) {
|
||||
@ -302,78 +76,5 @@ module.exports = {
|
||||
.remove({
|
||||
[this.primaryKey]: params.id
|
||||
});
|
||||
},
|
||||
|
||||
addRelation: async function (params) {
|
||||
return module.exports.update.call(this, params);
|
||||
},
|
||||
|
||||
removeRelation: async function (params) {
|
||||
return module.exports.update.call(this, params);
|
||||
},
|
||||
|
||||
addRelationMorph: async function (params) {
|
||||
/*
|
||||
TODO:
|
||||
Test this part because it has been coded during the development of the upload feature.
|
||||
However the upload doesn't need this method. It only uses the `removeRelationMorph`.
|
||||
*/
|
||||
|
||||
const entry = (await module.exports.findOne.call(this, params, [], false)).toJSON();
|
||||
const value = [];
|
||||
|
||||
// Retrieve association.
|
||||
const association = this.associations.find(association => association.alias === params.alias);
|
||||
|
||||
if (!association) {
|
||||
throw Error(`Impossible to create relationship with ${params.ref} (${params.refId})`);
|
||||
}
|
||||
|
||||
// Resolve if the association is already existing.
|
||||
const isExisting = value.find(obj => {
|
||||
if (obj.kind === params.ref && obj.ref.toString() === params.refId.toString() && obj.field === params.field) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
});
|
||||
|
||||
// Avoid duplicate.
|
||||
if (isExisting) {
|
||||
return Promise.resolve();
|
||||
}
|
||||
|
||||
// Push new relation to the association array.
|
||||
value.push({
|
||||
ref: params.ref,
|
||||
refId: params.refId,
|
||||
kind: params.ref,
|
||||
field: params.field
|
||||
});
|
||||
|
||||
entry[params.alias] = value;
|
||||
|
||||
return module.exports.update.call(this, {
|
||||
id: params.id,
|
||||
values: entry
|
||||
});
|
||||
},
|
||||
|
||||
removeRelationMorph: async function (params) {
|
||||
const entry = await module.exports.findOne.call(this, params, []);
|
||||
|
||||
// Filter the association array and remove the association.
|
||||
entry[params.alias] = entry[params.alias].filter(obj => {
|
||||
if (obj.kind === params.ref && obj.ref.toString() === params.refId.toString() && obj.field === params.field) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
});
|
||||
|
||||
return module.exports.update.call(this, {
|
||||
id: params.id,
|
||||
values: entry
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
@ -37,7 +37,7 @@ module.exports = {
|
||||
},
|
||||
|
||||
findOne: async function (params, populate) {
|
||||
const primaryKey = params[this.primaryKey] || params.id;
|
||||
const primaryKey = params[this.primaryKey] || params._id;
|
||||
|
||||
if (primaryKey) {
|
||||
params = {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user