Skip to main content
zerotal

Documentation


Documentation / @zerotal/orm / index / column

Function: column()

Call Signature

column(): ColumnDecorator

Defined in: packages/orm/src/model/decorators/column.ts:243

Map a model property to a database column.

Returns

ColumnDecorator

A class-field decorator that registers the property as a column.

Remarks

Applied to a field of a BaseModel subclass, @column registers that property in the ORM's column metadata (columnRegistry), so it participates in hydration, dirty-tracking, persistence, and schema generation / auto-migration.

The argument may be omitted (defaults to a "string" column), given as a ColumnShorthand string, or given as a full ColumnOptions object. Options control the storage type, primary key, nullable, default, and — most importantly — the cast that serializes the value between DB and model (json/array, datetime/date, boolean, integer/float, decimal:n, enum, or a custom { get, set } / CastContract). A declared cast is also mirrored onto the class's static casts map.

Timestamp and primary-key conventions themselves are configured on the class via @table (see table); @column only maps individual fields.

Registration is anchored at class-definition time by the @table decorator (a Bun 1.3.x standard-decorator workaround), so a model that declares columns must also carry @table — or be auto-discovered from app/models/.

Example

@table("users")
export class User extends BaseModel {
  @column({ primary: true }) id!: number;

  // No args — defaults to a string column
  @column() name!: string;

  // String shorthand
  @column("integer") age!: number;
  @column("datetime") createdAt!: Carbon;

  // Cast a JSON column to/from an array (reactive when `static reactiveCasts`)
  @column({ type: "json", cast: "array", default: [] }) roles!: string[];

  // Nullable with a custom cast object
  @column({ nullable: true, cast: { get: (v) => v && new URL(String(v)), set: (u) => u?.href } })
  website?: URL | null;
}

Call Signature

column(type): ColumnDecorator

Defined in: packages/orm/src/model/decorators/column.ts:244

Map a model property to a database column.

Parameters

type

ColumnShorthand

Returns

ColumnDecorator

A class-field decorator that registers the property as a column.

Remarks

Applied to a field of a BaseModel subclass, @column registers that property in the ORM's column metadata (columnRegistry), so it participates in hydration, dirty-tracking, persistence, and schema generation / auto-migration.

The argument may be omitted (defaults to a "string" column), given as a ColumnShorthand string, or given as a full ColumnOptions object. Options control the storage type, primary key, nullable, default, and — most importantly — the cast that serializes the value between DB and model (json/array, datetime/date, boolean, integer/float, decimal:n, enum, or a custom { get, set } / CastContract). A declared cast is also mirrored onto the class's static casts map.

Timestamp and primary-key conventions themselves are configured on the class via @table (see table); @column only maps individual fields.

Registration is anchored at class-definition time by the @table decorator (a Bun 1.3.x standard-decorator workaround), so a model that declares columns must also carry @table — or be auto-discovered from app/models/.

Example

@table("users")
export class User extends BaseModel {
  @column({ primary: true }) id!: number;

  // No args — defaults to a string column
  @column() name!: string;

  // String shorthand
  @column("integer") age!: number;
  @column("datetime") createdAt!: Carbon;

  // Cast a JSON column to/from an array (reactive when `static reactiveCasts`)
  @column({ type: "json", cast: "array", default: [] }) roles!: string[];

  // Nullable with a custom cast object
  @column({ nullable: true, cast: { get: (v) => v && new URL(String(v)), set: (u) => u?.href } })
  website?: URL | null;
}

Call Signature

column(options): ColumnDecorator

Defined in: packages/orm/src/model/decorators/column.ts:245

Map a model property to a database column.

Parameters

options

ColumnOptions

Returns

ColumnDecorator

A class-field decorator that registers the property as a column.

Remarks

Applied to a field of a BaseModel subclass, @column registers that property in the ORM's column metadata (columnRegistry), so it participates in hydration, dirty-tracking, persistence, and schema generation / auto-migration.

The argument may be omitted (defaults to a "string" column), given as a ColumnShorthand string, or given as a full ColumnOptions object. Options control the storage type, primary key, nullable, default, and — most importantly — the cast that serializes the value between DB and model (json/array, datetime/date, boolean, integer/float, decimal:n, enum, or a custom { get, set } / CastContract). A declared cast is also mirrored onto the class's static casts map.

Timestamp and primary-key conventions themselves are configured on the class via @table (see table); @column only maps individual fields.

Registration is anchored at class-definition time by the @table decorator (a Bun 1.3.x standard-decorator workaround), so a model that declares columns must also carry @table — or be auto-discovered from app/models/.

Example

@table("users")
export class User extends BaseModel {
  @column({ primary: true }) id!: number;

  // No args — defaults to a string column
  @column() name!: string;

  // String shorthand
  @column("integer") age!: number;
  @column("datetime") createdAt!: Carbon;

  // Cast a JSON column to/from an array (reactive when `static reactiveCasts`)
  @column({ type: "json", cast: "array", default: [] }) roles!: string[];

  // Nullable with a custom cast object
  @column({ nullable: true, cast: { get: (v) => v && new URL(String(v)), set: (u) => u?.href } })
  website?: URL | null;
}