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.
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();}Each one works on its own; use the ones your deployment needs.
createDatabase() speaks each dialect, skips an existing database and then runs the migrations or synchronize().
await createDatabase({ options })Fails the deploy when the migrations and the entities describe different schemas, and lists the statements that would reconcile them.
npx typeorm-extension db driftrenameIndex, renameForeignKey and changeColumnType read the current state first and return false when already applied.
renameIndex(queryRunner, { table, from, to })A named advisory lock for one replica at a time, and a row read with a write lock inside a transaction.
withDatabaseLock(queryRunner, 'migrations', fn)Recognize a unique or foreign key violation and a lock conflict, whichever driver raised it.
if (isDatabaseUniqueViolationError(e)) { … }Seeder classes and factories, run by seed run, optionally tracked so each seeder runs once.
await factoryManager.get(User).saveMany(5)The same deployment concerns, without and with typeorm-extension.
CREATE DATABASE.createDatabase() or db create. Skips an existing database.pg_advisory_lock / GET_LOCK.withDatabaseLock(): one session at a time, released also when the callback throws.assertSchemaMatchesMetadata() or db drift fails the deploy.withLockedEntity() reads the row with a write lock inside a transaction.code, number or errorNum, depending on the driver.isDatabaseUniqueViolationError(e) knows where each driver puts the code.Describe the data source once. The CLI finds data-source.ts in src/ and creates, checks and seeds from it.
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);