Definers

The definers are the entry points you import from @schemic/surrealdb. Each returns an immutable, chainable definition. This page lists their signatures and methods. For the codec methods (encode, decode, encodePartial) see type utilities and the encode/decode guide.

defineTable

TypeScript
defineTable(name: string, fields: Shape): TableDef

Creates a table definition. fields is an object of s.* schemas. The id field sets the record-id type and is otherwise implicit (no DEFINE FIELD id).

MethodDescription
.schemafull()Only defined fields are allowed (the default).
.schemaless()Allow arbitrary fields; defined fields are still validated.
.typeAny()TYPE ANY table.
.drop(drop = true)Mark the table DROP (writes are discarded).
.comment(text)Attach a table COMMENT.
.permissions(spec)Table-level PERMISSIONS for select/create/update/delete.
.index(name, fields, opts?)A composite index. opts: { unique?, count? }.
.event(name, { when?, then })A row-change event.
.pick(...keys) / .omit(...keys)A derived table with a subset of fields.
.partial()A derived table with every field optional.
.extend({ ... })A derived table with extra fields.
.record()The record<...> link type for this table.
.encode / .decode / .encodePartialCodec methods — see type utilities.

defineRelation

TypeScript
defineRelation(name: string, fields?: Shape): RelationDef

Creates an edge (relation) table. It has every TableDef method plus the endpoint setters; in and out are implicit.

MethodDescription
.from(table)Set the FROM endpoint.
.to(table)Set the TO endpoint.

.from(User).to(Post) emits TYPE RELATION FROM user TO post. Omit both to leave the relation unrestricted.

defineFunction

TypeScript
defineFunction(name: string, args?: Shape): FunctionDef

Declares a custom function. args is an object of named s.* schemas; they infer to SurrealQL types like fields do.

MethodDescription
.returns(type)Declare the return type (an s schema).
.body(expr)The function body — a surql block or raw string. Required to emit.
.permissions(p)FULL (true, default), NONE (false), or a surql condition.
.comment(text)Attach a COMMENT.

Emits DEFINE FUNCTION fn::<name>(<args>) -> <returns> { <body> }.

defineAccess

TypeScript
defineAccess(name: string): UnscopedAccessDef

Declares an access method. You choose a scope first — .onDatabase() or .onNamespace(), compile-enforced — then the kind:

MethodDescription
.onDatabase()Scope the access to the database. Required before a kind (or use .onNamespace()).
.onNamespace()Scope the access to the namespace — JWT/bearer only; .record() is database scope only.
.record()A RECORD access (SIGNUP/SIGNIN bodies). Database scope only.
.jwt({ alg?, key?, url? })Validate external JWTs, by key or by JWKS url.
.bearer({ for })Bearer API-key grants; for is "record" or "user".
TypeScript
import { defineAccess } from "@schemic/surrealdb";

export const Account = defineAccess("account").onDatabase().record();

defineEvent

TypeScript
defineEvent(table: TableDef | string, name: string, { when?, then }): EventDef

Declares an event as a standalone object. Equivalent to the table’s .event(name, spec) method; use this form when you keep events in their own file. The body sees $before, $after, $event, and $value.

defineView

TypeScript
defineView(name: string, shape?: Shape): ViewBuilder

Defines a view — a read-only table computed from a query. defineView returns a builder; pass the surql SELECT to .as(query), which returns the TableDef. The optional second argument is a shape that types the projected rows. Schemic emits DEFINE TABLE <name> TYPE ANY SCHEMALESS AS <query>, and you read it like any table.

TypeScript
import { defineView, surql } from "@schemic/surrealdb";

export const ActiveUser = defineView("active_user").as(
  surql`SELECT id, name FROM user WHERE active = true`,
);

defineAnalyzer

TypeScript
defineAnalyzer(name: string): AnalyzerDef

Declares a full-text search analyzer, configured with a fluent chain. Emits DEFINE ANALYZER <name> TOKENIZERS <...> FILTERS <...>. Reference the analyzer from a search index on a field.

MethodDescription
.tokenizers(...names)The tokenizers, e.g. "class", "blank", "camel", "punct".
.filters(...names)The token filters, e.g. "lowercase", "ascii", "snowball(english)". Also accepts a f => [...] callback.
TypeScript
import { defineAnalyzer } from "@schemic/surrealdb";

export const Standard = defineAnalyzer("standard")
  .tokenizers("class")
  .filters("lowercase", "ascii");

Where to go next