2016-07-12 11:15:01 +02:00
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Module dependencies
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
// Public node modules.
|
|
|
|
|
const _ = require('lodash');
|
2018-04-24 15:24:42 +02:00
|
|
|
|
const mongoose = require('mongoose');
|
|
|
|
|
const Mongoose = mongoose.Mongoose;
|
2016-08-05 15:51:55 +02:00
|
|
|
|
const mongooseUtils = require('mongoose/lib/utils');
|
2016-07-12 11:15:01 +02:00
|
|
|
|
|
2016-07-14 11:06:58 +02:00
|
|
|
|
// Local helpers.
|
|
|
|
|
const utils = require('./utils/');
|
|
|
|
|
|
2016-07-12 11:15:01 +02:00
|
|
|
|
// Strapi helpers for models.
|
2017-08-17 18:25:52 +02:00
|
|
|
|
const { models: utilsModels, logger } = require('strapi-utils');
|
2016-07-12 11:15:01 +02:00
|
|
|
|
|
|
|
|
|
/**
|
2017-11-17 14:23:26 +01:00
|
|
|
|
* Mongoose hook
|
2016-07-12 11:15:01 +02:00
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
module.exports = function (strapi) {
|
|
|
|
|
const hook = {
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Default options
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
defaults: {
|
2017-01-04 17:21:24 +01:00
|
|
|
|
defaultConnection: 'default',
|
|
|
|
|
host: 'localhost',
|
|
|
|
|
port: 27017,
|
2018-03-04 23:02:29 +08:00
|
|
|
|
database: 'strapi',
|
2018-03-08 15:51:03 +01:00
|
|
|
|
authenticationDatabase: '',
|
2018-03-04 23:02:29 +08:00
|
|
|
|
ssl: false
|
2016-07-12 11:15:01 +02:00
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Initialize the hook
|
|
|
|
|
*/
|
|
|
|
|
|
2016-11-07 16:31:34 +01:00
|
|
|
|
initialize: cb => {
|
2017-01-04 17:21:24 +01:00
|
|
|
|
_.forEach(_.pickBy(strapi.config.connections, {connector: 'strapi-mongoose'}), (connection, connectionName) => {
|
2017-10-10 15:23:49 +02:00
|
|
|
|
const instance = new Mongoose();
|
2018-04-24 16:38:19 +02:00
|
|
|
|
const { uri, host, port, username, password, database } = _.defaults(connection.settings, strapi.config.hook.settings.mongoose);
|
|
|
|
|
const { authenticationDatabase, ssl } = _.defaults(connection.options, strapi.config.hook.settings.mongoose);
|
2016-07-12 11:15:01 +02:00
|
|
|
|
|
2017-01-04 17:21:24 +01:00
|
|
|
|
// Connect to mongo database
|
2018-01-23 07:23:15 +01:00
|
|
|
|
const connectOptions = {}
|
2018-04-24 16:38:19 +02:00
|
|
|
|
|
2018-01-23 07:23:15 +01:00
|
|
|
|
if (!_.isEmpty(username)) {
|
2018-04-24 16:38:19 +02:00
|
|
|
|
connectOptions.user = username;
|
|
|
|
|
|
2018-01-23 07:26:57 +01:00
|
|
|
|
if (!_.isEmpty(password)) {
|
2018-04-24 16:38:19 +02:00
|
|
|
|
connectOptions.pass = password;
|
2018-01-23 07:26:57 +01:00
|
|
|
|
}
|
2017-01-04 17:21:24 +01:00
|
|
|
|
}
|
2018-04-24 16:38:19 +02:00
|
|
|
|
|
2018-03-08 15:51:03 +01:00
|
|
|
|
if (!_.isEmpty(authenticationDatabase)) {
|
|
|
|
|
connectOptions.authSource = authenticationDatabase;
|
|
|
|
|
}
|
2018-04-24 16:38:19 +02:00
|
|
|
|
|
2018-04-12 15:23:03 +02:00
|
|
|
|
connectOptions.ssl = ssl === true || ssl === 'true';
|
2018-02-07 00:19:05 +01:00
|
|
|
|
|
|
|
|
|
instance.connect(uri || `mongodb://${host}:${port}/${database}`, connectOptions);
|
2016-07-12 11:15:01 +02:00
|
|
|
|
|
2017-01-04 17:21:24 +01:00
|
|
|
|
// Handle error
|
2017-10-10 15:23:49 +02:00
|
|
|
|
instance.connection.on('error', error => {
|
2017-08-17 18:25:52 +02:00
|
|
|
|
if (error.message.indexOf(`:${port}`)) {
|
|
|
|
|
return cb('Make sure your MongoDB database is running...');
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-04 17:21:24 +01:00
|
|
|
|
cb(error);
|
|
|
|
|
});
|
2016-08-04 13:21:22 +02:00
|
|
|
|
|
2017-01-04 17:21:24 +01:00
|
|
|
|
// Handle success
|
2017-10-10 15:23:49 +02:00
|
|
|
|
instance.connection.on('open', () => {
|
2017-11-15 16:59:12 +01:00
|
|
|
|
const mountModels = (models, target, plugin = false) => {
|
2017-11-15 15:06:09 +01:00
|
|
|
|
if (!target) return;
|
|
|
|
|
|
|
|
|
|
const loadedAttributes = _.after(_.size(models), () => {
|
|
|
|
|
_.forEach(models, (definition, model) => {
|
|
|
|
|
try {
|
|
|
|
|
let collection = strapi.config.hook.settings.mongoose.collections[mongooseUtils.toCollectionName(definition.globalName)];
|
|
|
|
|
|
|
|
|
|
// Set the default values to model settings.
|
|
|
|
|
_.defaults(definition, {
|
|
|
|
|
primaryKey: '_id'
|
2017-01-04 17:21:24 +01:00
|
|
|
|
});
|
2016-08-04 13:21:22 +02:00
|
|
|
|
|
2017-11-15 15:06:09 +01:00
|
|
|
|
// Initialize lifecycle callbacks.
|
|
|
|
|
const preLifecycle = {
|
|
|
|
|
validate: 'beforeCreate',
|
2018-02-12 18:54:34 +01:00
|
|
|
|
findOneAndUpdate: 'beforeUpdate',
|
2017-12-19 14:47:48 +01:00
|
|
|
|
findOneAndRemove: 'beforeDestroy',
|
2017-11-15 15:06:09 +01:00
|
|
|
|
remove: 'beforeDestroy',
|
|
|
|
|
update: 'beforeUpdate',
|
|
|
|
|
find: 'beforeFetchAll',
|
|
|
|
|
findOne: 'beforeFetch',
|
|
|
|
|
save: 'beforeSave'
|
|
|
|
|
};
|
2016-08-04 13:21:22 +02:00
|
|
|
|
|
2018-02-12 18:54:34 +01:00
|
|
|
|
/*
|
|
|
|
|
Override populate path for polymorphic association.
|
|
|
|
|
|
|
|
|
|
It allows us to make Upload.find().populate('related')
|
|
|
|
|
instead of Upload.find().populate('related.item')
|
|
|
|
|
*/
|
2018-02-26 11:12:49 +01:00
|
|
|
|
|
2018-02-12 18:54:34 +01:00
|
|
|
|
const morphAssociations = definition.associations.filter(association => association.nature.toLowerCase().indexOf('morph') !== -1);
|
|
|
|
|
|
|
|
|
|
if (morphAssociations.length > 0) {
|
|
|
|
|
morphAssociations.forEach(association => {
|
|
|
|
|
Object.keys(preLifecycle)
|
|
|
|
|
.filter(key => key.indexOf('find') !== -1)
|
|
|
|
|
.forEach(key => {
|
|
|
|
|
collection.schema.pre(key, function (next) {
|
|
|
|
|
if (this._mongooseOptions.populate && this._mongooseOptions.populate[association.alias]) {
|
2018-02-22 16:08:11 +01:00
|
|
|
|
if (association.nature === 'oneToManyMorph' || association.nature === 'manyToManyMorph') {
|
2018-02-12 18:54:34 +01:00
|
|
|
|
this._mongooseOptions.populate[association.alias].match = {
|
2018-02-22 15:34:33 +01:00
|
|
|
|
[`${association.via}.${association.filter}`]: association.alias,
|
2018-02-12 19:04:36 +01:00
|
|
|
|
[`${association.via}.kind`]: definition.globalId
|
2018-02-12 18:54:34 +01:00
|
|
|
|
}
|
2018-04-23 12:59:53 +02:00
|
|
|
|
|
|
|
|
|
// Select last related to an entity.
|
|
|
|
|
this._mongooseOptions.populate[association.alias].options = {
|
|
|
|
|
sort: '-createdAt'
|
|
|
|
|
}
|
2018-02-12 18:54:34 +01:00
|
|
|
|
} else {
|
2018-02-22 16:08:11 +01:00
|
|
|
|
this._mongooseOptions.populate[association.alias].path = `${association.alias}.ref`;
|
2018-02-12 18:54:34 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
next();
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-15 15:06:09 +01:00
|
|
|
|
_.forEach(preLifecycle, (fn, key) => {
|
|
|
|
|
if (_.isFunction(target[model.toLowerCase()][fn])) {
|
2017-11-29 15:00:50 +01:00
|
|
|
|
collection.schema.pre(key, function (next) {
|
|
|
|
|
target[model.toLowerCase()][fn](this).then(next).catch(err => strapi.log.error(err));
|
|
|
|
|
});
|
2017-11-15 15:06:09 +01:00
|
|
|
|
}
|
|
|
|
|
});
|
2016-08-04 13:21:22 +02:00
|
|
|
|
|
2017-11-15 15:06:09 +01:00
|
|
|
|
const postLifecycle = {
|
|
|
|
|
validate: 'afterCreate',
|
2017-12-19 14:47:48 +01:00
|
|
|
|
findOneAndRemove: 'afterDestroy',
|
2017-11-15 15:06:09 +01:00
|
|
|
|
remove: 'afterDestroy',
|
|
|
|
|
update: 'afterUpdate',
|
|
|
|
|
find: 'afterFetchAll',
|
|
|
|
|
findOne: 'afterFetch',
|
|
|
|
|
save: 'afterSave'
|
|
|
|
|
};
|
2016-08-04 13:21:22 +02:00
|
|
|
|
|
2018-04-23 16:20:04 +02:00
|
|
|
|
// Mongoose doesn't allow post 'remove' event on model.
|
|
|
|
|
// See https://github.com/Automattic/mongoose/issues/3054
|
2017-11-15 15:06:09 +01:00
|
|
|
|
_.forEach(postLifecycle, (fn, key) => {
|
|
|
|
|
if (_.isFunction(target[model.toLowerCase()][fn])) {
|
2017-11-29 15:46:28 +01:00
|
|
|
|
collection.schema.post(key, function (doc, next) {
|
|
|
|
|
target[model.toLowerCase()][fn](this, doc).then(next).catch(err => strapi.log.error(err))
|
|
|
|
|
});
|
2017-11-15 15:06:09 +01:00
|
|
|
|
}
|
|
|
|
|
});
|
2017-07-24 19:58:03 +02:00
|
|
|
|
|
2017-11-15 15:06:09 +01:00
|
|
|
|
// Add virtual key to provide populate and reverse populate
|
|
|
|
|
_.forEach(_.pickBy(definition.loadedModel, model => {
|
|
|
|
|
return model.type === 'virtual';
|
|
|
|
|
}), (value, key) => {
|
|
|
|
|
collection.schema.virtual(key.replace('_v', ''), {
|
|
|
|
|
ref: value.ref,
|
|
|
|
|
localField: '_id',
|
|
|
|
|
foreignField: value.via,
|
|
|
|
|
justOne: value.justOne || false
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2018-03-05 17:26:55 +01:00
|
|
|
|
collection.schema.set('timestamps', _.get(definition, 'options.timestamps') === true);
|
2018-05-07 17:44:02 +02:00
|
|
|
|
collection.schema.set('minimize', _.get(definition, 'options.minimize', false) === true);
|
2018-03-05 17:26:55 +01:00
|
|
|
|
|
2018-02-12 18:54:34 +01:00
|
|
|
|
collection.schema.options.toObject = collection.schema.options.toJSON = {
|
|
|
|
|
virtuals: true,
|
|
|
|
|
transform: function (doc, returned, opts) {
|
2018-04-24 15:24:42 +02:00
|
|
|
|
// Remover $numberDecimal nested property.
|
|
|
|
|
Object.keys(returned)
|
|
|
|
|
.filter(key => returned[key] instanceof mongoose.Types.Decimal128)
|
|
|
|
|
.forEach((key, index) => {
|
|
|
|
|
// Parse to float number.
|
|
|
|
|
returned[key] = parseFloat(returned[key].toString());
|
|
|
|
|
});
|
|
|
|
|
|
2018-02-12 18:54:34 +01:00
|
|
|
|
morphAssociations.forEach(association => {
|
|
|
|
|
if (Array.isArray(returned[association.alias]) && returned[association.alias].length > 0) {
|
2018-02-22 16:08:11 +01:00
|
|
|
|
// Reformat data by bypassing the many-to-many relationship.
|
|
|
|
|
switch (association.nature) {
|
|
|
|
|
case 'oneMorphToOne':
|
|
|
|
|
returned[association.alias] = returned[association.alias][0].ref;
|
|
|
|
|
break;
|
2018-04-05 15:20:24 +02:00
|
|
|
|
case 'manyMorphToMany':
|
2018-02-22 16:08:11 +01:00
|
|
|
|
case 'manyMorphToOne':
|
2018-04-23 12:59:53 +02:00
|
|
|
|
|
2018-02-22 16:08:11 +01:00
|
|
|
|
returned[association.alias] = returned[association.alias].map(obj => obj.ref);
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
|
|
|
|
|
}
|
2018-02-12 18:54:34 +01:00
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
};
|
2016-08-04 13:21:22 +02:00
|
|
|
|
|
2017-11-15 16:59:12 +01:00
|
|
|
|
if (!plugin) {
|
2018-02-12 18:54:34 +01:00
|
|
|
|
global[definition.globalName] = instance.model(definition.globalId, collection.schema, definition.collectionName);
|
2017-11-15 16:59:12 +01:00
|
|
|
|
} else {
|
2018-02-12 18:54:34 +01:00
|
|
|
|
instance.model(definition.globalId, collection.schema, definition.collectionName);
|
2017-11-15 16:59:12 +01:00
|
|
|
|
}
|
2017-11-15 15:06:09 +01:00
|
|
|
|
|
|
|
|
|
// Expose ORM functions through the `target` object.
|
|
|
|
|
target[model] = _.assign(instance.model(definition.globalName), target[model]);
|
|
|
|
|
|
|
|
|
|
// Push model to strapi global variables.
|
2017-11-15 16:59:12 +01:00
|
|
|
|
collection = instance.model(definition.globalName, collection.schema);
|
2017-11-15 15:06:09 +01:00
|
|
|
|
|
|
|
|
|
// Push attributes to be aware of model schema.
|
|
|
|
|
target[model]._attributes = definition.attributes;
|
|
|
|
|
} catch (err) {
|
|
|
|
|
strapi.log.error('Impossible to register the `' + model + '` model.');
|
|
|
|
|
strapi.log.error(err);
|
|
|
|
|
strapi.stop();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2018-03-14 16:56:12 +01:00
|
|
|
|
// Parse every authenticated model.
|
2017-11-15 15:06:09 +01:00
|
|
|
|
_.forEach(models, (definition, model) => {
|
|
|
|
|
definition.globalName = _.upperFirst(_.camelCase(definition.globalId));
|
2017-12-11 18:23:15 +01:00
|
|
|
|
|
2017-11-15 15:06:09 +01:00
|
|
|
|
// Make sure the model has a connection.
|
|
|
|
|
// If not, use the default connection.
|
|
|
|
|
if (_.isEmpty(definition.connection)) {
|
2017-11-28 14:28:30 +01:00
|
|
|
|
definition.connection = strapi.config.currentEnvironment.database.defaultConnection;
|
2017-11-15 15:06:09 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Make sure this connection exists.
|
|
|
|
|
if (!_.has(strapi.config.connections, definition.connection)) {
|
|
|
|
|
strapi.log.error('The connection `' + definition.connection + '` specified in the `' + model + '` model does not exist.');
|
2017-01-04 17:21:24 +01:00
|
|
|
|
strapi.stop();
|
|
|
|
|
}
|
2016-08-10 11:00:56 +02:00
|
|
|
|
|
2017-11-15 15:06:09 +01:00
|
|
|
|
// Add some informations about ORM & client connection
|
|
|
|
|
definition.orm = 'mongoose';
|
|
|
|
|
definition.client = _.get(strapi.config.connections[definition.connection], 'client');
|
2018-02-26 11:12:49 +01:00
|
|
|
|
definition.associations = [];
|
2016-07-12 11:15:01 +02:00
|
|
|
|
|
2017-11-15 16:59:12 +01:00
|
|
|
|
// Register the final model for Mongoose.
|
2017-11-15 15:06:09 +01:00
|
|
|
|
definition.loadedModel = _.cloneDeep(definition.attributes);
|
2016-08-04 13:21:22 +02:00
|
|
|
|
|
2017-11-15 15:06:09 +01:00
|
|
|
|
// Initialize the global variable with the
|
|
|
|
|
// capitalized model name.
|
2017-11-15 16:59:12 +01:00
|
|
|
|
if (!plugin) {
|
|
|
|
|
global[definition.globalName] = {};
|
|
|
|
|
}
|
2017-01-04 17:21:24 +01:00
|
|
|
|
|
2017-11-15 15:06:09 +01:00
|
|
|
|
if (_.isEmpty(definition.attributes)) {
|
|
|
|
|
// Generate empty schema
|
|
|
|
|
_.set(strapi.config.hook.settings.mongoose, 'collections.' + mongooseUtils.toCollectionName(definition.globalName) + '.schema', new instance.Schema({}));
|
2017-09-19 13:40:26 +02:00
|
|
|
|
|
2017-11-15 15:06:09 +01:00
|
|
|
|
return loadedAttributes();
|
2017-01-04 17:21:24 +01:00
|
|
|
|
}
|
|
|
|
|
|
2017-11-15 15:06:09 +01:00
|
|
|
|
// Call this callback function after we are done parsing
|
|
|
|
|
// all attributes for relationships-- see below.
|
|
|
|
|
const done = _.after(_.size(definition.attributes), () => {
|
2018-02-12 18:54:34 +01:00
|
|
|
|
// Generate schema without virtual populate
|
|
|
|
|
const schema = new instance.Schema(_.omitBy(definition.loadedModel, model => {
|
|
|
|
|
return model.type === 'virtual';
|
|
|
|
|
}));
|
2018-02-09 10:43:09 +01:00
|
|
|
|
|
|
|
|
|
_.set(strapi.config.hook.settings.mongoose, 'collections.' + mongooseUtils.toCollectionName(definition.globalName) + '.schema', schema);
|
2017-01-04 17:21:24 +01:00
|
|
|
|
|
2017-11-15 15:06:09 +01:00
|
|
|
|
loadedAttributes();
|
|
|
|
|
});
|
2017-01-04 17:21:24 +01:00
|
|
|
|
|
2017-11-15 15:06:09 +01:00
|
|
|
|
// Add every relationships to the loaded model for Bookshelf.
|
|
|
|
|
// Basic attributes don't need this-- only relations.
|
|
|
|
|
_.forEach(definition.attributes, (details, name) => {
|
|
|
|
|
const verbose = _.get(utilsModels.getNature(details, name, undefined, model.toLowerCase()), 'verbose') || '';
|
2017-09-14 18:47:10 +02:00
|
|
|
|
|
2017-11-15 15:06:09 +01:00
|
|
|
|
// Build associations key
|
2018-01-23 18:54:17 +01:00
|
|
|
|
utilsModels.defineAssociations(model.toLowerCase(), definition, details, name);
|
2017-09-15 18:29:50 +02:00
|
|
|
|
|
2017-11-15 15:06:09 +01:00
|
|
|
|
if (_.isEmpty(verbose)) {
|
|
|
|
|
definition.loadedModel[name].type = utils(instance).convertType(details.type);
|
|
|
|
|
}
|
2017-09-07 17:16:31 +02:00
|
|
|
|
|
2017-11-15 15:06:09 +01:00
|
|
|
|
switch (verbose) {
|
2017-12-11 18:23:15 +01:00
|
|
|
|
case 'hasOne': {
|
2018-01-04 11:24:27 +01:00
|
|
|
|
const ref = details.plugin ? strapi.plugins[details.plugin].models[details.model].globalId : strapi.models[details.model].globalId;
|
2017-12-11 18:23:15 +01:00
|
|
|
|
|
2017-01-04 17:21:24 +01:00
|
|
|
|
definition.loadedModel[name] = {
|
2017-11-15 15:06:09 +01:00
|
|
|
|
type: instance.Schema.Types.ObjectId,
|
2017-12-11 18:23:15 +01:00
|
|
|
|
ref
|
2017-01-04 17:21:24 +01:00
|
|
|
|
};
|
2017-11-15 15:06:09 +01:00
|
|
|
|
break;
|
2017-12-11 18:23:15 +01:00
|
|
|
|
}
|
|
|
|
|
case 'hasMany': {
|
|
|
|
|
const FK = _.find(definition.associations, {alias: name});
|
2018-01-04 11:24:27 +01:00
|
|
|
|
const ref = details.plugin ? strapi.plugins[details.plugin].models[details.collection].globalId : strapi.models[details.collection].globalId;
|
2017-11-15 15:06:09 +01:00
|
|
|
|
|
|
|
|
|
if (FK) {
|
|
|
|
|
definition.loadedModel[name] = {
|
|
|
|
|
type: 'virtual',
|
2017-12-11 18:23:15 +01:00
|
|
|
|
ref,
|
2017-11-15 15:06:09 +01:00
|
|
|
|
via: FK.via,
|
|
|
|
|
justOne: false
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Set this info to be able to see if this field is a real database's field.
|
|
|
|
|
details.isVirtual = true;
|
|
|
|
|
} else {
|
|
|
|
|
definition.loadedModel[name] = [{
|
|
|
|
|
type: instance.Schema.Types.ObjectId,
|
2017-12-11 18:23:15 +01:00
|
|
|
|
ref
|
2017-11-15 15:06:09 +01:00
|
|
|
|
}];
|
|
|
|
|
}
|
|
|
|
|
break;
|
2017-12-11 18:23:15 +01:00
|
|
|
|
}
|
|
|
|
|
case 'belongsTo': {
|
|
|
|
|
const FK = _.find(definition.associations, {alias: name});
|
2018-01-04 11:24:27 +01:00
|
|
|
|
const ref = details.plugin ? strapi.plugins[details.plugin].models[details.model].globalId : strapi.models[details.model].globalId;
|
2017-11-15 15:06:09 +01:00
|
|
|
|
|
2018-02-12 18:54:34 +01:00
|
|
|
|
if (FK && FK.nature !== 'oneToOne' && FK.nature !== 'manyToOne' && FK.nature !== 'oneWay' && FK.nature !== 'oneToMorph') {
|
2017-11-15 15:06:09 +01:00
|
|
|
|
definition.loadedModel[name] = {
|
|
|
|
|
type: 'virtual',
|
2017-12-11 18:23:15 +01:00
|
|
|
|
ref,
|
2017-11-15 15:06:09 +01:00
|
|
|
|
via: FK.via,
|
|
|
|
|
justOne: true
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Set this info to be able to see if this field is a real database's field.
|
|
|
|
|
details.isVirtual = true;
|
|
|
|
|
} else {
|
|
|
|
|
definition.loadedModel[name] = {
|
|
|
|
|
type: instance.Schema.Types.ObjectId,
|
2017-12-11 18:23:15 +01:00
|
|
|
|
ref
|
2017-11-15 15:06:09 +01:00
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
break;
|
2017-12-11 18:23:15 +01:00
|
|
|
|
}
|
|
|
|
|
case 'belongsToMany': {
|
|
|
|
|
const FK = _.find(definition.associations, {alias: name});
|
2018-01-04 11:24:27 +01:00
|
|
|
|
const ref = details.plugin ? strapi.plugins[details.plugin].models[details.collection].globalId : strapi.models[details.collection].globalId;
|
2017-11-15 15:06:09 +01:00
|
|
|
|
|
|
|
|
|
// One-side of the relationship has to be a virtual field to be bidirectional.
|
2018-02-20 19:59:05 +01:00
|
|
|
|
if ((FK && _.isUndefined(FK.via)) || details.dominant !== true) {
|
2017-11-15 15:06:09 +01:00
|
|
|
|
definition.loadedModel[name] = {
|
|
|
|
|
type: 'virtual',
|
2017-12-11 18:23:15 +01:00
|
|
|
|
ref,
|
2017-11-15 15:06:09 +01:00
|
|
|
|
via: FK.via
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Set this info to be able to see if this field is a real database's field.
|
|
|
|
|
details.isVirtual = true;
|
|
|
|
|
} else {
|
|
|
|
|
definition.loadedModel[name] = [{
|
|
|
|
|
type: instance.Schema.Types.ObjectId,
|
2017-12-11 18:23:15 +01:00
|
|
|
|
ref
|
2017-11-15 15:06:09 +01:00
|
|
|
|
}];
|
|
|
|
|
}
|
|
|
|
|
break;
|
2017-12-11 18:23:15 +01:00
|
|
|
|
}
|
2018-02-20 19:59:05 +01:00
|
|
|
|
case 'morphOne': {
|
|
|
|
|
const FK = _.find(definition.associations, {alias: name});
|
|
|
|
|
const ref = details.plugin ? strapi.plugins[details.plugin].models[details.model].globalId : strapi.models[details.model].globalId;
|
|
|
|
|
|
|
|
|
|
definition.loadedModel[name] = {
|
|
|
|
|
type: 'virtual',
|
|
|
|
|
ref,
|
2018-02-22 15:34:33 +01:00
|
|
|
|
via: `${FK.via}.ref`,
|
2018-02-20 19:59:05 +01:00
|
|
|
|
justOne: true
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Set this info to be able to see if this field is a real database's field.
|
|
|
|
|
details.isVirtual = true;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case 'morphMany': {
|
|
|
|
|
const FK = _.find(definition.associations, {alias: name});
|
|
|
|
|
const ref = details.plugin ? strapi.plugins[details.plugin].models[details.collection].globalId : strapi.models[details.collection].globalId;
|
|
|
|
|
|
|
|
|
|
definition.loadedModel[name] = {
|
|
|
|
|
type: 'virtual',
|
|
|
|
|
ref,
|
2018-02-22 15:34:33 +01:00
|
|
|
|
via: `${FK.via}.ref`
|
2018-02-20 19:59:05 +01:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Set this info to be able to see if this field is a real database's field.
|
|
|
|
|
details.isVirtual = true;
|
|
|
|
|
break;
|
|
|
|
|
}
|
2018-02-12 18:54:34 +01:00
|
|
|
|
case 'belongsToMorph': {
|
2018-02-09 10:43:09 +01:00
|
|
|
|
definition.loadedModel[name] = {
|
2018-02-12 18:54:34 +01:00
|
|
|
|
kind: String,
|
2018-02-22 15:34:33 +01:00
|
|
|
|
[details.filter]: String,
|
|
|
|
|
ref: {
|
2018-02-12 18:54:34 +01:00
|
|
|
|
type: instance.Schema.Types.ObjectId,
|
|
|
|
|
refPath: `${name}.kind`
|
|
|
|
|
}
|
|
|
|
|
};
|
2018-02-09 10:43:09 +01:00
|
|
|
|
break;
|
|
|
|
|
}
|
2018-02-12 18:54:34 +01:00
|
|
|
|
case 'belongsToManyMorph': {
|
|
|
|
|
definition.loadedModel[name] = [{
|
|
|
|
|
kind: String,
|
2018-02-22 15:34:33 +01:00
|
|
|
|
[details.filter]: String,
|
|
|
|
|
ref: {
|
2018-02-12 18:54:34 +01:00
|
|
|
|
type: instance.Schema.Types.ObjectId,
|
|
|
|
|
refPath: `${name}.kind`
|
|
|
|
|
}
|
|
|
|
|
}];
|
|
|
|
|
break;
|
2018-02-09 10:43:09 +01:00
|
|
|
|
}
|
2017-11-15 15:06:09 +01:00
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
done();
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
};
|
2017-09-14 18:47:10 +02:00
|
|
|
|
|
2017-12-11 12:29:05 +01:00
|
|
|
|
// Mount `./api` models.
|
|
|
|
|
mountModels(_.pickBy(strapi.models, { connection: connectionName }), strapi.models);
|
2017-01-04 17:21:24 +01:00
|
|
|
|
|
2017-12-11 12:29:05 +01:00
|
|
|
|
// Mount `./plugins` models.
|
2017-11-15 15:06:09 +01:00
|
|
|
|
_.forEach(strapi.plugins, (plugin, name) => {
|
2017-12-11 12:29:05 +01:00
|
|
|
|
mountModels(_.pickBy(strapi.plugins[name].models, { connection: connectionName }), plugin.models, name);
|
2016-07-12 11:15:01 +02:00
|
|
|
|
});
|
2017-11-15 15:06:09 +01:00
|
|
|
|
|
|
|
|
|
cb();
|
2016-07-12 11:15:01 +02:00
|
|
|
|
});
|
|
|
|
|
});
|
2017-09-12 17:58:31 +02:00
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
getQueryParams: (value, type, key) => {
|
|
|
|
|
const result = {};
|
|
|
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
|
case '=':
|
|
|
|
|
result.key = `where.${key}`;
|
|
|
|
|
result.value = value;
|
|
|
|
|
break;
|
|
|
|
|
case '_ne':
|
|
|
|
|
result.key = `where.${key}.$ne`;
|
|
|
|
|
result.value = value;
|
|
|
|
|
break;
|
|
|
|
|
case '_lt':
|
|
|
|
|
result.key = `where.${key}.$lt`;
|
|
|
|
|
result.value = value;
|
|
|
|
|
break;
|
|
|
|
|
case '_gt':
|
|
|
|
|
result.key = `where.${key}.$gt`;
|
|
|
|
|
result.value = value;
|
|
|
|
|
break;
|
|
|
|
|
case '_lte':
|
|
|
|
|
result.key = `where.${key}.$lte`;
|
|
|
|
|
result.value = value;
|
|
|
|
|
break;
|
|
|
|
|
case '_gte':
|
|
|
|
|
result.key = `where.${key}.$gte`;
|
|
|
|
|
result.value = value;
|
|
|
|
|
break;
|
|
|
|
|
case '_sort':
|
2017-09-13 12:18:54 +02:00
|
|
|
|
result.key = `sort`;
|
2017-12-20 16:06:02 +01:00
|
|
|
|
result.value = (_.toLower(value) === 'desc') ? '-' : '';
|
2017-09-13 12:18:54 +02:00
|
|
|
|
result.value += key;
|
2017-09-12 17:58:31 +02:00
|
|
|
|
break;
|
|
|
|
|
case '_start':
|
|
|
|
|
result.key = `start`;
|
|
|
|
|
result.value = parseFloat(value);
|
|
|
|
|
break;
|
|
|
|
|
case '_limit':
|
|
|
|
|
result.key = `limit`;
|
|
|
|
|
result.value = parseFloat(value);
|
|
|
|
|
break;
|
2018-04-03 11:30:39 +02:00
|
|
|
|
case '_contains':
|
2018-04-10 18:54:01 +02:00
|
|
|
|
result.key = `where.${key}`;
|
2018-04-19 15:45:35 +02:00
|
|
|
|
result.value = {
|
|
|
|
|
$regex: value,
|
|
|
|
|
$options: 'i',
|
|
|
|
|
};
|
2018-04-10 18:54:01 +02:00
|
|
|
|
break;
|
|
|
|
|
case '_containss':
|
2018-04-03 11:30:39 +02:00
|
|
|
|
result.key = `where.${key}.$regex`;
|
|
|
|
|
result.value = value;
|
|
|
|
|
break;
|
2017-09-12 17:58:31 +02:00
|
|
|
|
default:
|
|
|
|
|
result = undefined;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result;
|
2017-10-25 16:24:35 +02:00
|
|
|
|
},
|
|
|
|
|
|
2018-03-07 17:48:40 +01:00
|
|
|
|
manageRelations: async function (model, params, source) {
|
2017-11-17 16:05:02 +01:00
|
|
|
|
const models = _.assign(_.clone(strapi.models), Object.keys(strapi.plugins).reduce((acc, current) => {
|
|
|
|
|
_.assign(acc, strapi.plugins[current].models);
|
|
|
|
|
return acc;
|
|
|
|
|
}, {}));
|
|
|
|
|
|
|
|
|
|
const Model = models[model];
|
2017-11-01 15:16:28 +01:00
|
|
|
|
|
2017-10-25 16:24:35 +02:00
|
|
|
|
const virtualFields = [];
|
|
|
|
|
const response = await Model
|
|
|
|
|
.findOne({
|
|
|
|
|
[Model.primaryKey]: params._id || params.id
|
|
|
|
|
})
|
|
|
|
|
.populate(_.keys(_.groupBy(_.reject(Model.associations, {autoPopulate: false}), 'alias')).join(' '));
|
|
|
|
|
|
|
|
|
|
// 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 = Model.associations.filter(x => x.alias === current)[0];
|
|
|
|
|
const details = Model._attributes[current];
|
|
|
|
|
|
|
|
|
|
if (_.get(Model._attributes, `${current}.isVirtual`) !== true && _.isUndefined(association)) {
|
|
|
|
|
acc[current] = params.values[current];
|
|
|
|
|
} else {
|
|
|
|
|
switch (association.nature) {
|
2018-04-23 16:57:59 +02:00
|
|
|
|
case 'oneWay':
|
|
|
|
|
acc[current] = _.get(params.values[current], this.primaryKey, params.values[current]) || null;
|
|
|
|
|
|
|
|
|
|
break;
|
2017-10-25 16:24:35 +02:00
|
|
|
|
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[Model.primaryKey] || value.id || value._id : value[current];
|
|
|
|
|
|
|
|
|
|
if (response[current] && _.isObject(response[current]) && response[current][Model.primaryKey] !== value[current]) {
|
|
|
|
|
virtualFields.push(
|
2017-11-01 15:16:28 +01:00
|
|
|
|
this.manageRelations(details.model || details.collection, {
|
2017-10-30 17:59:53 +01:00
|
|
|
|
_id: response[current][Model.primaryKey],
|
2017-10-25 16:24:35 +02:00
|
|
|
|
values: {
|
|
|
|
|
[details.via]: null
|
|
|
|
|
},
|
|
|
|
|
parseRelationships: false
|
|
|
|
|
})
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Remove previous relationship asynchronously if it exists.
|
|
|
|
|
virtualFields.push(
|
2017-10-25 17:38:48 +02:00
|
|
|
|
models[details.model || details.collection]
|
|
|
|
|
.findOne({ id : recordId })
|
|
|
|
|
.populate(_.keys(_.groupBy(_.reject(models[details.model || details.collection].associations, {autoPopulate: false}), 'alias')).join(' '))
|
2017-10-25 16:24:35 +02:00
|
|
|
|
.then(record => {
|
2017-12-13 14:36:07 +01:00
|
|
|
|
if (record && _.isObject(record[details.via]) && record[details.via][current] !== value[current]) {
|
2017-11-01 15:16:28 +01:00
|
|
|
|
return this.manageRelations(details.model || details.collection, {
|
2017-10-25 16:24:35 +02:00
|
|
|
|
id: record[details.via][Model.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.
|
2017-11-01 15:16:28 +01:00
|
|
|
|
virtualFields.push(this.manageRelations(details.model || details.collection, {
|
2017-10-25 16:24:35 +02:00
|
|
|
|
id: recordId,
|
|
|
|
|
values: {
|
2017-10-25 17:38:48 +02:00
|
|
|
|
[details.via]: _.isNull(params.values[current]) ? null : value[Model.primaryKey] || params.id || params._id || value.id || value._id
|
2017-10-25 16:24:35 +02:00
|
|
|
|
},
|
|
|
|
|
parseRelationships: false
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
acc[current] = _.isNull(params.values[current]) ? null : value[current];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
case 'oneToMany':
|
|
|
|
|
case 'manyToOne':
|
|
|
|
|
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) =>
|
2017-10-25 16:39:28 +02:00
|
|
|
|
((typeof a === 'string') ? a : a[Model.primaryKey].toString()) === b[Model.primaryKey].toString()
|
2017-10-25 16:24:35 +02:00
|
|
|
|
);
|
|
|
|
|
// Records to remove in the relation.
|
|
|
|
|
const toRemove = _.differenceWith(response[current], params.values[current], (a, b) =>
|
2017-10-25 16:39:28 +02:00
|
|
|
|
a[Model.primaryKey].toString() === ((typeof b === 'string') ? b : b[Model.primaryKey].toString())
|
2017-10-25 16:24:35 +02:00
|
|
|
|
)
|
|
|
|
|
.filter(x => toAdd.find(y => x.id === y.id) === undefined);
|
|
|
|
|
|
|
|
|
|
// Push the work into the flow process.
|
|
|
|
|
toAdd.forEach(value => {
|
2017-10-25 16:39:28 +02:00
|
|
|
|
value = (typeof value === 'string') ? { _id: value } : value;
|
2017-10-25 16:24:35 +02:00
|
|
|
|
|
|
|
|
|
if (association.nature === 'manyToMany' && !_.isArray(params.values[Model.primaryKey])) {
|
|
|
|
|
value[details.via] = (value[details.via] || []).concat([response[Model.primaryKey]]);
|
|
|
|
|
} else {
|
|
|
|
|
value[details.via] = params[Model.primaryKey];
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-01 15:16:28 +01:00
|
|
|
|
virtualFields.push(this.manageRelations(details.model || details.collection, {
|
2017-10-25 16:24:35 +02:00
|
|
|
|
id: value[Model.primaryKey] || value.id || value._id,
|
|
|
|
|
values: value,
|
|
|
|
|
foreignKey: current
|
|
|
|
|
}));
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
toRemove.forEach(value => {
|
2017-10-25 16:39:28 +02:00
|
|
|
|
value = (typeof value === 'string') ? { _id: value } : value;
|
|
|
|
|
|
2017-10-25 16:24:35 +02:00
|
|
|
|
if (association.nature === 'manyToMany' && !_.isArray(params.values[Model.primaryKey])) {
|
|
|
|
|
value[details.via] = value[details.via].filter(x => x.toString() !== response[Model.primaryKey].toString());
|
|
|
|
|
} else {
|
|
|
|
|
value[details.via] = null;
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-01 15:16:28 +01:00
|
|
|
|
virtualFields.push(this.manageRelations(details.model || details.collection, {
|
2017-10-25 16:24:35 +02:00
|
|
|
|
id: value[Model.primaryKey] || value.id || value._id,
|
|
|
|
|
values: value,
|
|
|
|
|
foreignKey: current
|
|
|
|
|
}));
|
|
|
|
|
});
|
|
|
|
|
} else if (_.get(Model._attributes, `${current}.isVirtual`) !== true) {
|
|
|
|
|
acc[current] = params.values[current];
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-07 17:48:40 +01:00
|
|
|
|
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[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
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
break;
|
|
|
|
|
case 'oneToManyMorph':
|
|
|
|
|
case 'manyToManyMorph':
|
|
|
|
|
const transformToArrayID = (array) => {
|
|
|
|
|
if (_.isArray(array)) {
|
|
|
|
|
return array.map(value => {
|
|
|
|
|
if (_.isObject(value)) {
|
|
|
|
|
return value._id || value.id;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return value;
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (association.type === 'model') {
|
|
|
|
|
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(this.addRelationMorph(details.model || details.collection, {
|
|
|
|
|
id,
|
|
|
|
|
alias: association.via,
|
|
|
|
|
ref: Model.globalId,
|
|
|
|
|
refId: response._id,
|
|
|
|
|
field: association.alias
|
|
|
|
|
}, details.plugin));
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Remove relations in the other side.
|
|
|
|
|
toRemove.forEach(id => {
|
|
|
|
|
virtualFields.push(this.removeRelationMorph(details.model || details.collection, {
|
|
|
|
|
id,
|
|
|
|
|
alias: association.via,
|
|
|
|
|
ref: Model.globalId,
|
|
|
|
|
refId: response._id,
|
|
|
|
|
field: association.alias
|
|
|
|
|
}, details.plugin));
|
|
|
|
|
});
|
|
|
|
|
break;
|
|
|
|
|
case 'oneMorphToOne':
|
|
|
|
|
case 'oneMorphToMany':
|
2017-10-25 16:24:35 +02:00
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return acc;
|
|
|
|
|
}, {});
|
|
|
|
|
|
|
|
|
|
virtualFields.push(Model
|
|
|
|
|
.update({
|
|
|
|
|
[Model.primaryKey]: params[Model.primaryKey] || params.id
|
|
|
|
|
}, values, {
|
|
|
|
|
strict: false
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
// Update virtuals fields.
|
|
|
|
|
const process = await Promise.all(virtualFields);
|
|
|
|
|
|
|
|
|
|
return process[process.length - 1];
|
2018-03-07 17:48:40 +01:00
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
addRelationMorph: async function (model, params, source) {
|
|
|
|
|
const models = _.assign(_.clone(strapi.models), Object.keys(strapi.plugins).reduce((acc, current) => {
|
|
|
|
|
_.assign(acc, strapi.plugins[current].models);
|
|
|
|
|
return acc;
|
|
|
|
|
}, {}));
|
|
|
|
|
|
|
|
|
|
const Model = models[model];
|
|
|
|
|
/*
|
|
|
|
|
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 Model.findOne({
|
|
|
|
|
[Model.primaryKey]: params.id
|
|
|
|
|
});
|
|
|
|
|
const value = entry[params.alias] || [];
|
|
|
|
|
|
|
|
|
|
// Retrieve association.
|
|
|
|
|
const association = Model.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 Model.update({
|
|
|
|
|
[Model.primaryKey]: params.id
|
|
|
|
|
}, entry, {
|
|
|
|
|
strict: false
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
removeRelationMorph: async function (model, params, source) {
|
|
|
|
|
const models = _.assign(_.clone(strapi.models), Object.keys(strapi.plugins).reduce((acc, current) => {
|
|
|
|
|
_.assign(acc, strapi.plugins[current].models);
|
|
|
|
|
return acc;
|
|
|
|
|
}, {}));
|
|
|
|
|
|
|
|
|
|
const Model = models[model];
|
|
|
|
|
|
|
|
|
|
const entry = await Model.findOne({
|
|
|
|
|
[Model.primaryKey]: params.id
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// 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 Model.update({
|
|
|
|
|
[Model.primaryKey]: params.id
|
|
|
|
|
}, entry, {
|
|
|
|
|
strict: false
|
|
|
|
|
});
|
2016-07-12 11:15:01 +02:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return hook;
|
|
|
|
|
};
|