Skip to main content

Models

A model is a class that extends QueryRow, decorated with @Table. Each column is a decorated property.

import {Column, ColumnOption, QueryRow, Table} from '@fca.gg/orm';

@Table('users')
export class User extends QueryRow {
@Column.Increment()
@ColumnOption.Primary()
@ColumnOption.NotNullable()
@ColumnOption.Unsigned()
private id!: number;

@Column.String(255)
@ColumnOption.NotNullable()
private name!: string;

@Column.String(255)
@ColumnOption.Unique()
@ColumnOption.NotNullable()
private email!: string;

@Column.Boolean()
@ColumnOption.NotNullable()
@ColumnOption.DefaultTo(true)
private active!: boolean;

public getId(): number {
return this.id;
}

public getName(): string {
return this.name;
}

public setName(name: string): this {
this.name = name;
return this;
}

public setEmail(email: string): this {
this.email = email;
return this;
}
}

getId() is required​

update(), delete() and createOrUpdate() identify the row with getId(). The base method throws: every model must implement it. The ID column is named id.

Accessors​

Columns are usually private, read and changed through accessors. orm accessor generates them for you, see the CLI. Returning this from setters lets you chain them:

await User.new().setName('Alice').setEmail('alice@example.com').create();

Column types​

@Column mirrors Knex column types:

  • Integers: Increment(options?), Integer(length?), Tinyint(length?), Smallint(), Mediumint(), Bigint(), BigInteger().
  • Decimals: Float(precision?, scale?), Double(precision?, scale?), Decimal(precision?, scale?).
  • Text: String(length?), Text(type?) ('text', 'mediumtext' or 'longtext'), Uuid(options?).
  • Dates: Date(), DateTime(options?), Time(), Timestamp(options?) (options: useTz, precision).
  • Others: Boolean(), Enum(values, options?) (alias Enu), Json(), Jsonb(), Binary(length?), Point().

Column options​

@ColumnOption adds constraints:

  • Keys and indexes: Primary(options?), Unique(options?), Index(indexName?).
  • Values: NotNullable(), Nullable(), DefaultTo(value, options?), Unsigned().
  • Checks: CheckPositive(), CheckNegative(), CheckIn(values), CheckNotIn(values), CheckBetween(values), CheckLength(operator, length), CheckRegex(regex). Each takes a constraint name as its last argument.
  • Misc: Comment(text), Collate(collation), References(column) for a foreign key, see relations.

Transform values​

@Transform.Hydrate(fn) transforms a value read from the database, @Transform.DeHydrate(fn) a value before it is written:

import {Column, QueryRow, Table, Transform} from '@fca.gg/orm';

@Table('settings')
export class Settings extends QueryRow {
@Column.Text()
@Transform.Hydrate((value) => JSON.parse(value))
@Transform.DeHydrate((value) => JSON.stringify(value))
private data!: Record<string, unknown>;
}

Both decorators also accept a property name as their first argument, to keep the raw value in the column and work on another property:

  • @Transform.Hydrate('parsed', fn): on read, parsed receives fn(raw value);
  • @Transform.DeHydrate('parsed', fn): on write, the column receives fn(parsed).