2017-11-15 16:59:12 +01:00
|
|
|
const _ = require('lodash');
|
|
|
|
|
2018-09-24 22:30:12 +02:00
|
|
|
const buildTempFieldPath = field => {
|
|
|
|
return `__${field}`;
|
|
|
|
};
|
|
|
|
|
|
|
|
const restoreRealFieldPath = (field, prefix) => {
|
|
|
|
return `${prefix}${field}`;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const generateLookupStage = (
|
|
|
|
strapiModel,
|
|
|
|
{ whitelistedPopulate = null, prefixPath = '' } = {}
|
|
|
|
) => {
|
|
|
|
const result = strapiModel.associations
|
|
|
|
.filter(ast => {
|
|
|
|
if (whitelistedPopulate) {
|
|
|
|
return _.includes(whitelistedPopulate, ast.alias);
|
|
|
|
}
|
|
|
|
return ast.autoPopulate;
|
|
|
|
})
|
|
|
|
.reduce((acc, ast) => {
|
|
|
|
const model = ast.plugin
|
|
|
|
? strapi.plugins[ast.plugin].models[ast.collection || ast.model]
|
|
|
|
: strapi.models[ast.collection || ast.model];
|
|
|
|
|
|
|
|
const from = model.collectionName;
|
|
|
|
const isDominantAssociation = ast.dominant || !!ast.model;
|
|
|
|
|
|
|
|
const _localField = !isDominantAssociation
|
|
|
|
? '_id'
|
|
|
|
: ast.via === strapiModel.collectionName || ast.via === 'related'
|
|
|
|
? '_id'
|
|
|
|
: ast.alias;
|
|
|
|
|
|
|
|
const localField = `${prefixPath}${_localField}`;
|
2018-09-14 00:23:27 +02:00
|
|
|
|
2018-09-24 22:30:12 +02:00
|
|
|
const foreignField = ast.filter
|
|
|
|
? `${ast.via}.ref`
|
|
|
|
: isDominantAssociation
|
|
|
|
? ast.via === strapiModel.collectionName
|
|
|
|
? ast.via
|
|
|
|
: '_id'
|
|
|
|
: ast.via === strapiModel.collectionName
|
|
|
|
? '_id'
|
|
|
|
: ast.via;
|
2018-09-14 00:23:27 +02:00
|
|
|
|
2018-09-24 22:30:12 +02:00
|
|
|
// Add the juncture like the `.populate()` function
|
|
|
|
const asTempPath = buildTempFieldPath(ast.alias, prefixPath);
|
|
|
|
const asRealPath = restoreRealFieldPath(ast.alias, prefixPath);
|
|
|
|
acc.push({
|
|
|
|
$lookup: {
|
|
|
|
from,
|
|
|
|
localField,
|
|
|
|
foreignField,
|
|
|
|
as: asTempPath,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
// Unwind the relation's result if only one is expected
|
|
|
|
if (ast.type === 'model') {
|
2018-09-14 00:23:27 +02:00
|
|
|
acc.push({
|
2018-09-24 22:30:12 +02:00
|
|
|
$unwind: {
|
|
|
|
path: `$${asTempPath}`,
|
|
|
|
preserveNullAndEmptyArrays: true,
|
|
|
|
},
|
2018-09-14 00:23:27 +02:00
|
|
|
});
|
2018-09-24 22:30:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Preserve relation field if it is empty
|
|
|
|
acc.push({
|
|
|
|
$addFields: {
|
|
|
|
[asRealPath]: {
|
|
|
|
$ifNull: [`$${asTempPath}`, null],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
// Remove temp field
|
|
|
|
acc.push({
|
|
|
|
$project: {
|
|
|
|
[asTempPath]: 0,
|
|
|
|
},
|
|
|
|
});
|
2018-09-14 00:23:27 +02:00
|
|
|
|
2018-09-24 22:30:12 +02:00
|
|
|
return acc;
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const generateMatchStage = (
|
|
|
|
strapiModel,
|
|
|
|
filters,
|
|
|
|
{ prefixPath = '' } = {}
|
|
|
|
) => {
|
|
|
|
const result = _.chain(filters)
|
|
|
|
.get('relations')
|
|
|
|
.reduce((acc, relationFilters, relationName) => {
|
|
|
|
const association = strapiModel.associations.find(
|
|
|
|
a => a.alias === relationName
|
|
|
|
);
|
|
|
|
|
|
|
|
// Ignore association if it's not been found
|
|
|
|
if (!association) {
|
|
|
|
return acc;
|
|
|
|
}
|
|
|
|
|
|
|
|
const model = association.plugin
|
|
|
|
? strapi.plugins[association.plugin].models[
|
|
|
|
association.collection || association.model
|
|
|
|
]
|
|
|
|
: strapi.models[association.collection || association.model];
|
|
|
|
|
|
|
|
_.forEach(relationFilters, (value, key) => {
|
|
|
|
if (key !== 'relations') {
|
2018-09-14 00:23:27 +02:00
|
|
|
acc.push({
|
2018-09-24 22:30:12 +02:00
|
|
|
$match: { [`${prefixPath}${relationName}.${key}`]: value },
|
2018-09-14 00:23:27 +02:00
|
|
|
});
|
2018-09-24 22:30:12 +02:00
|
|
|
} else {
|
|
|
|
const nextPrefixedPath = `${prefixPath}${relationName}.`;
|
|
|
|
acc.push(
|
|
|
|
...generateLookupStage(model, {
|
|
|
|
whitelistedPopulate: _.keys(value),
|
|
|
|
prefixPath: nextPrefixedPath,
|
|
|
|
}),
|
|
|
|
...generateMatchStage(model, relationFilters, {
|
|
|
|
prefixPath: nextPrefixedPath,
|
|
|
|
})
|
2018-09-14 00:23:27 +02:00
|
|
|
);
|
|
|
|
}
|
2018-09-24 22:30:12 +02:00
|
|
|
});
|
|
|
|
return acc;
|
|
|
|
}, [])
|
|
|
|
.value();
|
2018-09-14 00:23:27 +02:00
|
|
|
|
2018-09-24 22:30:12 +02:00
|
|
|
return result;
|
|
|
|
};
|
2018-09-14 00:23:27 +02:00
|
|
|
|
2018-09-24 22:30:12 +02:00
|
|
|
module.exports = {
|
|
|
|
find: async function (filters = {}, populate) {
|
|
|
|
// Generate stages.
|
|
|
|
const populateStage = generateLookupStage(this, { whitelistedPopulate: populate });
|
|
|
|
const matchStage = generateMatchStage(this, filters);
|
|
|
|
|
|
|
|
const result = this.aggregate([
|
|
|
|
{
|
|
|
|
$match: filters.where || {}, // Direct relation filter
|
|
|
|
},
|
|
|
|
...populateStage, // Nested-Population
|
|
|
|
...matchStage, // Nested relation filter
|
|
|
|
]);
|
2018-09-14 00:23:27 +02:00
|
|
|
|
2018-09-24 22:30:12 +02:00
|
|
|
if (_.has(filters, 'start')) result.skip(filters.start);
|
|
|
|
if (_.has(filters, 'limit')) result.limit(filters.limit);
|
|
|
|
if (_.has(filters, 'sort')) result.sort(filters.sort);
|
2018-09-14 00:23:27 +02:00
|
|
|
|
|
|
|
return result;
|
2017-11-15 16:59:12 +01:00
|
|
|
},
|
|
|
|
|
2018-01-17 18:50:12 +01:00
|
|
|
count: async function (params = {}) {
|
2017-11-15 16:59:12 +01:00
|
|
|
return Number(await this
|
2018-01-17 18:50:12 +01:00
|
|
|
.count(params));
|
2017-11-15 16:59:12 +01:00
|
|
|
},
|
|
|
|
|
2018-01-17 18:50:12 +01:00
|
|
|
findOne: async function (params, populate) {
|
2018-01-23 18:54:17 +01:00
|
|
|
const primaryKey = params[this.primaryKey] || params.id;
|
|
|
|
|
|
|
|
if (primaryKey) {
|
|
|
|
params = {
|
|
|
|
[this.primaryKey]: primaryKey
|
2018-04-30 18:26:56 +02:00
|
|
|
};
|
2017-12-14 15:14:53 +01:00
|
|
|
}
|
2018-01-19 17:57:10 +01:00
|
|
|
|
2017-11-15 16:59:12 +01:00
|
|
|
return this
|
2017-11-16 14:29:49 +01:00
|
|
|
.findOne(params)
|
2018-01-19 17:57:10 +01:00
|
|
|
.populate(populate || this.associations.map(x => x.alias).join(' '))
|
|
|
|
.lean();
|
2017-11-15 16:59:12 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
create: async function (params) {
|
2017-11-29 17:09:19 +01:00
|
|
|
return this.create(Object.keys(params).reduce((acc, current) => {
|
2018-01-17 18:50:12 +01:00
|
|
|
if (_.get(this._attributes, [current, 'type']) || _.get(this._attributes, [current, 'model'])) {
|
2017-11-29 17:09:19 +01:00
|
|
|
acc[current] = params[current];
|
2017-11-15 16:59:12 +01:00
|
|
|
}
|
2017-12-06 11:47:39 +01:00
|
|
|
|
2017-11-15 16:59:12 +01:00
|
|
|
return acc;
|
2017-12-06 11:47:39 +01:00
|
|
|
}, {}))
|
2018-04-30 18:26:56 +02:00
|
|
|
.catch((err) => {
|
|
|
|
if (err.message.indexOf('index:') !== -1) {
|
|
|
|
const message = err.message.split('index:');
|
|
|
|
const field = _.words(_.last(message).split('_')[0]);
|
|
|
|
const error = { message: `This ${field} is already taken`, field };
|
2018-01-25 18:12:29 +01:00
|
|
|
|
2018-04-30 18:26:56 +02:00
|
|
|
throw error;
|
|
|
|
}
|
2017-12-06 15:11:55 +01:00
|
|
|
|
2018-04-30 18:26:56 +02:00
|
|
|
throw err;
|
|
|
|
});
|
2017-11-15 16:59:12 +01:00
|
|
|
},
|
|
|
|
|
2018-01-23 15:38:43 +01:00
|
|
|
update: async function (search, params = {}) {
|
|
|
|
if (_.isEmpty(params)) {
|
|
|
|
params = search;
|
2018-01-22 18:19:44 +01:00
|
|
|
}
|
|
|
|
|
2018-01-23 15:38:43 +01:00
|
|
|
const primaryKey = search[this.primaryKey] || search.id;
|
2018-01-22 18:19:44 +01:00
|
|
|
|
|
|
|
if (primaryKey) {
|
|
|
|
search = {
|
2018-01-23 15:38:43 +01:00
|
|
|
[this.primaryKey]: primaryKey
|
2018-04-30 18:26:56 +02:00
|
|
|
};
|
2018-01-22 18:19:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return this.update(search, params, {
|
2017-11-29 18:45:51 +01:00
|
|
|
strict: false
|
2017-12-06 15:11:55 +01:00
|
|
|
})
|
2018-04-30 18:26:56 +02:00
|
|
|
.catch((error) => {
|
|
|
|
const field = _.last(_.words(error.message.split('_')[0]));
|
|
|
|
const err = { message: `This ${field} is already taken`, field };
|
2017-12-06 15:11:55 +01:00
|
|
|
|
2018-04-30 18:26:56 +02:00
|
|
|
throw err;
|
|
|
|
});
|
2017-11-15 16:59:12 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
delete: async function (params) {
|
|
|
|
// Delete entry.
|
|
|
|
return this
|
|
|
|
.remove({
|
2017-11-29 18:45:51 +01:00
|
|
|
[this.primaryKey]: params[this.primaryKey] || params.id
|
2017-11-15 16:59:12 +01:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2018-05-03 16:26:57 +02:00
|
|
|
deleteMany: async function (params) {
|
|
|
|
// Delete entry.
|
|
|
|
return this
|
|
|
|
.remove({
|
|
|
|
[this.primaryKey]: {
|
|
|
|
$in: params[this.primaryKey] || params.id
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2017-11-27 12:19:36 +01:00
|
|
|
search: async function (params) {
|
|
|
|
const re = new RegExp(params.id);
|
|
|
|
|
|
|
|
return this
|
|
|
|
.find({
|
|
|
|
'$or': [
|
|
|
|
{ username: re },
|
|
|
|
{ email: re }
|
|
|
|
]
|
|
|
|
});
|
2017-11-30 12:27:04 +01:00
|
|
|
},
|
|
|
|
|
2018-01-17 18:50:12 +01:00
|
|
|
addPermission: async function (params) {
|
|
|
|
return this
|
|
|
|
.create(params);
|
|
|
|
},
|
|
|
|
|
|
|
|
removePermission: async function (params) {
|
|
|
|
return this
|
|
|
|
.remove(params);
|
2017-11-15 16:59:12 +01:00
|
|
|
}
|
|
|
|
};
|