Skip to main content

ORM

@fca.gg/orm is a TypeScript object-relational mapper (ORM) for Node.js, built on Knex.js. Tables are described with decorated classes, and the CLI generates migrations by comparing those classes to the database.

Work in progress

The ORM is still under construction. Its API may change, and it is mostly used with MySQL: read the project status before adopting it.

  • Decorator-based models: @Table, @Column.String(255), @ColumnOption.Unique()…
  • The Active Record pattern: user.create(), user.update(), User.findOneBy({email}).
  • Declarative joins with @Join, hydrated automatically.
  • Generated migrations: orm migration compares your classes with the database schema and writes Knex migration files.
  • Transformations on read and write with @Transform.

Example​

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

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

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

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

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

await KnexInstance.init();

const user = await User.new().setName('Alice').create();
const alice = await User.findOneBy({name: 'Alice'});

To get started, follow the installation.

Project status​

What we noted in the current code:

  • query methods do not throw when SQL fails: they log the error and return undefined;
  • create() reads the created row back from the ID returned by the insert, and countBy() reads the result in MySQL's format (count(*)). On PostgreSQL, these two methods may not return the expected result;
  • the ORM does not provide transaction helpers yet: use Knex directly, see queries.

License​

The ORM is released under the AGPL v3 license with a linking exception: using it as a dependency does not require your application to be open source. The source code is on GitHub.