zanith

API Reference

Complete reference for all Zanith exports, types, and configuration.

Core functions

ExportDescription
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.

MethodReturnsDescription
findMany(args?)PromiseFind multiple records
findFirst(args?)PromiseFind first matching record
findUnique(args)PromiseFind by unique field
create(args)PromiseInsert a record
update(args)PromiseUpdate a record
delete(args)PromiseDelete a record
count(args?)PromiseCount matching records
query()RelationalQueryBuilderStart a relational query
insert(data)InsertBuilderStart an insert/upsert operation

RelationalQueryBuilder

MethodDescription
.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

MethodDescription
.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

ExportUsage
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

ExportSQL
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

ExportSQL
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

TypePurpose
InferModelFieldsExtract field type map from a TypedModelDefinition
InferFieldTypesExtract type map from a record of FieldBuilders
InferFieldTypeExtract T from FieldBuilder
TypedWhereInputTyped where input constrained to model fields
FilterOpsForMap scalar type to its filter operators
TypedZanithClientFully typed client mapped from model definitions
NullifyMake all values T | null (for LEFT JOIN)

Error types

ErrorCause
ZanithErrorBase error class
ParseErrorSchema parsing failure (line, column, file)
ValidationErrorSchema validation failure (missing @id, etc.)
QueryErrorSQL execution failure (includes sql string)
ConnectionErrorDatabase connection failure
UniqueConstraintErrorUNIQUE violation (constraint name, fields)
ForeignKeyErrorFK violation (constraint, detail)
NotNullErrorNOT NULL violation (column name)
SerializationErrorSerialization/deadlock failure (retryable)
NotImplementedErrorFeature not yet implemented