Rework one-to-one business logic and use getValuePrimaryKey util with Mongoose

This commit is contained in:
Aurelsicoko 2018-05-10 12:19:33 +02:00
parent 78517da354
commit 49c61d2ca5
2 changed files with 47 additions and 55 deletions

View File

@ -7,6 +7,9 @@
// Public node modules. // Public node modules.
const _ = require('lodash'); const _ = require('lodash');
// Utils
const { models: { getValuePrimaryKey } } = require('strapi-utils');
module.exports = { module.exports = {
getModel: function (model, plugin) { 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;
@ -22,7 +25,7 @@ module.exports = {
// Only update fields which are on this document. // 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 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 association = this.associations.find(x => x.alias === current);
const details = this._attributes[current]; const details = this._attributes[current];
if (_.get(this._attributes, `${current}.isVirtual`) !== true && _.isUndefined(association)) { if (_.get(this._attributes, `${current}.isVirtual`) !== true && _.isUndefined(association)) {
@ -36,68 +39,49 @@ module.exports = {
case 'oneToOne': case 'oneToOne':
if (response[current] !== params.values[current]) { if (response[current] !== params.values[current]) {
const value = _.isNull(params.values[current]) ? response[current] : params.values; 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]; const recordId = _.isNull(params.values[current]) ? getValuePrimaryKey(value, this.primaryKey) : value[current];
const model = module.exports.getModel(details.model || details.collection, details.plugin); const model = module.exports.getModel(details.model || details.collection, details.plugin);
if (response[current] && _.isObject(response[current]) && response[current][this.primaryKey] !== value[current]) { // Remove relation in the user side.
virtualFields.push(
module.exports.update.call(model, {
id: response[current][this.primaryKey],
values: {
[details.via]: null
},
parseRelationships: false
})
);
}
// Remove previous relationship asynchronously if it exists.
virtualFields.push( virtualFields.push(
model model
.findOne({ [model.primaryKey] : recordId }) .findOne({ [model.primaryKey]: value[current] })
.populate(details.via) .populate(details.via)
.then(record => { .then(record => {
if (record && _.isObject(record[details.via])) { if (record && _.isObject(record[details.via])) {
virtualFields.push( return module.exports.update.call(this, {
module.exports.update.call(model, { id: getValuePrimaryKey(record[details.via], model.primaryKey),
id: record[details.via][this.primaryKey] || record[details.via].id, values: {
values: { [current]: null
[current]: null },
}, parseRelationships: false
parseRelationships: false })
})
);
} }
return Promise.resolve(); return Promise.resolve();
}) })
.then(response => { .then(() => {
// Updating the new relations return module.exports.update.call(model, {
// When params.values[current] is null this means that we are removing the relation. id: getValuePrimaryKey(response[current] || {}, this.primaryKey) || value[current],
// Recreate relation on the first side values: {
virtualFields.push( [details.via]: null
module.exports.update.call(model, { },
parseRelationships: false
})
})
.then(() => {
if (!_.isNull(params.values[current])) {
return module.exports.update.call(model, {
id: recordId, id: recordId,
values: { values: {
[details.via]: params[this.primaryKey] || params.id || null [details.via]: getValuePrimaryKey(params, this.primaryKey) || null
}, },
parseRelationships: false 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 || null,
values: {
[current]: recordId
},
parseRelationships: false
})
);
} }
return Promise.resolve();
}) })
); );
@ -135,12 +119,12 @@ module.exports = {
return x !== null && x !== undefined; return x !== null && x !== undefined;
}); });
} else { } else {
value[details.via] = params[this.primaryKey] || params.id; value[details.via] = getValuePrimaryKey(params, this.primaryKey);
} }
virtualFields.push( virtualFields.push(
module.exports.addRelation.call(model, { module.exports.addRelation.call(model, {
id: value[this.primaryKey] || value.id || value._id, id: getValuePrimaryKey(value, this.primaryKey),
values: _.pick(value, [this.primaryKey, details.via]), values: _.pick(value, [this.primaryKey, details.via]),
foreignKey: current foreignKey: current
}) })
@ -150,7 +134,7 @@ module.exports = {
toRemove.forEach(value => { toRemove.forEach(value => {
value = _.isString(value) ? { [this.primaryKey]: value } : value; value = _.isString(value) ? { [this.primaryKey]: value } : value;
if (association.nature === 'manyToMany' && !_.isArray(params.values[this.primaryKey])) { if (association.nature === 'manyToMany' && !_.isArray(params.values[this.primaryKey]) || params[this.primaryKey]) {
value[details.via] = value[details.via].filter(x => x.toString() !== params.values[this.primaryKey].toString()); value[details.via] = value[details.via].filter(x => x.toString() !== params.values[this.primaryKey].toString());
} else { } else {
value[details.via] = null; value[details.via] = null;
@ -158,7 +142,7 @@ module.exports = {
virtualFields.push( virtualFields.push(
module.exports.removeRelation.call(model, { module.exports.removeRelation.call(model, {
id: value[this.primaryKey] || value.id || value._id, id: getValuePrimaryKey(value, this.primaryKey),
values: _.pick(value, [this.primaryKey, details.via]), values: _.pick(value, [this.primaryKey, details.via]),
foreignKey: current foreignKey: current
}) })
@ -192,7 +176,7 @@ module.exports = {
if (_.isArray(array)) { if (_.isArray(array)) {
return array.map(value => { return array.map(value => {
if (_.isPlainObject(value)) { if (_.isPlainObject(value)) {
return value._id || value.id; return getValuePrimaryKey(value, this.primaryKey);
} }
return value; return value;
@ -254,7 +238,7 @@ module.exports = {
virtualFields.push( virtualFields.push(
this this
.update({ .update({
[this.primaryKey]: params[this.primaryKey] || params.id [this.primaryKey]: getValuePrimaryKey(params, this.primaryKey)
}, values, { }, values, {
strict: false strict: false
}) })
@ -265,7 +249,7 @@ module.exports = {
return await this return await this
.findOne({ .findOne({
[this.primaryKey]: params[this.primaryKey] || params.id [this.primaryKey]: getValuePrimaryKey(params, this.primaryKey)
}) })
.populate(this.associations.map(x => x.alias).join(' ')); .populate(this.associations.map(x => x.alias).join(' '));
}, },
@ -288,7 +272,7 @@ module.exports = {
const entry = ( const entry = (
await this await this
.findOne({ .findOne({
[this.primaryKey]: params[this.primaryKey] || params.id [this.primaryKey]: getValuePrimaryKey(params, this.primaryKey)
}) })
.toJSON() .toJSON()
); );
@ -333,7 +317,7 @@ module.exports = {
removeRelationMorph: async function (params) { removeRelationMorph: async function (params) {
const entry = await this.findOne({ const entry = await this.findOne({
[this.primaryKey]: params[this.primaryKey] || params.id [this.primaryKey]: getValuePrimaryKey(params, this.primaryKey)
}); });
// Filter the association array and remove the association. // Filter the association array and remove the association.

View File

@ -46,6 +46,14 @@ module.exports = {
return undefined; return undefined;
}, },
/**
* Retrieve the value based on the primary key
*/
getValuePrimaryKey: (value, defaultKey) => {
return value[defaultKey] || value.id || value._id;
},
/** /**
* Find primary key per ORM * Find primary key per ORM
*/ */