mirror of
https://github.com/strapi/strapi.git
synced 2025-10-10 15:43:41 +00:00
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:
commit
0c4709153d
@ -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 {
|
||||||
|
@ -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);
|
||||||
}
|
}
|
||||||
|
@ -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,
|
||||||
};
|
};
|
||||||
|
Loading…
x
Reference in New Issue
Block a user