Skip to content
v4 TypeORM, ready for deployment

Create, migrate, check.
Lock and seed.

TypeORM maps your entities and runs your migrations. typeorm-extension covers the rest of a deployment: the database has to exist, replicas start at once, the schema can drift from the entities, and every driver reports a duplicate key differently.

npm install typeorm-extension typeorm
  • MIT licensed
  • Node 22+
  • ESM-only
  • TypeORM ^1.1.3
deploy.ts
import { DataSource } from 'typeorm';import {    assertSchemaMatchesMetadata,    runSeeders,    withDatabaseLock,} from 'typeorm-extension';import { options } from './data-source'; // `db create` ran once in the release stepconst dataSource = new DataSource({    ...options,    migrationsRun: false,    synchronize: false,    dropSchema: false,});await dataSource.initialize(); const queryRunner = dataSource.createQueryRunner();try {    await withDatabaseLock(queryRunner, 'migrations', async () => {        await dataSource.runMigrations();        await assertSchemaMatchesMetadata(dataSource);        await runSeeders(dataSource, { seedTracking: true });    });} finally {    await queryRunner.release();}
Drivers
  • postgres
  • cockroachdb
  • mysql
  • mariadb
  • mssql
  • oracle
  • mongodb
  • better-sqlite3
// why

What you stop writing.

The same deployment concerns, without and with typeorm-extension.

ConcernWithout itWith it
The database existsConnect to a server-level database and run each dialect's own CREATE DATABASE.createDatabase() or db create. Skips an existing database.
Several replicas migrateEach replica runs the migrations at boot, or you hand-roll pg_advisory_lock / GET_LOCK.withDatabaseLock(): one session at a time, released also when the callback throws.
Migrations drift from the entitiesNoticed when the next generated migration contains unrelated changes.assertSchemaMatchesMetadata() or db drift fails the deploy.
Read-modify-write racesTwo requests read the same row, both pass the check, both write.withLockedEntity() reads the row with a write lock inside a transaction.
A failed writeRead code, number or errorNum, depending on the driver.isDatabaseUniqueViolationError(e) knows where each driver puts the code.
Development and test dataAd-hoc scripts that insert rows and break on the second run.Seeders and factories, tracked so each seeder runs once.
// quickstart

From data source to seeded database

Describe the data source once. The CLI finds data-source.ts in src/ and creates, checks and seeds from it.

Installation
import { DataSource, DataSourceOptions } from 'typeorm';import { SeederOptions } from 'typeorm-extension'; export const options: DataSourceOptions & SeederOptions = {    type: 'postgres',    database: 'app',    seeds: ['src/database/seeds/**/*{.ts,.js}'],    factories: ['src/database/factories/**/*{.ts,.js}'],}; export const dataSource = new DataSource(options);