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
| Shorthand | TypeScript type | Read (DB → model) | Write (model → DB) |
|---|---|---|---|
'string' / 'text' | string | as-is | as-is |
'integer' | number | parseInt() | parseInt() |
'float' | number | parseFloat() | parseFloat() |
'boolean' | boolean | coerces 0/1/"1"/"true" | writes 1 or 0 |
'datetime' | Carbon | constructs a Carbon instance | ISO 8601 string |
'date' | Date | constructs a native Date | ISO 8601 string |
'json' | unknown | JSON.parse() | JSON.stringify() |
'array' | unknown[] | JSON.parse() | JSON.stringify() |
'encrypted' | string | decrypts under APP_KEY | AES-256-GCM |
'encrypted:json' | unknown | decrypts, then JSON.parse() | stringify, encrypt |
Note —
@column("date")reads back a nativeDate, 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 it | Reads back | Once connections run in UTC |
|---|---|---|
DATETIME or text, written by the ORM | as saved | as saved |
DATETIME, filled by NOW() | 2 hours late | old rows 2 hours late, new correct |
TIMESTAMP, written by the ORM | as saved | old rows 2 hours early, new correct |
TIMESTAMP, filled by CURRENT_TIMESTAMP | 2 hours late | as 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
DATETIMEthatNOW()filled:SET col = CONVERT_TZ(col, '+02:00', '+00:00') - a
TIMESTAMPthe 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:00never happens inAmerica/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 withTZ=UTC. - The zero date. Bun 1.3 returns a MySQL zero date (
0000-00-00) as1970-01-01T00:00:00Zin every zone, and the ORM leaves it there; Bun 1.4 returns an invalid date, which a date cast refuses with aRangeError. 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:00inAmerica/New_York, so that time reads as1970-01-01T00:00:00Ztoo, five hours late.
Bun 1.3 also reads one PostgreSQL value differently:
- A
timestampread by a query with no bound values, the first time it runs. On Bun 1.3 under aTZthat is not UTC, a PostgreSQLtimestamp without time zonecolumn read by a query that binds no values, such asModel.query().get()with nowhereorDB.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 columntimestamptz, bind a value in the query, or run Bun 1.3 withTZ=UTC. Bun 1.4 reads it correctly. The columnstable.dateTime()andtable.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), adecimal:Ncolumn surfaces as a string, not a number. Type the property asstring.
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
Carbonis already immutable: each modifier such asadd()returns a new instance and never mutates the original. Soimmutable_datetimeanddatetimeproduce 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 throwsEncryptedColumnErrorrather 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 aVARCHAR(255)that held the value will not hold its ciphertext.migrate:generateandsynchronize()widen an encrypted column to TEXT for you — the generated migration saystable.text(...)— because MySQL outside strict mode truncates instead of failing, and a truncated payload never decrypts. - Add them to
hiddenif the model is serialized to a client. Decryption puts the real value back on the instance, andtoJSON()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/arrayOfis hydrated without invoking its constructor (viaObject.assignon the prototype). Define a staticfromJSON(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 castsand@column(), thestatic castsentry 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 /Castclass — when you need a custom transform, adecimal:N/enumoption, or a reusable cast shared by several models.static castsmap — 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
reactiveCastsper 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(), orupdate(). - 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:
| Member | Signature | Description |
|---|---|---|
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():
| Option | Type | Description |
|---|---|---|
cast | shorthand string, { get, set }, or CastContract | The transform applied on read/write. |
enumValues | Record<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
datetimecasts.