Vex · Query DSLOptional filters (
Optional filters (optional)
A condition wrapped in optional applies only when the request supplies its key. When the key is absent, the condition is removed before the SQL is compiled, so no caller passes a % wildcard to match every row. Example: one stored query lists every member, or only the members a search matches.
{
from: ['members'],
fields: ['members.id', 'members.name'],
filter: {
optional: {
key: 'q',
then: { ilike: ['members.name', { $context: 'q' }] },
},
},
sort: [{ field: 'members.name', dir: 'asc' }],
}SELECT m1.id AS "id", m1.name AS "name" FROM members AS m1 ORDER BY m1.name ASC LIMIT 100
SELECT m1.id AS "id", m1.name AS "name" FROM members AS m1 WHERE m1.name ILIKE $1 ORDER BY m1.name ASC LIMIT 100
[{ key: 'q', kind: 'context', type: 'string' }]Runs in this page on the installed package. The input and the expected output are defined by the example.