@spinajs/orm
v2.0.482
Published
framework orm module
Readme
@spinajs/orm
The SpinaJS ORM core: model metadata and decorators, query builders, relations, a unit of work, and the schema / migration layer.
It contains no SQL. Statements and compilers are declared abstract and resolved from each
connection's DI container, which is what lets one application run SQLite and MySQL side by side.
The SQL itself lives in @spinajs/orm-sql and the driver packages above it.
Install
npm install @spinajs/orm @spinajs/orm-sqliteUsage
import { DI } from '@spinajs/di';
import { Connection, Model, ModelBase, Primary, Orm } from '@spinajs/orm';
import { SqliteOrmDriver } from '@spinajs/orm-sqlite';
@Connection('default')
@Model('users')
export class User extends ModelBase<User> {
@Primary()
public Id: number;
public Email: string;
}
DI.register(SqliteOrmDriver).as('orm-driver-sqlite');
await DI.resolve(Orm);
const user = await User.getOrCreate(null, { Email: '[email protected]' });
const recent = await User.where('Email', 'like', '%@example.com').take(10);Connections are declared under the db key of @spinajs/configuration. Resolving Orm opens
them, runs pending migrations, reflects each table's columns onto its model, and installs the
static methods used above.
Documentation
Full documentation lives in docs/.
| | Page | | --- | --- | | 01 | Getting started | | 02 | Configuration | | 03 | Models and decorators | | 04 | Static model API | | 05 | Instance API | | 06 | Query builder | | 07 | Relations | | 08 | Unit of work | | 09 | Transactions | | 10 | Schema and migrations | | 11 | Converters and hydration | | 12 | Architecture | | 13 | Observability |
Related packages
| Package | Purpose |
| --- | --- |
| @spinajs/orm-sql | Shared SQL statements and compilers |
| @spinajs/orm-sqlite | SQLite driver |
| @spinajs/orm-mysql | MySQL / MariaDB driver |
| @spinajs/orm-mssql | SQL Server driver |
| @spinajs/orm-http | Route arguments, filtering and DTO relations for @spinajs/http |
| @spinajs/orm-api | CRUD controller building blocks |
Development
npm test # unit suite, no database required
npm run build # compile to lib/
npm run docs:check # from the repo root: type-check every documentation sample