Filter operators
The filter methods of the query builder, the REST operator each one sends, what PostgreSQL does with it, and the operators the REST API accepts without an SDK method.
How filters combine#
- Each filter method adds one condition
column=operator.valueto the query string. All conditions must be true (and). - Filters apply to reads (
select), updates, deletes and to the rows returned byrpc(). - Values are sent as text and converted by PostgreSQL to the type of the column:
eq('done', false)sendsdone=eq.false. - The SDK quotes a value that contains
,,(,)or"inside a list (in.(Water,"Aix, FR")). Over REST, encode the query string (curl --get --data-urlencode). - Filters run after row level security: a filter never reveals a row that a policy hides.
Operators#
| SDK method | REST | PostgreSQL | Notes |
|---|---|---|---|
eq(column, value) | column=eq.value | = | |
neq(column, value) | column=neq.value | <> | Rows where the column is null do not match. |
gt(column, value) | column=gt.value | > | |
gte(column, value) | column=gte.value | >= | |
lt(column, value) | column=lt.value | < | |
lte(column, value) | column=lte.value | <= | |
like(column, pattern) | column=like.pattern | LIKE | % any text, _ one character. Over REST, * can replace %. |
ilike(column, pattern) | column=ilike.pattern | ILIKE | Case-insensitive LIKE. |
likeAnyOf(column, patterns) | column=like(any).{p1,p2} | LIKE ANY | At least one pattern. |
likeAllOf(column, patterns) | column=like(all).{p1,p2} | LIKE ALL | Every pattern. |
ilikeAnyOf(column, patterns) | column=ilike(any).{p1,p2} | ILIKE ANY | |
ilikeAllOf(column, patterns) | column=ilike(all).{p1,p2} | ILIKE ALL | |
in(column, values) | column=in.(v1,v2) | IN | |
notIn(column, values) | column=not.in.(v1,v2) | NOT IN | |
is(column, 'null') | column=is.null | IS NULL | Also 'true', 'false', and 'unknown' on a boolean column. |
is(column, 'not.null') | column=not.is.null | IS NOT NULL | Not neq(column, null): it sends neq.null, which fails on a number column and compares with the text 'null' on a text column. |
isDistinct(column, value) | column=isdistinct.value | IS DISTINCT FROM | Treats null as a value: null rows match. |
not(column, op, value) | column=not.op.value | NOT | Negates any operator. |
or(filters) | or=(c1.op.v1,c2.op.v2) | OR | Takes an array of { column, op, value }. |
match(object) | c1=eq.v1&c2=eq.v2 | = on each key | |
filter(column, op, value) | column=op.value | any | Raw REST operator, for operators without a method. |
contains(column, value) | column=cs.{…} or cs.{"k":"v"} | @> | Arrays, jsonb, ranges. |
containedBy(column, value) | column=cd.{…} | <@ | |
overlaps(column, value) | column=ov.{…} | && | Arrays and ranges. |
rangeGt(column, range) | column=sr.range | >> | Strictly right of. |
rangeGte(column, range) | column=nxl.range | &> | Does not extend to the left of. |
rangeLt(column, range) | column=sl.range | << | Strictly left of. |
rangeLte(column, range) | column=nxr.range | &< | Does not extend to the right of. |
rangeAdjacent(column, range) | column=adj.range | -|- | |
textSearch(column, query, options) | column=fts.query, plfts, phfts, wfts | @@ | See Full-text search. |
| (no method) | column=match.regex, imatch.regex | ~, ~* | Use filter(column, 'match', regex). |
| (no method) | column=eq(any).{v1,v2}, gt(all).{…} | = ANY, > ALL | Modifiers on any comparison. Over REST only. |
| (no method) | and=(c1.op.v1,c2.op.v2), or=(a.eq.1,and(b.gt.1,c.lt.2)), not.or=(…) | AND, nested groups | Over REST only; the SDK or() takes one flat group. |
A JSON key can be filtered by writing its path as the column: eq('attrs->>color', 'red'). See Query JSON columns.
Example table#
The examples read a table of four rows with a nullable number, a text array, a jsonb column and an integer range. Create it on a linked project with the service_role key in AURABASE_SERVICE_ROLE_KEY:
create table qd_ops_items (
id int primary key,
name text not null,
score int,
tags text[] not null default '{}',
attrs jsonb not null default '{}',
slot int4range
);
insert into qd_ops_items values
(1, 'Apple pie', 10, '{fruit,dessert}', '{"color": "red", "size": 2}', '[1,5)'),
(2, 'apple juice', null, '{fruit,drink}', '{"color": "green"}', '[5,8)'),
(3, 'Carrot cake', 7, '{vegetable,dessert}', '{"color": "orange", "size": 3}', '[10,20)'),
(4, 'Water', 0, '{drink}', '{}', null); ℹ No results (0 rows).Each example below prints the operator, the query string the SDK sends (decoded) and the ids of the matching rows.
Comparison#
import { createClient } from '@aurabase/aurabase-js'
const admin = createClient(process.env.AURABASE_URL!, process.env.AURABASE_SERVICE_ROLE_KEY!)
const q = () => admin.from('qd_ops_items').select('id').order('id')
const show = async (label: string, b: any) => {
const { data, error } = await b
console.log(label.padEnd(6), decodeURIComponent(b.buildQueryString()).padEnd(38), error ? `${error.status} ${error.code}` : data.map((r: any) => r.id).join(','))
}
await show('eq', q().eq('name', 'Water'))
await show('neq', q().neq('score', 0))
await show('gt', q().gt('score', 5))
await show('gte', q().gte('score', 7))
await show('lt', q().lt('score', 7))
await show('lte', q().lte('score', 7))
await show('match', q().match({ name: 'Water', score: 0 }))eq select=id&name=eq.Water&order=id.asc 4
neq select=id&score=neq.0&order=id.asc 1,3
gt select=id&score=gt.5&order=id.asc 1,3
gte select=id&score=gte.7&order=id.asc 1,3
lt select=id&score=lt.7&order=id.asc 4
lte select=id&score=lte.7&order=id.asc 3,4
match select=id&name=eq.Water&score=eq.0&order=id.asc 4Row 2 has a null score: it matches none of the comparisons, not even neq. Use isDistinct or or with is.null to include it.
Patterns#
import { createClient } from '@aurabase/aurabase-js'
const admin = createClient(process.env.AURABASE_URL!, process.env.AURABASE_SERVICE_ROLE_KEY!)
const q = () => admin.from('qd_ops_items').select('id').order('id')
const show = async (label: string, b: any) => {
const { data, error } = await b
console.log(label.padEnd(11), decodeURIComponent(b.buildQueryString()).padEnd(55), error ? `${error.status} ${error.code}` : data.map((r: any) => r.id).join(','))
}
await show('like', q().like('name', '%pie'))
await show('ilike', q().ilike('name', 'apple%'))
await show('likeAnyOf', q().likeAnyOf('name', ['Apple%', 'Water']))
await show('likeAllOf', q().likeAllOf('name', ['%a%', '%e%']))
await show('ilikeAnyOf', q().ilikeAnyOf('name', ['apple%', 'water']))
await show('ilikeAllOf', q().ilikeAllOf('name', ['%APPLE%', '%PIE']))
await show('imatch', q().filter('name', 'imatch', '^apple'))like select=id&name=like.%pie&order=id.asc 1
ilike select=id&name=ilike.apple%&order=id.asc 1,2
likeAnyOf select=id&name=like(any).{Apple%,Water}&order=id.asc 1,4
likeAllOf select=id&name=like(all).{%a%,%e%}&order=id.asc 2,3,4
ilikeAnyOf select=id&name=ilike(any).{apple%,water}&order=id.asc 1,2,4
ilikeAllOf select=id&name=ilike(all).{%APPLE%,%PIE}&order=id.asc 1
imatch select=id&name=imatch.^apple&order=id.asc 1,2A pattern that starts with % cannot use a B-tree index; on a large table, add a trigram index (Full-text search).
Lists and null#
import { createClient } from '@aurabase/aurabase-js'
const admin = createClient(process.env.AURABASE_URL!, process.env.AURABASE_SERVICE_ROLE_KEY!)
const q = () => admin.from('qd_ops_items').select('id').order('id')
const show = async (label: string, b: any) => {
const { data, error } = await b
console.log(label.padEnd(11), decodeURIComponent(b.buildQueryString()).padEnd(48), error ? `${error.status} ${error.code} ${error.message}` : data.map((r: any) => r.id).join(','))
}
await show('in', q().in('id', [1, 3]))
await show('in quoted', q().in('name', ['Water', 'Aix, FR']))
await show('notIn', q().notIn('id', [1, 3]))
await show('is null', q().is('score', 'null'))
await show('not null', q().is('score', 'not.null'))
await show('isDistinct', q().isDistinct('score', 0))
await show('neq null', q().neq('score', null as any))
await show('neq text', q().neq('name', null as any))in select=id&id=in.(1,3)&order=id.asc 1,3
in quoted select=id&name=in.(Water,"Aix,+FR")&order=id.asc 4
notIn select=id&id=not.in.(1,3)&order=id.asc 2,4
is null select=id&score=is.null&order=id.asc 2
not null select=id&score=not.is.null&order=id.asc 1,3,4
isDistinct select=id&score=isdistinct.0&order=id.asc 1,2,3
neq null select=id&score=neq.null&order=id.asc 400 INVALID_INPUT invalid input syntax for type integer: "null"
neq text select=id&name=neq.null&order=id.asc 1,2,3,4Negation and or#
import { createClient } from '@aurabase/aurabase-js'
const admin = createClient(process.env.AURABASE_URL!, process.env.AURABASE_SERVICE_ROLE_KEY!)
const q = () => admin.from('qd_ops_items').select('id').order('id')
const show = async (label: string, b: any) => {
const { data, error } = await b
console.log(label.padEnd(8), decodeURIComponent(b.buildQueryString()).padEnd(52), error ? `${error.status} ${error.code}` : data.map((r: any) => r.id).join(','))
}
await show('not', q().not('name', 'ilike', 'apple%'))
await show('or', q().or([{ column: 'score', op: 'gt', value: 8 }, { column: 'name', op: 'eq', value: 'Water' }]))
await show('or null', q().or([{ column: 'score', op: 'lt', value: 5 }, { column: 'score', op: 'is', value: 'null' }]))
await show('chained', q().gte('score', 0).lte('score', 9))not select=id&name=not.ilike.apple%&order=id.asc 3,4
or select=id&or=(score.gt.8,name.eq.Water)&order=id.asc 1,4
or null select=id&or=(score.lt.5,score.is.null)&order=id.asc 2,4
chained select=id&score=gte.0&score=lte.9&order=id.asc 3,4Nested groups are written over REST:
for f in 'and=(score.gte.1,score.lte.9)' 'or=(id.eq.4,and(score.gte.5,tags.cs.{dessert}))' 'not.or=(id.eq.1,id.eq.2)' 'score=eq(any).{0,7}'; do
printf '%-48s ' "$f"
curl -s --get "$AURABASE_URL/v1/db/qd_ops_items" -H "apikey: $AURABASE_SERVICE_ROLE_KEY" \
--data-urlencode 'select=id' --data-urlencode 'order=id' --data-urlencode "$f" \
| node -e 'console.log(JSON.parse(require("fs").readFileSync(0, "utf8")).data.map((r) => r.id).join(","))'
doneand=(score.gte.1,score.lte.9) 3
or=(id.eq.4,and(score.gte.5,tags.cs.{dessert})) 1,3,4
not.or=(id.eq.1,id.eq.2) 3,4
score=eq(any).{0,7} 3,4Arrays and JSON#
import { createClient } from '@aurabase/aurabase-js'
const admin = createClient(process.env.AURABASE_URL!, process.env.AURABASE_SERVICE_ROLE_KEY!)
const q = () => admin.from('qd_ops_items').select('id').order('id')
const show = async (label: string, b: any) => {
const { data, error } = await b
console.log(label.padEnd(14), decodeURIComponent(b.buildQueryString()).padEnd(46), error ? `${error.status} ${error.code}` : data.map((r: any) => r.id).join(','))
}
await show('contains', q().contains('tags', ['fruit']))
await show('containedBy', q().containedBy('tags', ['fruit', 'drink']))
await show('overlaps', q().overlaps('tags', ['drink', 'vegetable']))
await show('contains json', q().contains('attrs', { color: 'red' }))
await show('json key', q().eq('attrs->>color' as any, 'red'))contains select=id&tags=cs.{fruit}&order=id.asc 1,2
containedBy select=id&tags=cd.{fruit,drink}&order=id.asc 2,4
overlaps select=id&tags=ov.{drink,vegetable}&order=id.asc 2,3,4
contains json select=id&attrs=cs.{"color":"red"}&order=id.asc 1
json key select=id&attrs->>color=eq.red&order=id.asc 1An array value is sent as {a,b}, an object as JSON. containedBy on an array matches rows whose every element is in the list, including an empty array.
Ranges#
Range values are PostgreSQL range literals: [ and ] include the bound, ( and ) exclude it.
import { createClient } from '@aurabase/aurabase-js'
const admin = createClient(process.env.AURABASE_URL!, process.env.AURABASE_SERVICE_ROLE_KEY!)
const q = () => admin.from('qd_ops_items').select('id').order('id')
const show = async (label: string, b: any) => {
const { data, error } = await b
console.log(label.padEnd(14), decodeURIComponent(b.buildQueryString()).padEnd(38), error ? `${error.status} ${error.code}` : data.map((r: any) => r.id).join(','))
}
await show('rangeGt', q().rangeGt('slot', '[1,5)'))
await show('rangeGte', q().rangeGte('slot', '[5,6)'))
await show('rangeLt', q().rangeLt('slot', '[5,8)'))
await show('rangeLte', q().rangeLte('slot', '[1,8)'))
await show('rangeAdjacent', q().rangeAdjacent('slot', '[5,8)'))
await show('contains', q().contains('slot', '[6,7]'))rangeGt select=id&slot=sr.[1,5)&order=id.asc 2,3
rangeGte select=id&slot=nxl.[5,6)&order=id.asc 2,3
rangeLt select=id&slot=sl.[5,8)&order=id.asc 1
rangeLte select=id&slot=nxr.[1,8)&order=id.asc 1,2
rangeAdjacent select=id&slot=adj.[5,8)&order=id.asc 1
contains select=id&slot=cs.[6,7]&order=id.asc 2contains on a range takes a range: contains('slot', 6) fails with 400 INVALID_INPUT malformed range literal: "6"; write '[6,6]'.
Raw operators#
filter(column, operator, value) sends any REST operator as is, and not(column, operator, value) negates it. Use them for operators that have no method (match, imatch, fts variants with options) and keep the dedicated methods otherwise: they are typed on your columns when you use generated types.
Errors#
| Message | Cause |
|---|---|
400 COLUMN_NOT_FOUND column qd_ops_items.nope does not exist | Unknown column in a filter. |
400 INVALID_INPUT invalid input syntax for type integer: "…" | A value that does not convert to the column type, such as neq(column, null) on a number column. |
400 INVALID_INPUT malformed range literal: "…" | A range operator with a value that is not a range. |
400 POSTGREST_ERROR "failed to parse filter (…)" | Unknown operator in filter() or in the query string. |
400 POSTGREST_ERROR argument of IS UNKNOWN must be type boolean, not type integer | is(column, 'unknown') on a non-boolean column. |
400 BAD_REQUEST whose message says the key is reserved by PostgREST | A filter on a column named select, order, limit, offset, on_conflict, columns, and or or. Rename the column. |
Clean up#
aura db raw "drop table qd_ops_items" ℹ No results (0 rows).