2017-09-07 19:11:12 +02:00
|
|
|
const _ = require('lodash');
|
|
|
|
|
2017-06-17 17:01:50 +02:00
|
|
|
module.exports = {
|
2017-08-29 19:34:34 +02:00
|
|
|
find: async function (params) {
|
|
|
|
return this
|
2017-06-17 17:01:50 +02:00
|
|
|
.find()
|
|
|
|
.limit(Number(params.limit))
|
|
|
|
.sort(params.sort)
|
|
|
|
.skip(Number(params.skip));
|
|
|
|
},
|
|
|
|
|
2017-08-29 19:34:34 +02:00
|
|
|
count: async function (params) {
|
|
|
|
return Number(await this
|
|
|
|
.count());
|
2017-06-17 17:01:50 +02:00
|
|
|
},
|
|
|
|
|
2018-02-28 12:33:32 +01:00
|
|
|
findOne: async function (params, populate) {
|
2017-09-07 19:11:12 +02:00
|
|
|
return this
|
2017-08-29 19:34:34 +02:00
|
|
|
.findOne({
|
2017-09-18 16:41:31 +02:00
|
|
|
[this.primaryKey]: params[this.primaryKey] || params.id
|
2017-09-07 17:16:31 +02:00
|
|
|
})
|
2018-02-28 12:33:32 +01:00
|
|
|
.populate(populate || this.associations.map(x => x.alias).join(' '))
|
2018-02-27 16:53:06 +01:00
|
|
|
.lean();
|
2017-06-17 17:01:50 +02:00
|
|
|
},
|
|
|
|
|
2017-08-29 19:34:34 +02:00
|
|
|
create: async function (params) {
|
2018-02-27 11:52:18 +01:00
|
|
|
// Exclude relationships.
|
|
|
|
const values = Object.keys(params.values).reduce((acc, current) => {
|
2018-02-27 16:53:06 +01:00
|
|
|
if (this._attributes[current] && this._attributes[current].type) {
|
2017-09-18 16:41:31 +02:00
|
|
|
acc[current] = params.values[current];
|
|
|
|
}
|
|
|
|
|
|
|
|
return acc;
|
2018-02-27 11:52:18 +01:00
|
|
|
}, {});
|
2017-12-06 15:58:20 +01:00
|
|
|
|
2018-02-27 11:52:18 +01:00
|
|
|
const entry = await this.create(values)
|
|
|
|
.catch((err) => {
|
|
|
|
const message = err.message.split('index:');
|
|
|
|
const field = _.words(_.last(message).split('_')[0]);
|
|
|
|
const error = { message: `This ${field} is already taken`, field };
|
|
|
|
|
|
|
|
throw error;
|
|
|
|
});
|
2017-09-18 16:41:31 +02:00
|
|
|
|
|
|
|
return module.exports.update.call(this, {
|
|
|
|
[this.primaryKey]: entry[this.primaryKey],
|
2017-09-18 18:20:33 +02:00
|
|
|
values: _.merge({
|
|
|
|
id: entry[this.primaryKey]
|
|
|
|
}, params.values)
|
2017-09-18 16:41:31 +02:00
|
|
|
});
|
2017-06-17 17:01:50 +02:00
|
|
|
},
|
|
|
|
|
2017-08-29 19:34:34 +02:00
|
|
|
update: async function (params) {
|
2017-09-15 18:29:50 +02:00
|
|
|
const virtualFields = [];
|
|
|
|
const response = await module.exports.findOne.call(this, params);
|
|
|
|
|
|
|
|
// Only update fields which are on this document.
|
2017-09-18 16:41:31 +02:00
|
|
|
const values = params.parseRelationships === false ? params.values : Object.keys(JSON.parse(JSON.stringify(params.values))).reduce((acc, current) => {
|
2017-09-15 18:29:50 +02:00
|
|
|
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) {
|
2018-01-08 12:10:48 +01:00
|
|
|
case 'oneWay':
|
|
|
|
acc[current] = _.get(params.values[current], this.primaryKey, params.values[current]) || null;
|
|
|
|
|
|
|
|
break;
|
2017-09-15 18:29:50 +02:00
|
|
|
case 'oneToOne':
|
2017-09-18 16:41:31 +02:00
|
|
|
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(
|
2017-12-11 18:23:15 +01:00
|
|
|
strapi.query(details.collection || details.model, details.plugin).update({
|
2017-09-18 16:41:31 +02:00
|
|
|
id: response[current][this.primaryKey],
|
|
|
|
values: {
|
|
|
|
[details.via]: null
|
|
|
|
},
|
|
|
|
parseRelationships: false
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove previous relationship asynchronously if it exists.
|
|
|
|
virtualFields.push(
|
2017-12-11 18:23:15 +01:00
|
|
|
strapi.query(details.model || details.collection, details.plugin).findOne({ id : recordId })
|
2017-09-18 16:41:31 +02:00
|
|
|
.then(record => {
|
|
|
|
if (record && _.isObject(record[details.via])) {
|
|
|
|
return module.exports.update.call(this, {
|
|
|
|
id: record[details.via][this.primaryKey] || record[details.via].id,
|
|
|
|
values: {
|
|
|
|
[current]: null
|
|
|
|
},
|
|
|
|
parseRelationships: false
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return Promise.resolve();
|
|
|
|
})
|
|
|
|
);
|
2017-09-15 18:29:50 +02:00
|
|
|
|
|
|
|
// Update the record on the other side.
|
|
|
|
// When params.values[current] is null this means that we are removing the relation.
|
2017-12-11 18:23:15 +01:00
|
|
|
virtualFields.push(strapi.query(details.model || details.collection, details.plugin).update({
|
2017-09-18 16:41:31 +02:00
|
|
|
id: recordId,
|
2017-09-15 18:29:50 +02:00
|
|
|
values: {
|
|
|
|
[details.via]: _.isNull(params.values[current]) ? null : value[this.primaryKey] || value.id || value._id
|
2017-09-18 16:41:31 +02:00
|
|
|
},
|
|
|
|
parseRelationships: false
|
2017-09-15 18:29:50 +02:00
|
|
|
}));
|
|
|
|
|
2017-09-18 16:41:31 +02:00
|
|
|
acc[current] = _.isNull(params.values[current]) ? null : value[current];
|
2017-09-15 18:29:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
case 'oneToMany':
|
2017-09-18 16:41:31 +02:00
|
|
|
case 'manyToOne':
|
2017-09-15 18:29:50 +02:00
|
|
|
case 'manyToMany':
|
|
|
|
if (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()
|
|
|
|
);
|
2018-01-23 15:38:43 +01:00
|
|
|
|
2017-09-15 18:29:50 +02:00
|
|
|
// 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 => {
|
2017-10-23 17:35:00 +02:00
|
|
|
if (association.nature === 'manyToMany' && !_.isArray(params.values[this.primaryKey] || params[this.primaryKey])) {
|
2017-12-11 18:23:15 +01:00
|
|
|
value[details.via] = (value[details.via] || [])
|
|
|
|
.concat([(params.values[this.primaryKey] || params[this.primaryKey])])
|
|
|
|
.filter(x => {
|
|
|
|
return x !== null && x !== undefined;
|
|
|
|
});
|
2017-09-15 18:29:50 +02:00
|
|
|
} else {
|
2017-10-23 17:07:57 +02:00
|
|
|
value[details.via] = params[this.primaryKey] || params.id;
|
2017-09-15 18:29:50 +02:00
|
|
|
}
|
|
|
|
|
2017-12-11 18:23:15 +01:00
|
|
|
virtualFields.push(strapi.query(details.model || details.collection, details.plugin).addRelation({
|
2017-09-15 18:29:50 +02:00
|
|
|
id: value[this.primaryKey] || value.id || value._id,
|
|
|
|
values: value,
|
|
|
|
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 {
|
2017-09-18 17:58:29 +02:00
|
|
|
value[details.via] = null;
|
2017-09-15 18:29:50 +02:00
|
|
|
}
|
|
|
|
|
2017-12-11 18:23:15 +01:00
|
|
|
virtualFields.push(strapi.query(details.model || details.collection, details.plugin).removeRelation({
|
2017-09-15 18:29:50 +02:00
|
|
|
id: value[this.primaryKey] || value.id || value._id,
|
|
|
|
values: value,
|
|
|
|
foreignKey: current
|
|
|
|
}));
|
|
|
|
});
|
|
|
|
} else if (_.get(this._attributes, `${current}.isVirtual`) !== true) {
|
|
|
|
acc[current] = params.values[current];
|
|
|
|
}
|
|
|
|
|
2018-02-27 18:57:28 +01:00
|
|
|
break;
|
|
|
|
case 'manyMorphToMany':
|
|
|
|
case 'manyMorphToOne':
|
2018-02-28 15:49:28 +01:00
|
|
|
// Update the relational array.
|
|
|
|
acc[current] = params.values[current].map(obj => {
|
|
|
|
const globalId = obj.source && obj.source !== 'content-manager' ?
|
|
|
|
strapi.plugins[obj.source].models[obj.ref].globalId:
|
|
|
|
strapi.models[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
|
|
|
|
}
|
2018-02-28 12:33:32 +01:00
|
|
|
});
|
|
|
|
break;
|
2018-03-02 12:01:18 +01:00
|
|
|
case 'oneToManyMorph':
|
2018-02-28 12:33:32 +01:00
|
|
|
case 'manyToManyMorph':
|
|
|
|
const transformToArrayID = (array) => {
|
2018-03-02 16:31:32 +01:00
|
|
|
if (_.isArray(array)) {
|
|
|
|
return array.map(value => {
|
|
|
|
if (_.isPlainObject(value)) {
|
|
|
|
return value._id || value.id;
|
|
|
|
}
|
|
|
|
|
|
|
|
return value;
|
|
|
|
})
|
|
|
|
}
|
2018-02-28 12:33:32 +01:00
|
|
|
|
2018-03-02 16:31:32 +01:00
|
|
|
if (_.isPlainObject(array)) {
|
|
|
|
return _.isEmpty(array) ? [] : transformToArrayID([array]);
|
|
|
|
}
|
2018-02-28 12:33:32 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
// 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;
|
2018-02-27 18:57:28 +01:00
|
|
|
case 'oneMorphToOne':
|
|
|
|
case 'oneMorphToMany':
|
2017-09-15 18:29:50 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return acc;
|
|
|
|
}, {});
|
|
|
|
|
|
|
|
virtualFields.push(this
|
2017-08-29 19:34:34 +02:00
|
|
|
.update({
|
2017-09-18 16:41:31 +02:00
|
|
|
[this.primaryKey]: params[this.primaryKey] || params.id
|
2017-09-15 18:29:50 +02:00
|
|
|
}, values, {
|
2017-09-07 17:16:31 +02:00
|
|
|
strict: false
|
2017-09-15 18:29:50 +02:00
|
|
|
}));
|
|
|
|
|
|
|
|
// Update virtuals fields.
|
2018-02-27 16:53:06 +01:00
|
|
|
await Promise.all(virtualFields);
|
2017-09-15 18:29:50 +02:00
|
|
|
|
2018-02-27 16:53:06 +01:00
|
|
|
return await module.exports.findOne.call(this, params);
|
2017-06-17 17:01:50 +02:00
|
|
|
},
|
|
|
|
|
2017-08-29 19:34:34 +02:00
|
|
|
delete: async function (params) {
|
2017-09-20 13:19:36 +02:00
|
|
|
// Delete entry.
|
2017-09-15 18:29:50 +02:00
|
|
|
return this
|
2017-08-31 17:26:44 +02:00
|
|
|
.remove({
|
2017-09-20 18:14:18 +02:00
|
|
|
[this.primaryKey]: params.id
|
2017-08-29 19:34:34 +02:00
|
|
|
});
|
2017-09-14 18:47:10 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
addRelation: async function (params) {
|
|
|
|
return module.exports.update.call(this, params);
|
|
|
|
},
|
|
|
|
|
|
|
|
removeRelation: async function (params) {
|
|
|
|
return module.exports.update.call(this, params);
|
2018-02-28 12:33:32 +01:00
|
|
|
},
|
|
|
|
|
2018-02-28 15:49:28 +01:00
|
|
|
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, []);
|
|
|
|
const value = entry[params.alias] || [];
|
|
|
|
|
|
|
|
// Retrieve association.
|
|
|
|
const association = this.associations.find(association => association.via === params.alias)[0];
|
|
|
|
|
|
|
|
if (!association) {
|
|
|
|
throw Error(`Impossible to create relationship with ${params.ref} (${params.refId})`);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Resolve if the association is already existing.
|
|
|
|
const isExisting = entry[params.alias].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.refId,
|
|
|
|
kind: params.ref,
|
|
|
|
field: association.filter
|
|
|
|
});
|
|
|
|
|
|
|
|
entry[params.alias] = value;
|
|
|
|
|
|
|
|
return module.exports.update.call(this, {
|
|
|
|
id: params.id,
|
|
|
|
values: entry
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2018-02-28 12:33:32 +01:00
|
|
|
removeRelationMorph: async function (params) {
|
|
|
|
const entry = await module.exports.findOne.call(this, params, []);
|
|
|
|
|
2018-02-28 15:49:28 +01:00
|
|
|
// Filter the association array and remove the association.
|
2018-02-28 12:33:32 +01:00
|
|
|
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
|
|
|
|
});
|
2017-06-17 17:01:50 +02:00
|
|
|
}
|
2017-08-29 19:34:34 +02:00
|
|
|
};
|