strapi-utils
Advanced tools
Comparing version 3.4.0-beta.0 to 3.4.0-beta.1
@@ -14,3 +14,3 @@ 'use strict'; | ||
* @param {Object} options - Options | ||
* @param {string} options.model - Strapi model | ||
* @param {Object} options.model - Strapi model | ||
* @param {string} options.field - path of relation / attribute | ||
@@ -96,15 +96,20 @@ */ | ||
const hasDeepFilters = (whereClauses = [], { minDepth = 1 } = {}) => { | ||
return ( | ||
whereClauses.filter(({ field, operator, value }) => { | ||
if (BOOLEAN_OPERATORS.includes(operator)) { | ||
return value.filter(hasDeepFilters).length > 0; | ||
} | ||
const hasDeepFilters = ({ where = [], sort = [] }, { minDepth = 1 } = {}) => { | ||
// A query uses deep filtering if some of the clauses contains a sort or a match expression on a field of a relation | ||
return field.split('.').length > minDepth; | ||
}).length > 0 | ||
); | ||
// We don't use minDepth here because deep sorting is limited to depth 1 | ||
const hasDeepSortClauses = sort.some(({ field }) => field.includes('.')); | ||
const hasDeepWhereClauses = where.some(({ field, operator, value }) => { | ||
if (BOOLEAN_OPERATORS.includes(operator)) { | ||
return value.some(clauses => hasDeepFilters({ where: clauses })); | ||
} | ||
return field.split('.').length > minDepth; | ||
}); | ||
return hasDeepSortClauses || hasDeepWhereClauses; | ||
}; | ||
const normalizeClauses = (whereClauses, { model }) => { | ||
const normalizeWhereClauses = (whereClauses, { model }) => { | ||
return whereClauses | ||
@@ -117,3 +122,3 @@ .filter(({ value }) => !_.isNil(value)) | ||
operator, | ||
value: value.map(clauses => normalizeClauses(clauses, { model })), | ||
value: value.map(clauses => normalizeWhereClauses(clauses, { model })), | ||
}; | ||
@@ -140,2 +145,18 @@ } | ||
const normalizeSortClauses = (clauses, { model }) => { | ||
const normalizedClauses = clauses.map(({ field, order }) => ({ | ||
field: normalizeFieldName({ model, field }), | ||
order, | ||
})); | ||
normalizedClauses.forEach(({ field }) => { | ||
if (field.includes('.')) { | ||
// Check if the relational field exists | ||
getAssociationFromFieldKey({ model, field }); | ||
} | ||
}); | ||
return normalizedClauses; | ||
}; | ||
/** | ||
@@ -149,5 +170,7 @@ * | ||
const buildQuery = ({ model, filters = {}, ...rest }) => { | ||
const { where, sort } = filters; | ||
// Validate query clauses | ||
if (filters.where && Array.isArray(filters.where)) { | ||
if (hasDeepFilters(filters.where, { minDepth: 2 })) { | ||
if ([where, sort].some(Array.isArray)) { | ||
if (hasDeepFilters({ where, sort }, { minDepth: 2 })) { | ||
strapi.log.warn( | ||
@@ -158,7 +181,13 @@ 'Deep filtering queries should be used carefully (e.g Can cause performance issues).\nWhen possible build custom routes which will in most case be more optimised.' | ||
// cast where clauses to match the inner types | ||
filters.where = normalizeClauses(filters.where, { model }); | ||
if (sort) { | ||
filters.sort = normalizeSortClauses(sort, { model }); | ||
} | ||
if (where) { | ||
// Cast where clauses to match the inner types | ||
filters.where = normalizeWhereClauses(where, { model }); | ||
} | ||
} | ||
// call the orm's buildQuery implementation | ||
// call the ORM's buildQuery implementation | ||
return strapi.db.connectors.get(model.orm).buildQuery({ model, filters, ...rest }); | ||
@@ -165,0 +194,0 @@ }; |
{ | ||
"name": "strapi-utils", | ||
"version": "3.4.0-beta.0", | ||
"version": "3.4.0-beta.1", | ||
"description": "Shared utilities for the Strapi packages", | ||
@@ -48,3 +48,3 @@ "homepage": "http://strapi.io", | ||
"license": "SEE LICENSE IN LICENSE", | ||
"gitHead": "84f9808472e8247f9986fce66ac1720ec40c8a15" | ||
"gitHead": "baa71f9e10fc5536cdfbd06a6e050ab9ccb6aa8c" | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
102466
3024
1