Merge pull request #4923 from strapi/fix/mongoose-deep-id-filters

fix(mongoose): Add manyWay match expr and fix conversion of id and _id to ObjectId
This commit is contained in:
Alexandre BODIN 2020-01-07 14:04:33 +01:00 committed by GitHub
commit 0c4709153d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 31 additions and 2 deletions

View File

@ -1,3 +1,5 @@
'use strict';
const _ = require('lodash'); const _ = require('lodash');
const utils = require('./utils')(); const utils = require('./utils')();
@ -294,6 +296,15 @@ const buildLookupMatch = ({ assoc }) => {
}, },
}; };
} }
case 'manyWay': {
return {
$match: {
$expr: {
$in: ['$_id', '$$localAlias'],
},
},
};
}
case 'manyToMany': { case 'manyToMany': {
if (assoc.dominant === true) { if (assoc.dominant === true) {
return { return {

View File

@ -100,6 +100,8 @@ module.exports = (mongoose = Mongoose) => {
}; };
const valueToId = value => { const valueToId = value => {
if (Array.isArray(value)) return value.map(valueToId);
if (isMongoId(value)) { if (isMongoId(value)) {
return mongoose.Types.ObjectId(value); return mongoose.Types.ObjectId(value);
} }

View File

@ -17,7 +17,7 @@ const isAttribute = (model, field) =>
* Returns the model, attribute name and association from a path of relation * Returns the model, attribute name and association from a path of relation
* @param {Object} options - Options * @param {Object} options - Options
* @param {string} options.model - Strapi model * @param {string} options.model - Strapi model
* @param {string} options.field - pathj of relation / attribute * @param {string} options.field - path of relation / attribute
*/ */
const getAssociationFromFieldKey = ({ model, field }) => { const getAssociationFromFieldKey = ({ model, field }) => {
const fieldParts = field.split('.'); const fieldParts = field.split('.');
@ -82,6 +82,22 @@ const castValue = ({ type, value, operator }) => {
if (operator === 'null') return parseType({ type: 'boolean', value }); if (operator === 'null') return parseType({ type: 'boolean', value });
return parseType({ type, value }); return parseType({ type, value });
}; };
/**
*
* @param {Object} options - Options
* @param {string} options.model - The model
* @param {string} options.field - path of relation / attribute
*/
const normalizeFieldName = ({ model, field }) => {
const fieldPath = field.split('.');
return _.last(fieldPath) === 'id'
? _.initial(fieldPath)
.concat(model.primaryKey)
.join('.')
: fieldPath.join('.');
};
/** /**
* *
* @param {Object} options - Options * @param {Object} options - Options
@ -116,7 +132,7 @@ const buildQuery = ({ model, filters = {}, ...rest }) => {
const castedValue = castInput({ type, operator, value }); const castedValue = castInput({ type, operator, value });
return { return {
field: field === 'id' ? model.primaryKey : field, field: normalizeFieldName({ model, field }),
operator, operator,
value: castedValue, value: castedValue,
}; };