v4 query warnings
Some query patterns in v4 component find are slow on large components. The response flags them with a warning header. The requests keep working and won't be rejected later. This page shows how to see the warnings and write faster queries.
What's changing
Warnings arrive in a coming release. Affected requests work exactly as today. No later release rejects them.
Am I affected?
You are affected if all of these are true:
- You query component data with find:
POST|GET /v4/system/component/{ref}/models,/models/stream, or the SDKfind(),findOne()or stream. - You use the v4 endpoints, or the SDK imported from
@ptkl/sdk/beta(v0.10). - Your query uses one of the patterns in Warning codes.
You are not affected if you only use:
- v3 endpoints, or the bare
@ptkl/sdkimport (it uses v3). v3 will not get these warnings. It keeps working, with a 15 second query time limit (408with a hint). POST /aggregate. See Aggregation.- get, create, update or delete by uuid.
How to see warnings
An affected request still works and returns this header:
Protokol-Query-Warninglists one entry per problem:<code>;field="<name>", comma separated. It contains field names only, never values.
Browser apps can read this header. It is exposed through CORS.
Reading them with fetch
const res = await fetch(`/v4/system/component/orders/models?limit=25`, { headers })
const warning = res.headers.get('Protokol-Query-Warning')
if (warning) console.warn('Protokol query warning:', warning)
Reading them with axios
const res = await axios.get('/v4/system/component/orders/models', { params: { limit: 25 } })
const warning = res.headers['protokol-query-warning']
if (warning) console.warn('Protokol query warning:', warning)
Log every warning in development
if (import.meta.env.DEV) {
const originalFetch = window.fetch
window.fetch = async (...args) => {
const res = await originalFetch(...args)
const warning = res.headers.get('Protokol-Query-Warning')
if (warning) console.warn('Protokol query warning:', warning, args[0])
return res
}
}
Warning codes
Each code below has a faster way to write the query.
| Code | Triggered by |
|---|---|
exists_operator |
$exists (true or false) anywhere in $adv, or in a $match inside $aggregate |
negation_operator |
$ne, $nin, $not |
unindexed_text_search |
A $regex that is case-insensitive or not anchored with ^; also, until a search index exists for the fields, the filter / filterOn text search |
javascript_operator |
$where, $function, $expr |
exists_operator
Don't test for field presence. Write an explicit value when you save, and filter on it. For example, save a boolean has_phone.
Before:
await platform.component('contacts').find(
{ currentPage: 1, perPage: 25 },
{ $adv: { 'phone': { $exists: true } } }
)
After:
await platform.component('contacts').find(
{ currentPage: 1, perPage: 25 },
{ $adv: { 'has_phone': true } }
)
Raw HTTP, before and after:
GET /v4/system/component/contacts/models?$adv={"phone":{"$exists":true}}
GET /v4/system/component/contacts/models?$adv={"has_phone":true}
negation_operator
Filter on the values you want, with $in or equality. For "not empty" on a text field use { "$gt": "" }.
Before:
After:
Raw HTTP, before and after:
GET /v4/system/component/orders/models?$adv={"status":{"$ne":"draft"}}
GET /v4/system/component/orders/models?$adv={"status":{"$in":["paid","shipped"]}}
unindexed_text_search
Short term: anchor the pattern with ^term and drop the i option where your data allows. Long term: declare a search index (planned, see Planned).
Until a search index exists for the fields, the filter / filterOn text search also gives this warning. You can't avoid it by changing the request. Keep using filter / filterOn for text search anyway.
Before:
After:
Raw HTTP, before and after:
GET /v4/system/component/contacts/models?$adv={"name":{"$regex":"smith","$options":"i"}}
GET /v4/system/component/contacts/models?$adv={"name":{"$regex":"^Smith"}}
javascript_operator
$where, $function and $expr are already rejected today in most paths. Rewrite them as plain field conditions.
Before:
After: store the result you need as a field, for example a boolean has_balance, and filter on it.
What doesn't change
- Results are never silently cut short. There is no automatic date window and no partial result. A query either returns full results or fails with an error.
sortBykeeps its current rules. See Sorting. Any single-value field can be sorted on. Arrays and objects are rejected, as today. Sorting a large component on a field that isn't declared won't be guaranteed fast, but it won't be rejected.limitstays at a maximum of 250. Cursor pagination is unchanged.
Recommended now
These are good practice today.
- Use cursor pagination (
next_cursor) instead of deepoffsetpaging for lists and exports. See cursor pagination. - Don't use
totalortotal_filteredas the loop end condition. Stop when a page comes back empty, or when there is nonext_cursor. - Send text search through
filterandfilterOn, not your own$regex.
Known issue
When a request sends both $adv and filter, the text search is ignored. Don't rely on combining them yet.
Planned
These are coming later. Details will be documented when they are available.
- Query shapes. Declare in component settings which filter and sort combinations your app uses. The platform keeps those fast.
- Search indexes. Declare which fields are text-searchable. Text search will then be fast, and a declared index will be required for text search.
Checklist for AI agents updating an app
Follow these steps in order.
- Find every call that uses v4 find: SDK imports from
@ptkl/sdk/beta, and URLs containing/v4/system/component/with/models. Ignore v3 code, the bare@ptkl/sdkimport,/aggregatecalls and uuid get/create/update/delete. - In those calls (and in any
$advor$aggregatevalue passed to them), grep for$exists. Replace each with a filter on an explicit value such as a booleanhas_x. Make sure the code that saves records writes that value, and backfill existing records if needed. - Grep for
$ne,$ninand$not. Replace each with$inor equality on the wanted values. For "not empty" on a text field use{ "$gt": "" }. - Grep for
$regex. For each, anchor with^and remove$options: "i"where the data allows. If the pattern is a user text search, switch tofilterandfilterOn. - Grep for
$where,$functionand$expr. Rewrite them as plain field conditions. - Find every request that sends both
$advandfilter. The text search is ignored there. Move the text condition into$advor drop the combination. - Find loops that page with
offset. Switch them tocursor. End the loop when a page is empty ornext_cursoris missing, not when a count fromtotalis reached. - Add a dev-only logger for
Protokol-Query-Warning(see How to see warnings). Run the app and fix every warning that appears. Warnings fromfilter/filterOntext search are expected for now. - Repeat step 8 until no other warnings appear.