Type utilities

A table definition gives you four static types and a set of codec methods. The types describe the same schema from different angles; the methods move values between the encoded and decoded sides.

Types

Import these from @schemic/core and apply them to a table or relation definition with typeof.

TypeSideShapeMeaning
App<typeof T>decoded (z.output)fullThe value you work with in TypeScript — Date, string, etc.
Wire<typeof T>encoded (z.input)fullA full row as SurrealDB stores and returns it.
Create<typeof T>decoded inputdefault fields optionalThe argument to encode — a new record.
Update<typeof T>decoded inputall fields optionalThe argument to encodePartial — a partial change.
TypeScript
import type { App, Create } from "@schemic/core";
import { User } from "./schema/user";

function render(user: App<typeof User>) { /* ... */ }
function makeUser(): Create<typeof User> {
  return { name: "Ada", email: "ada@example.com" }; // createdAt has a default
}

Also exported for advanced use: Shape, StandaloneDef, TableConfig, TableEvent, TableIndex, SurrealMeta, Expr.

Codec methods

These live on a table or relation definition.

MethodDirectionInputReturns
decode(row)wire → appa full rowApp<typeof T>
encode(input)app → wireCreate<typeof T>a full wire row
encodePartial(input)app → wireUpdate<typeof T>a partial wire payload

decode validates and converts a row you read; encode builds the payload for a CREATE/CONTENT; encodePartial builds a partial payload for a MERGE/UPDATE/PATCH.

Safe and async variants

Each method has a non-throwing safe* variant returning { success, data } or { success, error }, and an *Async variant for schemas with async refinements.

ThrowingNon-throwingAsync
decodesafeDecodedecodeAsync
encodesafeEncodeencodeAsync
encodePartialsafeEncodePartialencodePartialAsync

Field-level codecs

Any s.* field exposes .encode(value) and .decode(value) to convert a single value:

TypeScript
const createdAt = s.datetime();
createdAt.encode(new Date()); // Date -> DateTime
createdAt.decode(wireValue);  // DateTime -> Date

Where to go next