Usage Chart
How strongly this package shapes the runtime
Package Flow
How this package moves through xtaskjs runtime phases
Before startup
Register datasources with registerTypeOrmDataSource() or TypeOrmDataSource() so xtaskjs knows what to initialize.
During CreateApplication()
The integration opens datasources, publishes repository and datasource tokens into the container, and makes them injectable to services and controllers.
During app.close()
Datasource connections are destroyed automatically so database shutdown is aligned with the main application lifecycle.
API Surface
Representative exports from the upstream package
TypeORM bridge
- registerTypeOrmDataSource
- TypeOrmDataSource
- InjectDataSource
- InjectRepository
- TypeOrmMigration
- TypeOrmSeeder
Re-exported ORM APIs
- DataSource
- Entity
- Column
- PrimaryGeneratedColumn
- OneToMany
- ManyToOne
- Repository
Lifecycle surface
- initializeTypeOrmIntegration
- shutdownTypeOrmIntegration
Usage
Typical adoption flow
1. Register datasource definitions
Describe your datasource once and keep entities, migrations, and connection details close to the app entry point.
2. Inject repositories or datasources
Use the xtask TypeORM decorators and tokens to inject repositories into DI-managed services instead of constructing them manually.
3. Let lifecycle manage connections
Rely on bootstrap and shutdown hooks instead of manual initialize() and destroy() calls across the codebase.
Example
Reference snippet
import {
DataSource,
MigrationInterface,
QueryRunner,
TypeOrmDataSource,
TypeOrmMigration,
TypeOrmSeeder,
} from "@xtaskjs/typeorm";
import { UserEntity } from "./user.entity";
@TypeOrmDataSource({
name: "default",
type: "sqlite",
database: process.env.DB_PATH || "xtask-typeorm.sqlite",
entities: [UserEntity],
synchronize: false,
runMigrationsOnServerStart: true,
runSeedersOnServerStart: true,
})
export class DatabaseConfig {}
@TypeOrmMigration({ dataSourceName: "default" })
export class CreateUsersTable1700000000000 implements MigrationInterface {
async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query("CREATE TABLE IF NOT EXISTS users (id integer primary key autoincrement, name varchar(120) not null)");
}
async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query("DROP TABLE IF EXISTS users");
}
}
@TypeOrmSeeder({ dataSourceName: "default", order: 1 })
export class DefaultUsersSeeder {
async run(dataSource: DataSource): Promise<void> {
await dataSource.query("INSERT INTO users (name) VALUES ('Ada Lovelace')");
}
}
Samples
Official samples to inspect next
Reference samples: 04-typeorm_app
Related
Packages commonly used with this one