zite client
from zitejs/db. It’s generated into .zite/db.ts at the workspace root from the database’s schema, so it
knows your tables and fields. To persist data, use these tables: a request’s context has no storage, db,
or cache.
zite.* is server-only. Use it inside a server function’s handler, a *.server.ts helper, or an
automation, never at a route file’s top level or in a component. Pages reach data by calling
server functions.
Names
Read.zite/db.ts before writing code against the database: it has the table types and the methods each
table client has. Each table is a property on zite, named by its SDK name in camelCase (zite.ballRuns).
Fields are camelCase SDK names too (runLabel). SQL takes a different name for the table, in PascalCase
("BallRuns"). See SQL.
Table methods
Writes always go through these methods, never through SQL.
findAll returns 500 rows by default
findAll returns 500 records unless you pass limit, and it does so silently: hasMore is the only sign
there were more. limit goes up to 2000, and more than that is rejected with a 400.
So an aggregate computed by pulling rows and reducing them in JavaScript quietly produces wrong numbers once
a table grows past that. SQL aggregates in Postgres see every row, whatever the table’s size. Reach for
findAll only when you need a small, paginated list of full records to display, and use
zite.sql() for everything else.
Filters
Filters are keyed by field SDK name. A bare value means equality, and an object applies an operator:
Zite tables have no
ilike: a filter using it matches no records. For a case-insensitive match, filter with
contains and keep the record whose value, lowercased, equals what you’re looking for, lowercased.
User and linked-record fields
- To record who a row belongs to, put
context.user.idin auserfield. - To filter records by a user field, pass the user’s id directly:
filters: { createdBy: context.user.id }. The SDK handles the array matching. - A
useror linked-record field reads back as an array of ids, so test it with[row.owner ?? []].flat().includes(context.user.id), never===.
bulkCreate
When creating multiple records, usebulkCreate instead of calling create in a loop. Individual calls are
slower and may hit rate limits:
bulkCreate takes at most 100 records per call. More is rejected with a 400, and nothing chunks for
you, so slice anything unbounded into batches of 100:
Upserts with matchOn
bulkCreate is also the upsert. With matchOn, it updates the rows that already match on those fields
(only the fields you pass change) and inserts the rest:
findOne followed by a create costs two round trips, and can insert twice when two calls land at once.
A database trigger sees a row an upsert inserts as created, a row it
matched as updated, and a matched row it left as it was not at all.
Raw SQL
Default to raw SQL throughzite.sql() for anything that aggregates, joins, or summarizes data:
dashboards, KPIs, reports, totals, counts, group-bys, top-N lists, anything that touches more than one table
together.
zite.sql() resolves to { rows, columns, rowCount, truncated }. Result columns come back keyed by each
field’s SDK name (rows[0].orderPlacedAt), and expressions by their alias.
zite.sql() is read-only: it accepts SELECT statements only, and anything else is rejected at runtime.
Writes go through the table methods (create, update, delete, bulkCreate).
Identifier rules
A wrong name in SQL is a runtime 500, which type-checking can’t catch:- Use the SDK names from
.zite/db.ts, never display names. The table’s accessor in code (zite.ballRuns) is a different name from the one SQL takes ("BallRuns"). - Double-quote every table and field identifier: Postgres lowercases unquoted ones.
- The
.zite/db.tsheader lists the workspace’s link tables. - Pass runtime values through
params($1,$2, …), never string interpolation. - Soft-deleted rows are excluded automatically.
truncated: true. See Dashboards and reports for paging, linked records,
dates, select and user fields, and worked dashboards.
Try a server function’s SQL with
zite.sql() in run_one_off_script, which takes SDK names as server
functions do. The execute_sql data tool takes display names ("Amount Paid") rather than SDK names
(amountPaid), so it fails correct server-function SQL with “column does not exist”.
run_one_off_script runs against the live database, so anything it writes is real data.The app’s users
The client also lists the app’s users and updates their profiles, withzite.auth.findAllUsers and
zite.auth.updateUserProfile. See Access and sign-in.