Skip to content

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 SDK find(), 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/sdk import (it uses v3). v3 will not get these warnings. It keeps working, with a 15 second query time limit (408 with 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-Warning: exists_operator;field="phone", negation_operator;field="status"
  • Protokol-Query-Warning lists 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:

{ $adv: { 'status': { $nin: ['draft', 'cancelled'] } } }
{ $adv: { 'email': { $ne: '' } } }

After:

{ $adv: { 'status': { $in: ['paid', 'shipped'] } } }
{ $adv: { 'email': { $gt: '' } } }

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"]}}

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:

{ $adv: { 'name': { $regex: 'smith', $options: 'i' } } }

After:

{ $adv: { 'name': { $regex: '^Smith' } } }

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:

{ $adv: { $expr: { $gt: ['$total', '$paid'] } } }

After: store the result you need as a field, for example a boolean has_balance, and filter on it.

{ $adv: { 'has_balance': true } }

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.
  • sortBy keeps 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.
  • limit stays at a maximum of 250. Cursor pagination is unchanged.

These are good practice today.

  • Use cursor pagination (next_cursor) instead of deep offset paging for lists and exports. See cursor pagination.
  • Don't use total or total_filtered as the loop end condition. Stop when a page comes back empty, or when there is no next_cursor.
  • Send text search through filter and filterOn, 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.

  1. 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/sdk import, /aggregate calls and uuid get/create/update/delete.
  2. In those calls (and in any $adv or $aggregate value passed to them), grep for $exists. Replace each with a filter on an explicit value such as a boolean has_x. Make sure the code that saves records writes that value, and backfill existing records if needed.
  3. Grep for $ne, $nin and $not. Replace each with $in or equality on the wanted values. For "not empty" on a text field use { "$gt": "" }.
  4. Grep for $regex. For each, anchor with ^ and remove $options: "i" where the data allows. If the pattern is a user text search, switch to filter and filterOn.
  5. Grep for $where, $function and $expr. Rewrite them as plain field conditions.
  6. Find every request that sends both $adv and filter. The text search is ignored there. Move the text condition into $adv or drop the combination.
  7. Find loops that page with offset. Switch them to cursor. End the loop when a page is empty or next_cursor is missing, not when a count from total is reached.
  8. Add a dev-only logger for Protokol-Query-Warning (see How to see warnings). Run the app and fix every warning that appears. Warnings from filter / filterOn text search are expected for now.
  9. Repeat step 8 until no other warnings appear.