Skip to main content
zerotal

Casts & Mutators

Casts translate between the raw value stored in the database and the typed TypeScript value you work with on your model. Declare them inline on @column() or in a class-level static casts map; custom casts give you full control over both the read (get) and write (set) transforms.

Basic usage

The shorthand string passed to @column("…") is a cast alias. Each one resolves to a built-in get/set pair:

// app/models/Post.ts
import { Model, column, table } from "@zerotal/orm";
import { Carbon } from "zerotal/carbon";

@table("posts")
export class Post extends Model {
  @column("string") title!: string;
  @column("integer") views!: number;
  @column("float") score!: number;
  @column("boolean") published!: boolean;
  @column("datetime") publishedAt!: Carbon;
  @column("date") birthday?: Date;
  @column("json") meta!: Record<string, unknown>;
  @column("array") tags!: string[];
  @column("text") bio?: string;
}

Built-in cast shorthands

ShorthandTypeScript typeRead (DB → model)Write (model → DB)
'string' / 'text'stringas-isas-is
'integer'numberparseInt()parseInt()
'float'numberparseFloat()parseFloat()
'boolean'booleancoerces 0/1/"1"/"true"writes 1 or 0
'datetime'Carbonconstructs a Carbon instanceISO 8601 string
'date'Dateconstructs a native DateISO 8601 string
'json'unknownJSON.parse()JSON.stringify()
'array'unknown[]JSON.parse()JSON.stringify()
'encrypted'stringdecrypts under APP_KEYAES-256-GCM
'encrypted:json'unknowndecrypts, then JSON.parse()stringify, encrypt

Note — @column("date") reads back a native Date, while @column("datetime") reads back a Carbon instance. Type the property accordingly.

Dates are stored as UTC

Every date is written as UTC: ISO 8601 with a Z on SQLite and PostgreSQL, and YYYY-MM-DD HH:MM:SS on MySQL, whose DATETIME does not accept the T or the Z. A stored date-time with no zone of its own, such as that MySQL text or SQLite's CURRENT_TIMESTAMP, reads back as UTC, so a value comes back as the instant you saved whatever the server's TZ is.

A MySQL DATE, DATETIME or TIMESTAMP column reads back as the same instant on every supported Bun, and so does a PostgreSQL timestamp, with the exceptions in What Bun 1.3 cannot read back exactly. A PostgreSQL infinity cannot become a Carbon, so a datetime cast on one throws a RangeError that names the column; read such a column through a custom cast instead.

table.dateTime() and table.timestamps() create a text column on every database, so the two sections below matter for columns you declared with the database's own date types, and for values the database fills in itself.

The MySQL server's time zone

The ORM writes and reads UTC. MySQL uses the connection's time zone in two places the ORM cannot see: NOW() and CURRENT_TIMESTAMP give the wall-clock time in that zone, and a TIMESTAMP column is converted through it on the way in and on the way out.

Bun 1.4.1 and later open every MySQL connection with its time zone set to UTC. Bun 1.3 and 1.4.0 leave it at the server's default, and offer no way to set it on each pooled connection, so on those Buns the server itself has to run in UTC. bun zt doctor warns when the app's connection, or the server's default zone, is not UTC in January or in July, so a zone such as Europe/London, which is UTC only in winter, is caught in any season. On Bun 1.4.1 and later the connection is always UTC, and the warning is about the server: rows that Bun 1.3, Bun 1.4.0 or another client wrote through it still carry its zone.

On a server whose zone is +02:00, under Bun 1.3 or 1.4.0:

Column, and what wrote itReads backOnce connections run in UTC
DATETIME or text, written by the ORMas savedas saved
DATETIME, filled by NOW()2 hours lateold rows 2 hours late, new correct
TIMESTAMP, written by the ORMas savedold rows 2 hours early, new correct
TIMESTAMP, filled by CURRENT_TIMESTAMP2 hours lateas saved, old rows included

A comparison with NOW() in a whereRaw() is off by the same two hours. "Once connections run in UTC" is what changing the server's zone does, and also what moving the app to Bun 1.4.1 or later does on a server you leave as it is.

