@karcass/migration-commands
v0.0.2
Published
CliService commands for creation, doing and undoing TypeORM migrations
Downloads
6
Readme
@karcass/migration-commands
CliService commands for creation, doing and undoing TypeORM migrations. This commands included by default in karcass.
Installation
npm install @karcass/migration-commands
Usage
index.ts:
import { CliService } from '@karcass/cli-service'
import { createConnection } from 'typeorm'
import { CreateMigrationCommand, MigrateCommand, MigrateUndoCommand } from '@karcass/migration-commands'
const cliService = new CliService()
const connection = await createConnection({
type: 'sqlite',
database: 'test/test.sqlite',
})
cliService.add(CreateMigrationCommand, () => new CreateMigrationCommand())
cliService.add(MigrateCommand, () => new MigrateCommand(connection))
cliService.add(MigrateUndoCommand, () => new MigrateUndoCommand(connection))
await cliService.run()
Output:
=== Available commands: ===
--help Show this help
migrations:generate <path/name> Create migration with name from argument, e.g. migrations:generate MyBundle/Migrations/MyMigrationName
migrations:migrate Doing migrations
migrations:migrate:undo Reverts last migration
So running ts-node index.ts migrations:generate test/MyMigration
will create a file with name like test/1577032764-MyMigration.ts
and contents like:
import { MigrationInterface, QueryRunner } from 'typeorm'
export default class MyMigration1577032764233 implements MigrationInterface {
public async up(queryRunner: QueryRunner): Promise<any> {}
public async down(queryRunner: QueryRunner): Promise<any> {}
}
Command ts-node index.ts migrations:migrate
will apply all non-applied migrations.
Command ts-noed index.ts migrations:migrate:undo
reverts one last migrated migration.
More about migrations in TypeORM you can read here.