API Reference
Complete reference for all Zanith exports, types, and configuration.
Core functions
| Export | Description |
|---|---|
createZanith(config) | Create a typed database client. Returns TypedZanithClient. |
defineModel(factory) | Define a model with typed fields. Returns TypedModelDefinition. |
defineEnum(spec) | Define an enum. Returns EnumNode. |
compileSchema(models, enums) | Compile model definitions into a SchemaGraph. |
parseSchema(input) | Parse a Prisma-style .zanith file into SchemaAST. |
parseDSL(input) | Parse a .zn DSL file into SchemaAST. |
generateTypes(graph) | Generate TypeScript type declarations from a schema graph. |
applyPatch(graph, patch) | Incrementally patch a schema graph. Returns new graph. |
ModelAPI<T>
Every model gets a ModelAPI<T> instance with these methods.T is the field type map inferred from defineModel.
| Method | Returns | Description |
|---|---|---|
findMany(args?) | Promise | Find multiple records |
findFirst(args?) | Promise | Find first matching record |
findUnique(args) | Promise | Find by unique field |
create(args) | Promise | Insert a record |
update(args) | Promise | Update a record |
delete(args) | Promise | Delete a record |
count(args?) | Promise | Count matching records |
query() | RelationalQueryBuilder | Start a relational query |
insert(data) | InsertBuilder | Start an insert/upsert operation |
RelationalQueryBuilder
| Method | Description |
|---|---|
.with({ rel: true }) | Join a relation (LEFT JOIN by default) |
.with({ rel: 'inner' }) | INNER JOIN |
.with({ rel: { nested: true } }) | Multi-hop join |
.select(fn) | Explicit field projection with aliases |
.where(fn) | Filter with typed field references |
.orderBy(fn) | Sort by field references |
.limit(n) | Limit results |
.offset(n) | Skip results |
.distinctOn(fn) | PostgreSQL DISTINCT ON |
.withCTE(name, sql) | Define a CTE |
.fromCTE(name) | Select from a CTE |
.fromSubquery(sql, params, alias) | Select from a subquery |
.groupBy(fn) | GROUP BY fields |
.having(fn) | HAVING filter |
.toSQL() | Compile to { sql, params } without executing |
.execute() | Execute and return rows |
.getNullableFields() | Get field names that are nullable from LEFT JOINs |
InsertBuilder
| Method | Description |
|---|---|
.onConflict({ columns, action, set? }) | ON CONFLICT (upsert) |
.returning(cols) | Specify RETURNING columns (or '*') |
.execute() | Execute and return first row |
.executeMany() | Execute and return all rows (bulk insert) |
.toSQL() | Compile to { sql, params } |
Expression helpers
| Export | Usage |
|---|---|
and(...conditions) | Combine with AND |
or(...conditions) | Combine with OR |
not(condition) | Negate |
exists(subquery) | EXISTS subquery |
notExists(subquery) | NOT EXISTS subquery |
inSubquery(field, subquery) | field IN (subquery) |
Aggregate functions
| Export | SQL |
|---|---|
count(field?) | COUNT(*) or COUNT(field) |
countDistinct(field) | COUNT(DISTINCT field) |
sum(field) | SUM(field) |
avg(field) | AVG(field) |
min(field) | MIN(field) |
max(field) | MAX(field) |
Window functions
| Export | SQL |
|---|---|
rowNumber() | ROW_NUMBER() OVER (...) |
rank() | RANK() OVER (...) |
denseRank() | DENSE_RANK() OVER (...) |
sumOver(field) | SUM(field) OVER (...) |
avgOver(field) | AVG(field) OVER (...) |
countOver(field?) | COUNT(*) OVER (...) |
All window functions support .partitionBy(...fields) and .orderBy(...orders).
Type system exports
| Type | Purpose |
|---|---|
InferModelFields | Extract field type map from a TypedModelDefinition |
InferFieldTypes | Extract type map from a record of FieldBuilders |
InferFieldType | Extract T from FieldBuilder |
TypedWhereInput | Typed where input constrained to model fields |
FilterOpsFor | Map scalar type to its filter operators |
TypedZanithClient | Fully typed client mapped from model definitions |
Nullify | Make all values T | null (for LEFT JOIN) |
Error types
| Error | Cause |
|---|---|
ZanithError | Base error class |
ParseError | Schema parsing failure (line, column, file) |
ValidationError | Schema validation failure (missing @id, etc.) |
QueryError | SQL execution failure (includes sql string) |
ConnectionError | Database connection failure |
UniqueConstraintError | UNIQUE violation (constraint name, fields) |
ForeignKeyError | FK violation (constraint, detail) |
NotNullError | NOT NULL violation (column name) |
SerializationError | Serialization/deadlock failure (retryable) |
NotImplementedError | Feature not yet implemented |