To run the server in UTC, set default-time-zone = '+00:00' under [mysqld], or run SET GLOBAL time_zone = '+00:00', and restart the app so its connections reopen. Rows written before the change keep the old zone. With connections in UTC, and '+02:00' standing for the old zone (a named zone needs MySQL's time zone tables loaded):

  • a DATETIME that NOW() filled: SET col = CONVERT_TZ(col, '+02:00', '+00:00')
  • a TIMESTAMP the ORM wrote: SET col = CONVERT_TZ(col, '+00:00', '+02:00')

Pick the rows each statement touches: a column usually holds only one of the two kinds, but one written both ways needs a WHERE that tells them apart.

What Bun 1.3 cannot read back exactly

Bun 1.3 decodes a MySQL DATE, DATETIME or TIMESTAMP as local time before the ORM sees it, and the ORM turns that back into the UTC value stored. Two values do not survive that trip:

  • A time in the process zone's daylight-saving gap. 2026-03-08 02:30:00 never happens in America/New_York, so Bun 1.3 moves it to 03:30 before the ORM can correct it, and it reads back an hour late. Bun 1.4 reads it correctly, and a zone with no daylight saving has no gap: run Bun 1.3 with TZ=UTC.
  • The zero date. Bun 1.3 returns a MySQL zero date (0000-00-00) as 1970-01-01T00:00:00Z in every zone, and the ORM leaves it there; Bun 1.4 returns an invalid date, which a date cast refuses with a RangeError. Under a zone that is not UTC, Bun 1.3 returns the same value for the stored time whose fields are the epoch in that zone, 1969-12-31 19:00:00 in America/New_York, so that time reads as 1970-01-01T00:00:00Z too, five hours late.

Bun 1.3 also reads one PostgreSQL value differently:

  • A timestamp read by a query with no bound values, the first time it runs. On Bun 1.3 under a TZ that is not UTC, a PostgreSQL timestamp without time zone column read by a query that binds no values, such as Model.query().get() with no where or DB.raw("SELECT …") with no bindings, can come back moved by the zone's offset the first time that query runs on a connection. Later runs on the same connection, and any query that binds a value, read it correctly. Declare the column timestamptz, bind a value in the query, or run Bun 1.3 with TZ=UTC. Bun 1.4 reads it correctly. The columns table.dateTime() and table.timestamps() create are text, and are not affected.

Scalars in a json column

json and array encode on write and parse on read, in both directions, so a value round-trips as the type you gave it — including a bare scalar:

setting.value = "62812345678"; // stored as "62812345678", read back as a string
setting.value = "051001"; // a branch code keeps its leading zero
setting.value = { plan: "pro" }; // objects and arrays as you would expect

This is worth stating because the obvious alternative is wrong. Skipping the encode for values that are already strings looks like it avoids double-encoding, but it makes the column hold bare characters — and JSON.parse("62812345678") is a number. The value's type would change between write and read, silently, for some values and not others.

If you are reading rows written by an older version that stored bare scalars, a value that was a numeric string may come back as a number; coerce on read where it matters.

This holds for the column table.json() creates, which stores JSON as text. A column you declare with the database's own JSON type (JSON on MySQL and MariaDB, jsonb on PostgreSQL) is parsed by the driver before the cast sees it, and a top-level string that is itself valid JSON, such as "62812345678", is then parsed a second time. Keep scalar strings in a table.json() column, or wrap them in an object.

Advanced cast options

decimal:N — fixed-precision number

Reads and writes the value as a string with exactly N decimal places. Useful for currency, where you want to avoid floating-point drift:

// app/models/Product.ts
@column({ type: "number", cast: "decimal:2" }) price!: string;
// DB stores "9.99" — the model reads it back as the string "9.99".

Note — Because both the read and write transforms call .toFixed(N), a decimal:N column surfaces as a string, not a number. Type the property as string.

immutable_datetime — datetime alias

Behaves like 'datetime' on read (constructs a Carbon) and serializes to an ISO 8601 string on write:

// app/models/Booking.ts
@column({ type: "datetime", cast: "immutable_datetime" }) lockedAt?: Carbon;

const tomorrow = booking.lockedAt?.add(1, "day"); // returns a new Carbon

Note — Every Carbon is already immutable: each modifier such as add() returns a new instance and never mutates the original. So immutable_datetime and datetime produce equivalent values — always assign the result of a modifier rather than relying on in-place mutation.

enum — TypeScript enums

Stores and retrieves the raw enum value (the underlying string or number); TypeScript narrows the property type. The cast itself is a pass-through, so pair it with enumValues to document the enum:

// app/models/Post.ts
enum Status {
  Draft     = "draft",
  Published = "published",
  Archived  = "archived",
}

@column({ type: "string", cast: "enum", enumValues: Status }) status!: Status;

// TypeScript now knows post.status is Status, not string:
if (post.status === Status.Published) { /* … */ }

encrypted — ciphertext at rest

The column stores an opaque AES-256-GCM payload keyed by APP_KEY; the property holds the value you assigned. Nothing in between — your code, validation, $dirty — has to know:

// app/models/Client.ts
@column("encrypted", { nullable: true }) idNumber?: string;

// Structured values need the :json variant, so the type round-trips:
@column("encrypted:json", { nullable: true }) medical?: MedicalInfo;

// The same thing spelled out. `encrypted` is a cast, not a storage type —
// `{ type: "encrypted" }` is not a thing:
@column({ type: "text", nullable: true, cast: "encrypted" }) passportNumber?: string;

The shorthand resolves to { type: "text", cast: "encrypted" }, which is why it is worth preferring: it gets the storage type right without you having to remember that ciphertext outgrows its plaintext.

For several columns at once, list them instead — it means exactly the same thing:

class Client extends BaseModel {
  static encryptable = ["idNumber", "passportNumber"];
}

A column in that list whose @column({ type }) is json encrypts as encrypted:json automatically, so the structure survives the round trip rather than reaching the cipher as "[object Object]".

Unlike hashable, this is reversible and does not touch the instance: after save(), client.idNumber still reads as the plaintext you set. $dirty therefore compares plaintext, and an unchanged column is not rewritten with a fresh IV on every unrelated save.

Danger — You cannot query an encrypted column. Every write draws a new IV, so the same value encrypts to different ciphertext each time and an equality match can never hit. where() on one throws EncryptedColumnError rather than quietly returning zero rows. If you need lookup, keep a separate hashed column (a blind index) beside it and query that. Sorting and grouping are meaningless for the same reason, and are not guarded.

Two more things worth knowing:

  • Declare the column as text. A payload is roughly 1.4× the plaintext plus 28 bytes, so a VARCHAR(255) that held the value will not hold its ciphertext. migrate:generate and synchronize() widen an encrypted column to TEXT for you — the generated migration says table.text(...) — because MySQL outside strict mode truncates instead of failing, and a truncated payload never decrypts.
  • Add them to hidden if the model is serialized to a client. Decryption puts the real value back on the instance, and toJSON() will include it.

Turning encryption on for a column that already holds data needs a back-fill first: existing plaintext rows are not decryptable, and reading one throws EncryptedColumnError naming the model and column. Read the rows with the cast off, then write them back with it on. The same error covers a rotated APP_KEY — decrypt with the old key and re-save. Failing the read is deliberate: handing back the ciphertext would put an unreadable value where the application expects a real one, and re-encrypt it on the next save, losing the original for good.

Custom cast — full get/set control

Pass an object with get and set functions for complete control over serialization:

// app/models/Place.ts
interface GeoPoint { lat: number; lng: number }

@column({
  type: "string",
  cast: {
    get: (v: unknown): GeoPoint => JSON.parse(v as string),
    set: (v: unknown): string  => JSON.stringify(v),
  },
})
location!: GeoPoint;

// You now work with a typed object, not a raw string:
console.log(place.location.lat, place.location.lng);

Reusable casts

For a cast you reuse across models, extend the Cast base class instead of repeating an inline { get, set } object. Put your cast in app/casts/ and pass an instance:

// app/casts/MoneyCast.ts
import { Cast } from "@zerotal/orm";

export class MoneyCast extends Cast<number> {
  get(db: unknown) {
    return Number(db) / 100;
  } // cents → dollars
  set(v: number) {
    return Math.round(v * 100);
  }
}
// app/models/Invoice.ts
import { column } from "@zerotal/orm";
import { MoneyCast } from "../casts/MoneyCast.ts";

@column({ cast: new MoneyCast() }) total!: number;

For JSON columns the ORM ships ready-made helpers that optionally hydrate the parsed value into a class:

// app/models/Customer.ts
import { column } from "@zerotal/orm";
import { json, objectOf, arrayOf } from "@zerotal/orm";
import { Address } from "../value-objects/Address.ts";

@column({ cast: json<Settings>() })   settings!: Settings;   // typed plain JSON
@column({ cast: objectOf(Address) })  billing!: Address;     // hydrate one object
@column({ cast: arrayOf(Address) })   addresses!: Address[]; // hydrate a list

Tip — A class passed to objectOf/arrayOf is hydrated without invoking its constructor (via Object.assign on the prototype). Define a static fromJSON(raw) on the class to customise how a row is rebuilt.

static casts map

An alternative to @column() for columns you don't declare directly (e.g. from an external schema, a view, or a generated table):

// app/models/Post.ts
@table("posts")
export class Post extends Model {
  static casts = {
    publishedAt: "datetime",
    meta: "json",
    price: "decimal:2",
    active: "boolean",
  } as const;
}

static casts and @column() can coexist. Casts are merged up the prototype chain, so a subclass inherits its parent's casts without re-declaring them.

Warning — When a column is declared in both static casts and @column(), the static casts entry wins — it is checked first during hydration. Pick one place to define a column's cast to avoid surprises.

Which should I use?

  • @column("…") shorthand — the default. Co-locates the cast with the property and gives you the TypeScript type in one place.
  • @column({ cast }) object / Cast class — when you need a custom transform, a decimal:N/enum option, or a reusable cast shared by several models.
  • static casts map — when the property isn't declared with @column() (external/generated schemas) or you want all casts listed in one table.

Reactive JSON casts

By default, mutating a nested JSON property directly (e.g. post.meta.views++) does not mark the column dirty and won't be persisted on the next save(). Enable reactiveCasts to make json and array columns use a reactive proxy that tracks deep mutations:

// app/models/Post.ts
@table("posts")
export class Post extends Model {
  static reactiveCasts = true;

  @column("json") meta!: Record<string, unknown>;
}

const post = await Post.find(1);

// With reactiveCasts = true, this nested mutation IS tracked:
post.meta.views = (post.meta.views as number) + 1;
await post.save(); // persists the updated meta

Without reactiveCasts, replace the whole value to ensure dirty tracking:

// in a controller
post.meta = { ...post.meta, views: (post.meta.views as number) + 1 };
await post.save();

Note — Enable reactiveCasts per model. There is no performance cost on models that don't use it.

Cast application order

Casts are applied:

  • On read — immediately after the row is hydrated from the database.
  • On write — just before the value is sent to the database in save(), create(), or update().
  • In dirty tracking — the hydrated (post-read-cast) value is captured as the original, so isDirty() reflects actual changes, not cast-representation differences.

References

Custom-cast surface, all exported from @zerotal/orm:

MemberSignatureDescription
Cast<T>abstract class Cast<T> { get(db); set(v) }Base class for a reusable custom cast.
CastContract<T>interface { get(db): T; set(v: T): unknown }The shape any { get, set } cast must satisfy.
json<T>(mapper?)(mapper?: CastMapper<T>) => JsonCast<T>Cast a JSON column to a typed object, optionally hydrated.
objectOf<T>(mapper?)(mapper?: CastMapper<T>) => JsonCast<T>Alias of json, reads nicely with a class.
arrayOf<T>(mapper?)(mapper?: CastMapper<T>) => ArrayCast<T>Cast a JSON column to an array of typed values.

Cast options accepted by @column():

OptionTypeDescription
castshorthand string, { get, set }, or CastContractThe transform applied on read/write.
enumValuesRecord<string, string | number>The TS enum object, paired with cast: "enum".

Types

CastField is what a cast declaration holds. EncryptedCastName names the encrypted cast variants, and isEncryptedCast says whether a given cast is one — which matters because an encrypted column cannot be queried by value, only by a blind index kept beside it.

Next steps

  • ORM — defining models and columns.
  • Queries — the query builder and scopes.
  • Serialization — control JSON output.
  • Carbon — the date type behind datetime casts.