2016-07-12 11:15:01 +02:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Module dependencies
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Public node modules.
|
2018-07-01 18:45:21 +02:00
|
|
|
const url = require('url');
|
2018-11-23 14:36:31 -03:00
|
|
|
const path = require('path');
|
2016-07-12 11:15:01 +02:00
|
|
|
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
|
|
|
|
2018-05-04 18:09:55 +02:00
|
|
|
// Strapi helpers for models.
|
2019-04-09 12:09:03 +02:00
|
|
|
const { models: utilsModels } = require('strapi-utils');
|
2018-05-04 18:09:55 +02:00
|
|
|
|
2016-07-14 11:06:58 +02:00
|
|
|
// Local helpers.
|
|
|
|
const utils = require('./utils/');
|
2018-10-08 22:19:31 +02:00
|
|
|
|
2018-05-08 18:44:47 +02:00
|
|
|
const relations = require('./relations');
|
2019-03-13 19:27:18 +01:00
|
|
|
const buildQuery = require('./buildQuery');
|
2016-07-14 11:06:58 +02:00
|
|
|
|
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
|
|
|
*/
|
|
|
|
|
2018-05-04 18:09:55 +02:00
|
|
|
/* eslint-disable prefer-template */
|
|
|
|
/* eslint-disable no-case-declarations */
|
|
|
|
/* eslint-disable no-const-assign */
|
|
|
|
/* eslint-disable no-unused-vars */
|
2019-04-09 12:09:03 +02:00
|
|
|
/* eslint-disable no-unexpected-multiline */
|
2019-04-09 16:01:01 +02:00
|
|
|
/* eslint-disable indent */
|
2019-04-09 12:09:03 +02:00
|
|
|
module.exports = function(strapi) {
|
|
|
|
const hook = _.merge(
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Default options
|
|
|
|
*/
|
|
|
|
|
|
|
|
defaults: {
|
|
|
|
defaultConnection: 'default',
|
|
|
|
host: 'localhost',
|
|
|
|
port: 27017,
|
|
|
|
database: 'strapi',
|
|
|
|
authenticationDatabase: '',
|
|
|
|
ssl: false,
|
|
|
|
debug: false,
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialize the hook
|
|
|
|
*/
|
|
|
|
|
|
|
|
initialize: cb =>
|
|
|
|
_.forEach(
|
|
|
|
_.pickBy(strapi.config.connections, {
|
|
|
|
connector: 'strapi-hook-mongoose',
|
|
|
|
}),
|
|
|
|
async (connection, connectionName) => {
|
|
|
|
const instance = new Mongoose();
|
2019-04-09 16:01:01 +02:00
|
|
|
const {
|
|
|
|
uri,
|
|
|
|
host,
|
|
|
|
port,
|
|
|
|
username,
|
|
|
|
password,
|
|
|
|
database,
|
|
|
|
srv,
|
|
|
|
} = _.defaults(
|
2019-04-09 12:09:03 +02:00
|
|
|
connection.settings,
|
|
|
|
strapi.config.hook.settings.mongoose,
|
|
|
|
);
|
|
|
|
const uriOptions = uri ? url.parse(uri, true).query : {};
|
|
|
|
const { authenticationDatabase, ssl, debug } = _.defaults(
|
|
|
|
connection.options,
|
|
|
|
uriOptions,
|
|
|
|
strapi.config.hook.settings.mongoose,
|
|
|
|
);
|
|
|
|
const isSrv = srv === true || srv === 'true';
|
|
|
|
|
|
|
|
// Connect to mongo database
|
|
|
|
const connectOptions = {};
|
|
|
|
const options = {
|
|
|
|
useFindAndModify: false,
|
|
|
|
};
|
|
|
|
|
|
|
|
if (!_.isEmpty(username)) {
|
|
|
|
connectOptions.user = username;
|
|
|
|
|
|
|
|
if (!_.isEmpty(password)) {
|
|
|
|
connectOptions.pass = password;
|
|
|
|
}
|
|
|
|
}
|
2018-02-07 00:19:05 +01:00
|
|
|
|
2019-04-09 12:09:03 +02:00
|
|
|
if (!_.isEmpty(authenticationDatabase)) {
|
|
|
|
connectOptions.authSource = authenticationDatabase;
|
|
|
|
}
|
2018-08-26 03:26:57 +09:00
|
|
|
|
2019-04-09 12:09:03 +02:00
|
|
|
connectOptions.ssl = ssl === true || ssl === 'true';
|
|
|
|
connectOptions.useNewUrlParser = true;
|
|
|
|
connectOptions.dbName = database;
|
|
|
|
connectOptions.useCreateIndex = true;
|
2018-08-26 03:26:57 +09:00
|
|
|
|
2019-04-09 12:09:03 +02:00
|
|
|
options.debug = debug === true || debug === 'true';
|
2018-08-26 03:26:57 +09:00
|
|
|
|
2019-04-09 12:09:03 +02:00
|
|
|
try {
|
|
|
|
/* FIXME: for now, mongoose doesn't support srv auth except the way including user/pass in URI.
|
|
|
|
* https://github.com/Automattic/mongoose/issues/6881 */
|
|
|
|
await instance.connect(
|
2019-03-22 12:16:09 +01:00
|
|
|
uri ||
|
2019-04-09 16:01:01 +02:00
|
|
|
`mongodb${
|
|
|
|
isSrv ? '+srv' : ''
|
|
|
|
}://${username}:${password}@${host}${
|
2019-03-22 12:16:09 +01:00
|
|
|
!isSrv ? ':' + port : ''
|
|
|
|
}/`,
|
2019-04-09 12:09:03 +02:00
|
|
|
connectOptions,
|
|
|
|
);
|
|
|
|
} catch ({ message }) {
|
2019-03-22 12:16:09 +01:00
|
|
|
const errMsg = message.includes(`:${port}`)
|
|
|
|
? 'Make sure your MongoDB database is running...'
|
|
|
|
: message;
|
2018-08-26 03:26:57 +09:00
|
|
|
|
2019-04-09 12:09:03 +02:00
|
|
|
return cb(errMsg);
|
|
|
|
}
|
2018-10-08 16:45:24 +02:00
|
|
|
|
2019-04-09 12:09:03 +02:00
|
|
|
try {
|
|
|
|
// Require `config/functions/mongoose.js` file to customize connection.
|
2019-04-09 16:01:01 +02:00
|
|
|
require(path.resolve(
|
|
|
|
strapi.config.appPath,
|
|
|
|
'config',
|
|
|
|
'functions',
|
|
|
|
'mongoose.js',
|
|
|
|
))(instance, connection);
|
2019-04-09 12:09:03 +02:00
|
|
|
} catch (err) {
|
|
|
|
// This is not an error if the file is not found.
|
|
|
|
}
|
2017-07-24 19:58:03 +02:00
|
|
|
|
2019-04-09 12:09:03 +02:00
|
|
|
Object.keys(options, key => instance.set(key, options[key]));
|
2018-11-23 14:36:31 -03:00
|
|
|
|
2019-04-09 12:09:03 +02:00
|
|
|
const mountModels = (models, target, plugin = false) => {
|
|
|
|
if (!target) return;
|
2016-08-04 13:21:22 +02:00
|
|
|
|
2019-04-09 12:09:03 +02:00
|
|
|
const loadedAttributes = _.after(_.size(models), () => {
|
|
|
|
_.forEach(models, (definition, model) => {
|
|
|
|
try {
|
|
|
|
let collection =
|
|
|
|
strapi.config.hook.settings.mongoose.collections[
|
|
|
|
mongooseUtils.toCollectionName(definition.globalName)
|
|
|
|
];
|
2018-08-29 23:27:49 +09:00
|
|
|
|
2019-04-09 12:09:03 +02:00
|
|
|
// Set the default values to model settings.
|
|
|
|
_.defaults(definition, {
|
|
|
|
primaryKey: '_id',
|
|
|
|
});
|
2018-10-08 16:45:24 +02:00
|
|
|
|
2019-04-09 12:09:03 +02:00
|
|
|
// Initialize lifecycle callbacks.
|
|
|
|
const preLifecycle = {
|
|
|
|
validate: 'beforeCreate',
|
|
|
|
findOneAndUpdate: 'beforeUpdate',
|
|
|
|
findOneAndRemove: 'beforeDestroy',
|
|
|
|
remove: 'beforeDestroy',
|
|
|
|
update: 'beforeUpdate',
|
|
|
|
updateOne: 'beforeUpdate',
|
|
|
|
find: 'beforeFetchAll',
|
|
|
|
findOne: 'beforeFetch',
|
|
|
|
save: 'beforeSave',
|
|
|
|
};
|
2018-10-08 16:45:24 +02:00
|
|
|
|
2019-04-09 12:09:03 +02:00
|
|
|
/*
|
2018-10-08 16:45:24 +02:00
|
|
|
Override populate path for polymorphic association.
|
|
|
|
It allows us to make Upload.find().populate('related')
|
|
|
|
instead of Upload.find().populate('related.item')
|
|
|
|
*/
|
|
|
|
|
2019-04-09 12:09:03 +02:00
|
|
|
const morphAssociations = definition.associations.filter(
|
2019-04-09 16:01:01 +02:00
|
|
|
association =>
|
|
|
|
association.nature.toLowerCase().indexOf('morph') !==
|
|
|
|
-1,
|
2019-04-09 12:09:03 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
if (morphAssociations.length > 0) {
|
|
|
|
morphAssociations.forEach(association => {
|
|
|
|
Object.keys(preLifecycle)
|
|
|
|
.filter(key => key.indexOf('find') !== -1)
|
|
|
|
.forEach(key => {
|
|
|
|
collection.schema.pre(key, function(next) {
|
2019-03-22 12:16:09 +01:00
|
|
|
if (
|
|
|
|
this._mongooseOptions.populate &&
|
2019-04-09 16:01:01 +02:00
|
|
|
this._mongooseOptions.populate[
|
|
|
|
association.alias
|
|
|
|
]
|
2019-03-22 12:16:09 +01:00
|
|
|
) {
|
2019-04-09 12:09:03 +02:00
|
|
|
if (
|
|
|
|
association.nature === 'oneToManyMorph' ||
|
|
|
|
association.nature === 'manyToManyMorph'
|
|
|
|
) {
|
2019-04-09 16:01:01 +02:00
|
|
|
this._mongooseOptions.populate[
|
|
|
|
association.alias
|
|
|
|
].match = {
|
|
|
|
[`${association.via}.${
|
|
|
|
association.filter
|
|
|
|
}`]: association.alias,
|
|
|
|
[`${
|
|
|
|
association.via
|
|
|
|
}.kind`]: definition.globalId,
|
2019-04-09 12:09:03 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
// Select last related to an entity.
|
2019-04-09 16:01:01 +02:00
|
|
|
this._mongooseOptions.populate[
|
|
|
|
association.alias
|
|
|
|
].options = {
|
2019-04-09 12:09:03 +02:00
|
|
|
sort: '-createdAt',
|
|
|
|
};
|
|
|
|
} else {
|
2019-04-09 16:01:01 +02:00
|
|
|
this._mongooseOptions.populate[
|
2019-03-22 12:16:09 +01:00
|
|
|
association.alias
|
2019-04-09 16:01:01 +02:00
|
|
|
].path = `${association.alias}.ref`;
|
2019-04-09 12:09:03 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (!this._mongooseOptions.populate) {
|
|
|
|
this._mongooseOptions.populate = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
// Images are not displayed in populated data.
|
|
|
|
// We automatically populate morph relations.
|
|
|
|
if (
|
|
|
|
association.nature === 'oneToManyMorph' ||
|
|
|
|
association.nature === 'manyToManyMorph'
|
|
|
|
) {
|
2019-04-09 16:01:01 +02:00
|
|
|
this._mongooseOptions.populate[
|
|
|
|
association.alias
|
|
|
|
] = {
|
2019-04-09 12:09:03 +02:00
|
|
|
path: association.alias,
|
|
|
|
match: {
|
2019-03-22 12:16:09 +01:00
|
|
|
[`${association.via}.${
|
|
|
|
association.filter
|
|
|
|
}`]: association.alias,
|
2019-04-09 16:01:01 +02:00
|
|
|
[`${
|
|
|
|
association.via
|
|
|
|
}.kind`]: definition.globalId,
|
2019-04-09 12:09:03 +02:00
|
|
|
},
|
|
|
|
options: {
|
|
|
|
sort: '-createdAt',
|
|
|
|
},
|
|
|
|
select: undefined,
|
|
|
|
model: undefined,
|
|
|
|
_docs: {},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
});
|
2018-08-29 23:27:49 +09:00
|
|
|
});
|
2019-04-09 12:09:03 +02:00
|
|
|
}
|
2018-10-08 16:45:24 +02:00
|
|
|
|
2019-04-09 12:09:03 +02:00
|
|
|
_.forEach(preLifecycle, (fn, key) => {
|
|
|
|
if (_.isFunction(target[model.toLowerCase()][fn])) {
|
|
|
|
collection.schema.pre(key, function(next) {
|
|
|
|
target[model.toLowerCase()]
|
|
|
|
[fn](this)
|
|
|
|
.then(next)
|
|
|
|
.catch(err => strapi.log.error(err));
|
|
|
|
});
|
|
|
|
}
|
2018-10-08 16:45:24 +02:00
|
|
|
});
|
|
|
|
|
2019-04-09 12:09:03 +02:00
|
|
|
const postLifecycle = {
|
|
|
|
validate: 'afterCreate',
|
|
|
|
findOneAndRemove: 'afterDestroy',
|
|
|
|
remove: 'afterDestroy',
|
|
|
|
update: 'afterUpdate',
|
|
|
|
updateOne: 'afterUpdate',
|
|
|
|
find: 'afterFetchAll',
|
|
|
|
findOne: 'afterFetch',
|
|
|
|
save: 'afterSave',
|
|
|
|
};
|
2018-08-26 03:26:57 +09:00
|
|
|
|
2019-04-09 12:09:03 +02:00
|
|
|
// Mongoose doesn't allow post 'remove' event on model.
|
|
|
|
// See https://github.com/Automattic/mongoose/issues/3054
|
|
|
|
_.forEach(postLifecycle, (fn, key) => {
|
|
|
|
if (_.isFunction(target[model.toLowerCase()][fn])) {
|
|
|
|
collection.schema.post(key, function(doc, next) {
|
|
|
|
target[model.toLowerCase()]
|
|
|
|
[fn](this, doc)
|
|
|
|
.then(next)
|
|
|
|
.catch(err => {
|
|
|
|
strapi.log.error(err);
|
|
|
|
next(err);
|
|
|
|
});
|
|
|
|
});
|
2018-10-08 16:45:24 +02:00
|
|
|
}
|
|
|
|
});
|
2018-08-26 03:26:57 +09:00
|
|
|
|
2019-04-09 12:09:03 +02: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,
|
|
|
|
});
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
// Use provided timestamps if the elemnets in the array are string else use default.
|
|
|
|
if (_.isArray(_.get(definition, 'options.timestamps'))) {
|
|
|
|
const timestamps = {
|
2019-04-09 16:01:01 +02:00
|
|
|
createdAt: _.isString(
|
|
|
|
_.get(definition, 'options.timestamps[0]'),
|
|
|
|
)
|
2019-04-09 12:09:03 +02:00
|
|
|
? _.get(definition, 'options.timestamps[0]')
|
|
|
|
: 'createdAt',
|
2019-04-09 16:01:01 +02:00
|
|
|
updatedAt: _.isString(
|
|
|
|
_.get(definition, 'options.timestamps[1]'),
|
|
|
|
)
|
2019-04-09 12:09:03 +02:00
|
|
|
? _.get(definition, 'options.timestamps[1]')
|
|
|
|
: 'updatedAt',
|
|
|
|
};
|
|
|
|
collection.schema.set('timestamps', timestamps);
|
|
|
|
} else {
|
2019-03-22 12:16:09 +01:00
|
|
|
collection.schema.set(
|
|
|
|
'timestamps',
|
2019-04-09 16:01:01 +02:00
|
|
|
_.get(definition, 'options.timestamps') === true,
|
2019-03-22 12:16:09 +01:00
|
|
|
);
|
2019-04-09 12:09:03 +02:00
|
|
|
_.set(
|
|
|
|
definition,
|
|
|
|
'options.timestamps',
|
2019-03-22 12:16:09 +01:00
|
|
|
_.get(definition, 'options.timestamps') === true
|
|
|
|
? ['createdAt', 'updatedAt']
|
2019-04-09 16:01:01 +02:00
|
|
|
: false,
|
2019-04-09 12:09:03 +02:00
|
|
|
);
|
|
|
|
}
|
2019-03-22 12:16:09 +01:00
|
|
|
collection.schema.set(
|
|
|
|
'minimize',
|
2019-04-09 16:01:01 +02:00
|
|
|
_.get(definition, 'options.minimize', false) === true,
|
2019-03-22 12:16:09 +01:00
|
|
|
);
|
2019-04-09 12:09:03 +02:00
|
|
|
|
2019-04-30 18:33:14 +02:00
|
|
|
// Save all attributes (with timestamps)
|
|
|
|
target[model].allAttributes = _.clone(definition.attributes);
|
|
|
|
|
2019-04-09 12:09:03 +02:00
|
|
|
collection.schema.options.toObject = collection.schema.options.toJSON = {
|
|
|
|
virtuals: true,
|
|
|
|
transform: function(doc, returned, opts) {
|
|
|
|
// Remover $numberDecimal nested property.
|
|
|
|
Object.keys(returned)
|
2019-04-09 16:01:01 +02:00
|
|
|
.filter(
|
|
|
|
key =>
|
|
|
|
returned[key] instanceof
|
|
|
|
mongoose.Types.Decimal128,
|
|
|
|
)
|
2019-04-09 12:09:03 +02:00
|
|
|
.forEach((key, index) => {
|
|
|
|
// Parse to float number.
|
2019-04-09 16:01:01 +02:00
|
|
|
returned[key] = parseFloat(
|
|
|
|
returned[key].toString(),
|
|
|
|
);
|
2019-04-09 12:09:03 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
morphAssociations.forEach(association => {
|
2019-03-22 12:16:09 +01:00
|
|
|
if (
|
|
|
|
Array.isArray(returned[association.alias]) &&
|
|
|
|
returned[association.alias].length > 0
|
|
|
|
) {
|
2019-04-09 12:09:03 +02:00
|
|
|
// Reformat data by bypassing the many-to-many relationship.
|
|
|
|
switch (association.nature) {
|
|
|
|
case 'oneMorphToOne':
|
2019-04-09 16:01:01 +02:00
|
|
|
returned[association.alias] =
|
|
|
|
returned[association.alias][0].ref;
|
2019-04-09 12:09:03 +02:00
|
|
|
break;
|
|
|
|
case 'manyMorphToMany':
|
|
|
|
case 'manyMorphToOne':
|
2019-04-09 16:01:01 +02:00
|
|
|
returned[association.alias] = returned[
|
|
|
|
association.alias
|
|
|
|
].map(obj => obj.ref);
|
2019-04-09 12:09:03 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
};
|
2018-10-08 16:45:24 +02:00
|
|
|
|
2019-04-09 12:09:03 +02:00
|
|
|
// Instantiate model.
|
2019-03-22 12:16:09 +01:00
|
|
|
const Model = instance.model(
|
|
|
|
definition.globalId,
|
|
|
|
collection.schema,
|
2019-04-09 16:01:01 +02:00
|
|
|
definition.collectionName,
|
2019-03-22 12:16:09 +01:00
|
|
|
);
|
2018-10-08 16:45:24 +02:00
|
|
|
|
2019-04-09 12:09:03 +02:00
|
|
|
if (!plugin) {
|
|
|
|
global[definition.globalName] = Model;
|
|
|
|
}
|
2018-10-08 16:45:24 +02:00
|
|
|
|
2019-04-09 12:09:03 +02:00
|
|
|
// Expose ORM functions through the `target` object.
|
|
|
|
target[model] = _.assign(Model, target[model]);
|
2018-10-08 16:45:24 +02:00
|
|
|
|
2019-04-09 12:09:03 +02:00
|
|
|
// Push attributes to be aware of model schema.
|
|
|
|
target[model]._attributes = definition.attributes;
|
|
|
|
target[model].updateRelations = relations.update;
|
|
|
|
} catch (err) {
|
2019-04-09 16:01:01 +02:00
|
|
|
strapi.log.error(
|
|
|
|
'Impossible to register the `' + model + '` model.',
|
|
|
|
);
|
2019-04-09 12:09:03 +02:00
|
|
|
strapi.log.error(err);
|
|
|
|
strapi.stop();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2018-10-08 16:45:24 +02:00
|
|
|
|
2019-04-09 12:09:03 +02:00
|
|
|
// Parse every authenticated model.
|
|
|
|
_.forEach(models, (definition, model) => {
|
2019-04-09 16:01:01 +02:00
|
|
|
definition.globalName = _.upperFirst(
|
|
|
|
_.camelCase(definition.globalId),
|
|
|
|
);
|
2018-10-08 16:45:24 +02:00
|
|
|
|
2019-04-09 12:09:03 +02:00
|
|
|
// Make sure the model has a connection.
|
|
|
|
// If not, use the default connection.
|
|
|
|
if (_.isEmpty(definition.connection)) {
|
2019-03-22 12:16:09 +01:00
|
|
|
definition.connection =
|
|
|
|
strapi.config.currentEnvironment.database.defaultConnection;
|
2019-04-09 12:09:03 +02:00
|
|
|
}
|
2018-10-08 16:45:24 +02:00
|
|
|
|
2019-04-09 12:09:03 +02: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.',
|
|
|
|
);
|
|
|
|
strapi.stop();
|
|
|
|
}
|
2018-10-08 16:45:24 +02:00
|
|
|
|
2019-04-09 12:09:03 +02:00
|
|
|
// Add some informations about ORM & client connection
|
|
|
|
definition.orm = 'mongoose';
|
2019-03-22 12:16:09 +01:00
|
|
|
definition.client = _.get(
|
|
|
|
strapi.config.connections[definition.connection],
|
2019-04-09 16:01:01 +02:00
|
|
|
'client',
|
2019-03-22 12:16:09 +01:00
|
|
|
);
|
2019-04-09 12:09:03 +02:00
|
|
|
definition.associations = [];
|
2018-10-08 16:45:24 +02:00
|
|
|
|
2019-04-09 12:09:03 +02:00
|
|
|
// Register the final model for Mongoose.
|
|
|
|
definition.loadedModel = _.cloneDeep(definition.attributes);
|
2017-01-04 17:21:24 +01:00
|
|
|
|
2019-04-09 12:09:03 +02:00
|
|
|
// Initialize the global variable with the
|
|
|
|
// capitalized model name.
|
|
|
|
if (!plugin) {
|
|
|
|
global[definition.globalName] = {};
|
|
|
|
}
|
2018-08-29 23:27:49 +09:00
|
|
|
|
2019-04-09 12:09:03 +02:00
|
|
|
if (_.isEmpty(definition.attributes)) {
|
|
|
|
// Generate empty schema
|
|
|
|
_.set(
|
|
|
|
strapi.config.hook.settings.mongoose,
|
2019-03-22 12:16:09 +01:00
|
|
|
'collections.' +
|
|
|
|
mongooseUtils.toCollectionName(definition.globalName) +
|
|
|
|
'.schema',
|
2019-04-09 12:09:03 +02:00
|
|
|
new instance.Schema({}),
|
|
|
|
);
|
2018-08-29 23:27:49 +09:00
|
|
|
|
2019-04-09 12:09:03 +02:00
|
|
|
return loadedAttributes();
|
|
|
|
}
|
2018-08-29 23:27:49 +09:00
|
|
|
|
2019-04-09 12:09:03 +02:00
|
|
|
// Call this callback function after we are done parsing
|
|
|
|
// all attributes for relationships-- see below.
|
|
|
|
const done = _.after(_.size(definition.attributes), () => {
|
|
|
|
// Generate schema without virtual populate
|
|
|
|
const schema = new instance.Schema(
|
|
|
|
_.omitBy(definition.loadedModel, model => {
|
|
|
|
return model.type === 'virtual';
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
|
|
|
|
_.set(
|
|
|
|
strapi.config.hook.settings.mongoose,
|
2019-03-22 12:16:09 +01:00
|
|
|
'collections.' +
|
|
|
|
mongooseUtils.toCollectionName(definition.globalName) +
|
|
|
|
'.schema',
|
2019-04-09 12:09:03 +02:00
|
|
|
schema,
|
|
|
|
);
|
|
|
|
|
|
|
|
loadedAttributes();
|
|
|
|
});
|
2018-08-29 23:27:49 +09:00
|
|
|
|
2019-04-09 12:09:03 +02: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 =
|
2019-03-22 12:16:09 +01:00
|
|
|
_.get(
|
2019-04-09 16:01:01 +02:00
|
|
|
utilsModels.getNature(
|
|
|
|
details,
|
|
|
|
name,
|
|
|
|
undefined,
|
|
|
|
model.toLowerCase(),
|
|
|
|
),
|
|
|
|
'verbose',
|
2019-03-22 12:16:09 +01:00
|
|
|
) || '';
|
2018-08-29 01:33:58 +09:00
|
|
|
|
2019-04-09 12:09:03 +02:00
|
|
|
// Build associations key
|
2019-04-09 16:01:01 +02:00
|
|
|
utilsModels.defineAssociations(
|
|
|
|
model.toLowerCase(),
|
|
|
|
definition,
|
|
|
|
details,
|
|
|
|
name,
|
|
|
|
);
|
2018-08-29 23:27:49 +09:00
|
|
|
|
2019-04-09 12:09:03 +02:00
|
|
|
if (_.isEmpty(verbose)) {
|
2019-04-09 16:01:01 +02:00
|
|
|
definition.loadedModel[name].type = utils(
|
|
|
|
instance,
|
|
|
|
).convertType(details.type);
|
2018-08-29 23:27:49 +09:00
|
|
|
}
|
|
|
|
|
2019-04-09 12:09:03 +02:00
|
|
|
switch (verbose) {
|
|
|
|
case 'hasOne': {
|
|
|
|
const ref = details.plugin
|
2019-04-09 16:01:01 +02:00
|
|
|
? strapi.plugins[details.plugin].models[details.model]
|
|
|
|
.globalId
|
2019-04-09 12:09:03 +02:00
|
|
|
: strapi.models[details.model].globalId;
|
|
|
|
|
|
|
|
definition.loadedModel[name] = {
|
|
|
|
type: instance.Schema.Types.ObjectId,
|
|
|
|
ref,
|
|
|
|
};
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'hasMany': {
|
|
|
|
const FK = _.find(definition.associations, {
|
|
|
|
alias: name,
|
|
|
|
});
|
|
|
|
const ref = details.plugin
|
2019-04-09 16:01:01 +02:00
|
|
|
? strapi.plugins[details.plugin].models[
|
|
|
|
details.collection
|
|
|
|
].globalId
|
2019-04-09 12:09:03 +02:00
|
|
|
: strapi.models[details.collection].globalId;
|
|
|
|
|
|
|
|
if (FK) {
|
|
|
|
definition.loadedModel[name] = {
|
|
|
|
type: 'virtual',
|
|
|
|
ref,
|
|
|
|
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,
|
|
|
|
ref,
|
|
|
|
},
|
|
|
|
];
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'belongsTo': {
|
|
|
|
const FK = _.find(definition.associations, {
|
|
|
|
alias: name,
|
|
|
|
});
|
|
|
|
const ref = details.plugin
|
2019-04-09 16:01:01 +02:00
|
|
|
? strapi.plugins[details.plugin].models[details.model]
|
|
|
|
.globalId
|
2019-04-09 12:09:03 +02:00
|
|
|
: strapi.models[details.model].globalId;
|
|
|
|
|
|
|
|
if (
|
|
|
|
FK &&
|
|
|
|
FK.nature !== 'oneToOne' &&
|
|
|
|
FK.nature !== 'manyToOne' &&
|
|
|
|
FK.nature !== 'oneWay' &&
|
|
|
|
FK.nature !== 'oneToMorph'
|
|
|
|
) {
|
|
|
|
definition.loadedModel[name] = {
|
|
|
|
type: 'virtual',
|
|
|
|
ref,
|
|
|
|
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,
|
|
|
|
ref,
|
|
|
|
};
|
|
|
|
}
|
2018-08-29 23:27:49 +09:00
|
|
|
|
2019-04-09 12:09:03 +02:00
|
|
|
break;
|
2018-10-08 16:45:24 +02:00
|
|
|
}
|
2019-04-09 12:09:03 +02:00
|
|
|
case 'belongsToMany': {
|
|
|
|
const FK = _.find(definition.associations, {
|
|
|
|
alias: name,
|
|
|
|
});
|
|
|
|
const ref = details.plugin
|
2019-04-09 16:01:01 +02:00
|
|
|
? strapi.plugins[details.plugin].models[
|
|
|
|
details.collection
|
|
|
|
].globalId
|
2019-04-09 12:09:03 +02:00
|
|
|
: strapi.models[details.collection].globalId;
|
|
|
|
|
|
|
|
// One-side of the relationship has to be a virtual field to be bidirectional.
|
2019-04-09 16:01:01 +02:00
|
|
|
if (
|
|
|
|
(FK && _.isUndefined(FK.via)) ||
|
|
|
|
details.dominant !== true
|
|
|
|
) {
|
2019-04-09 12:09:03 +02:00
|
|
|
definition.loadedModel[name] = {
|
|
|
|
type: 'virtual',
|
|
|
|
ref,
|
|
|
|
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,
|
|
|
|
ref,
|
|
|
|
},
|
|
|
|
];
|
|
|
|
}
|
|
|
|
break;
|
2018-10-08 16:45:24 +02:00
|
|
|
}
|
2019-04-09 12:09:03 +02:00
|
|
|
case 'morphOne': {
|
|
|
|
const FK = _.find(definition.associations, {
|
|
|
|
alias: name,
|
|
|
|
});
|
|
|
|
const ref = details.plugin
|
2019-04-09 16:01:01 +02:00
|
|
|
? strapi.plugins[details.plugin].models[details.model]
|
|
|
|
.globalId
|
2019-04-09 12:09:03 +02:00
|
|
|
: strapi.models[details.model].globalId;
|
|
|
|
|
|
|
|
definition.loadedModel[name] = {
|
|
|
|
type: 'virtual',
|
|
|
|
ref,
|
|
|
|
via: `${FK.via}.ref`,
|
|
|
|
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
|
2019-04-09 16:01:01 +02:00
|
|
|
? strapi.plugins[details.plugin].models[
|
|
|
|
details.collection
|
|
|
|
].globalId
|
2019-04-09 12:09:03 +02:00
|
|
|
: strapi.models[details.collection].globalId;
|
|
|
|
|
|
|
|
definition.loadedModel[name] = {
|
|
|
|
type: 'virtual',
|
|
|
|
ref,
|
|
|
|
via: `${FK.via}.ref`,
|
|
|
|
};
|
|
|
|
|
|
|
|
// Set this info to be able to see if this field is a real database's field.
|
|
|
|
details.isVirtual = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'belongsToMorph': {
|
|
|
|
definition.loadedModel[name] = {
|
|
|
|
kind: String,
|
|
|
|
[details.filter]: String,
|
|
|
|
ref: {
|
|
|
|
type: instance.Schema.Types.ObjectId,
|
|
|
|
refPath: `${name}.kind`,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'belongsToManyMorph': {
|
|
|
|
definition.loadedModel[name] = [
|
|
|
|
{
|
|
|
|
kind: String,
|
|
|
|
[details.filter]: String,
|
|
|
|
ref: {
|
|
|
|
type: instance.Schema.Types.ObjectId,
|
|
|
|
refPath: `${name}.kind`,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2018-08-29 23:27:49 +09:00
|
|
|
|
2019-04-09 12:09:03 +02:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
// Mount `./api` models.
|
2019-04-09 16:01:01 +02:00
|
|
|
mountModels(
|
|
|
|
_.pickBy(strapi.models, { connection: connectionName }),
|
|
|
|
strapi.models,
|
|
|
|
);
|
2019-04-09 12:09:03 +02:00
|
|
|
|
|
|
|
// Mount `./admin` models.
|
2019-04-09 16:01:01 +02:00
|
|
|
mountModels(
|
|
|
|
_.pickBy(strapi.admin.models, { connection: connectionName }),
|
|
|
|
strapi.admin.models,
|
|
|
|
);
|
2019-04-09 12:09:03 +02:00
|
|
|
|
|
|
|
// Mount `./plugins` models.
|
|
|
|
_.forEach(strapi.plugins, (plugin, name) => {
|
|
|
|
mountModels(
|
|
|
|
_.pickBy(strapi.plugins[name].models, {
|
|
|
|
connection: connectionName,
|
|
|
|
}),
|
|
|
|
plugin.models,
|
|
|
|
name,
|
|
|
|
);
|
2018-10-08 16:45:24 +02:00
|
|
|
});
|
2019-04-09 12:09:03 +02:00
|
|
|
|
|
|
|
cb();
|
|
|
|
},
|
|
|
|
),
|
|
|
|
|
|
|
|
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':
|
|
|
|
result.key = 'sort';
|
|
|
|
result.value = _.toLower(value) === 'desc' ? '-' : '';
|
|
|
|
result.value += key;
|
|
|
|
break;
|
|
|
|
case '_start':
|
|
|
|
result.key = 'start';
|
|
|
|
result.value = parseFloat(value);
|
|
|
|
break;
|
|
|
|
case '_limit':
|
|
|
|
result.key = 'limit';
|
|
|
|
result.value = parseFloat(value);
|
|
|
|
break;
|
|
|
|
case '_populate':
|
2019-03-22 12:16:09 +01:00
|
|
|
result.key = 'populate';
|
2019-04-09 12:09:03 +02:00
|
|
|
result.value = value;
|
|
|
|
break;
|
|
|
|
case '_contains':
|
|
|
|
result.key = `where.${key}`;
|
|
|
|
result.value = {
|
|
|
|
$regex: value,
|
|
|
|
$options: 'i',
|
|
|
|
};
|
|
|
|
break;
|
|
|
|
case '_containss':
|
|
|
|
result.key = `where.${key}.$regex`;
|
|
|
|
result.value = value;
|
|
|
|
break;
|
|
|
|
case '_in':
|
|
|
|
result.key = `where.${key}.$in`;
|
|
|
|
result.value = _.castArray(value);
|
|
|
|
break;
|
|
|
|
case '_nin':
|
|
|
|
result.key = `where.${key}.$nin`;
|
|
|
|
result.value = _.castArray(value);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
result = undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
},
|
2019-03-22 12:16:09 +01:00
|
|
|
buildQuery,
|
2019-04-09 12:09:03 +02:00
|
|
|
},
|
|
|
|
relations,
|
|
|
|
);
|
2016-07-12 11:15:01 +02:00
|
|
|
|
|
|
|
return hook;
|
2019-01-28 11:56:27 +01:00
|
|
|
};
|