Query Builder Parameter Handling in Entity Manager#
Strapi's database layer exposes an entity manager (createEntityManager in packages/core/database/src/entity-manager/index.ts) whose methods are not symmetric in which query parameters they forward to the underlying query builder. This asymmetry is intentional but non-obvious: it prevents dangerous or semantically incorrect SQL for bulk/aggregate operations.
Parameter forwarding by method#
The query builder's init(params) method accepts a full params object ā _q, where, filters, select, limit, offset, orderBy, groupBy, populate ā and applies each non-nil value. Entity-manager methods that call init only pass a subset of params using Lodash's pick, deliberately discarding the rest.
| Method | Params forwarded to query builder | How |
|---|---|---|
findOne | all | qb.init(params) |
findMany | all | qb.init(params) |
count | _q, where, filters | qb.init(pick(['_q', 'where', 'filters'], params)) |
deleteMany | _q, where, filters | qb.init(pick(['_q', 'where', 'filters'], params)) |
updateMany | where only | qb.where(where) directly ā no _q or filters |
Sources: for count, for deleteMany, for updateMany.
Why the differences matter#
count and deleteMany ā pick(['_q', 'where', 'filters'], params)
Both use the same three-key whitelist . The inline comment explains the rationale for deleteMany:
Only apply filter criteria (
_q/where/filters), same as count ā not full findMany params.limit,offset,orderBy,populate, etc. must be ignored:populatethrows on delete results, and pagination keys can makedeleteManydiverge fromfindManyor delete an unexpected slice.
In other words: limit/offset on a delete would silently delete a different row-set than the caller expects, and populate causes a runtime error because delete queries return a count, not entity objects.
updateMany ā where only
updateMany does not call init at all. It destructures where and data directly from params and calls qb.where(where) . This means:
_q(full-text search) is silently ignored ā passing_qtoupdateManyhas no effect.filtersis silently ignored ā programmatic filter functions are not applied.
This is a footgun when porting logic from findMany to updateMany: a caller might pass { _q: 'foo', data: { ... } } expecting full-text-search scoping and get a full-table update instead.
What each forwarded parameter does#
Once inside init, each parameter is dispatched to the relevant query-builder method and evaluated lazily during processState() at execution time :
whereāqb.where(where)ā synchronous predicate, supports nested operators ($eq,$in,$gt, etc.) and relation traversal via automatic joins ._qāqb.search(searchQuery)ā generates dialect-specificLIKE/ILIKEclauses across all searchable string/number attributes .filtersā stored instate.filters; duringprocessState, if it's a function it's called with{ qb, uid, meta, db }and the return value is merged intostate.where; if it's an object it is pushed directly .
Key files#
| File | Role |
|---|---|
entity-manager/index.ts | updateMany, deleteMany, count implementations with their pick calls |
query/query-builder.ts | init() and processState() ā where params become SQL |
query/helpers/where.ts | where / filters object processing and join generation |
query/helpers/search.ts | _q search ā dialect-specific LIKE/ILIKE generation |