Skip to content
NordBaseDocs

Search the docs

Find a page or a section of the NordBase docs

Connect

The builder

A query builder familiar from Supabase that compiles to the same parameterized SQL, checks names and rows before sending and mixes freely with templates.

For everyday reads and writes on one table, the builder is shorter than SQL. It compiles to the same parameterized SQL as templates and sends it over the same connection. It is not a REST layer: nothing goes through an HTTP API.

Reading

ts
const orders = await db
  .from('orders')
  .select('id, total, status')
  .eq('customer_id', 1042)
  .order('created_at', { desc: true })
  .limit(20)

This sends one select statement, which counts as one Read. Names are quoted as identifiers and 1042 travels as a bind parameter, exactly as if you had written:

ts
const orders = await db.sql`
  select id, total, status from orders
  where customer_id = ${1042}
  order by created_at desc
  limit 20
`

Writing

ts
// Insert and return the new id
const [order] = await db.from('orders').insert({ customer_id: 1042, total: '3480.00', status: 'pending' }).returning('id')

// Update rows that match a filter
await db.from('orders').update({ status: 'paid' }).eq('id', order.id)

// Delete rows that match a filter
await db.from('orders').delete().eq('id', order.id)

// Insert, or update the row whose email already exists
await db.from('customers').upsert(rows, { onConflict: 'email' })

insert and upsert take one row or a list of rows. A list goes in one statement, so 500 rows inserted together count as one Write.

Filters

FilterMeaningExample
eqequal.eq('status', 'paid')
neqnot equal.neq('status', 'refunded')
gt, gtegreater than, or equal.gte('total', 100)
lt, lteless than, or equal.lt('created_at', cutoff)
inone of a list.in('id', [1042, 1043])
like, ilikepattern, case-sensitive or not.ilike('name', 'ana%')
isis null, true or false.is('deleted_at', null)
notthe opposite of another filter.not('deleted_at', 'is', null)
orany of several conditionssee below

Filters combine with and. or(...) takes builder conditions, never a string, so a value cannot change the shape of the condition. The exact form of or(...) in each library is coming soon.

Guards before sending

The builder refuses some queries before anything reaches the Database:

  • Unfiltered update and delete. update or delete without a filter is refused. If you mean every row, say so with .all():

    ts
    await db.from('carts').delete().all()
  • Unknown names. Table and column names are checked against the Database's schema, from your generated types or from a cached description of the live schema. A typo fails in your code, not in production data.

  • Rows that do not fit. Rows are checked against the table's types before they are sent. A wrong type or a missing required column is refused.

Validation with your own schema

Rows can also be checked against a schema you already use:

LanguageSchema library
NodeAny Standard Schema library, such as Zod or Valibot
PythonPydantic models
Rustserde types

nordbase gen types writes these from the live schema of your Database. It connects with NORDBASE_URL, like the libraries:

bash
npx nordbase gen types          # Node: Zod schemas
uv run nordbase gen types       # Python: Pydantic models

Run it again after every migration. How you attach your own schema to a table in each library is coming soon to the library pages.

Mixing the builder and templates

The builder and templates share one client and one connection pool, so you can use each where it reads best. Loops and conditions stay in your language.

Optional filters from a request:

ts
let query = db.from('orders').select('id, total, status, created_at').eq('customer_id', customerId)

if (status) query = query.eq('status', status)
if (minTotal) query = query.gte('total', minTotal)
if (since) query = query.gte('created_at', since)

const orders = await query.order('created_at', { desc: true }).limit(50)

A nightly script that updates per Region with the builder, then reports with SQL:

ts
for (const region of ['fi-hel', 'de-fsn']) {
  await db.from('carts').update({ status: 'stale' }).eq('region', region).lt('updated_at', cutoff)
}

const report = await db.sql`
  select region, count(*) as stale_carts
  from carts
  where status = ${'stale'}
  group by region
`

Joins, window functions and anything else the builder does not cover are plain SQL with templates, with the same protection.

Python and Rust

from, in, is, not and or are reserved words in Python, so the Python builder spells them with a trailing underscore, and a query runs with .execute():

python
orders = (
    db.from_("orders")
    .select("id, total, status")
    .eq("customer_id", 1042)
    .in_("status", ["pending", "paid"])
    .order("created_at", desc=True)
    .limit(20)
    .execute()
)

db.from_("customers").upsert(rows, on_conflict="email").execute()
rust
let orders = db
    .from("orders")
    .select("id, total, status")
    .eq("customer_id", 1042)
    .limit(20)
    .await?;

See the Node, Python and Rust pages for each library.

Never pass a request body straight in

update(input) and insert(input) write every key that is a real column. If input is a request body, a caller can set columns you never meant to expose, such as is_admin. Pick the fields you accept, or attach a schema that leaves protected columns out:

ts
await db.from('users').update({ name: body.name, bio: body.bio }).eq('id', me)

nordbase gen types leaves identity and generated columns out of the write schema. Empty or() and and() groups are refused, because they would make a filtered update or delete touch every row.