Skip to main content

Class: SchemaGenerator

Defined in: src/codegen/SchemaGenerator.ts:36

Code-First schema generation utilities.

Example​

const gen = new SchemaGenerator(ctx.adapter, [User, Order]);
await gen.ensureCreated(); // create all tables that don't exist
const file = gen.generateMigration('Init'); // get migration file content

Constructors​

Constructor​

new SchemaGenerator(adapter, entityClasses, options?): SchemaGenerator

Defined in: src/codegen/SchemaGenerator.ts:40

Parameters​

ParameterType
adapterIDbAdapter
entityClassesFunction[]
optionsSchemaGeneratorOptions

Returns​

SchemaGenerator

Methods​

ensureCreated()​

ensureCreated(): Promise<void>

Defined in: src/codegen/SchemaGenerator.ts:59

Creates all tables that do not yet exist in the database. Uses CREATE TABLE IF NOT EXISTS semantics so it is safe to call repeatedly.

Returns​

Promise<void>


generateMigration()​

generateMigration(name): string

Defined in: src/codegen/SchemaGenerator.ts:85

Generates and returns the TypeScript source code for a migration file that will create all entity tables.

Parameters​

ParameterType
namestring

Returns​

string


generateDiffMigration()​

generateDiffMigration(name): Promise<string>

Defined in: src/codegen/SchemaGenerator.ts:125

Compares the live database schema against current entity metadata and generates an incremental migration file with only the necessary up/down diff statements.

Parameters​

ParameterType
namestring

Returns​

Promise<string>


writeDiffMigration()​

writeDiffMigration(name, migrationsDir): Promise<string>

Defined in: src/codegen/SchemaGenerator.ts:212

Writes the generated diff migration to a file in the migrations directory.

Parameters​

ParameterType
namestring
migrationsDirstring

Returns​

Promise<string>


writeMigration()​

writeMigration(name, migrationsDir): string

Defined in: src/codegen/SchemaGenerator.ts:228

Writes the generated migration to a file in the given migrations directory. Returns the file path that was written.

Parameters​

ParameterType
namestring
migrationsDirstring

Returns​

string


diff()​

diff(): Promise<SchemaDiff>

Defined in: src/codegen/SchemaGenerator.ts:243

Compares live DB schema against entity metadata and returns a diff. Introspects using information_schema or sqlite_master depending on provider.

Returns​

Promise<SchemaDiff>


diffDetailed()​

diffDetailed(): Promise<DetailedSchemaDiff>

Defined in: src/codegen/SchemaGenerator.ts:355

Detailed schema diff with hasChanges flag and structured modification lists.

Returns​

Promise<DetailedSchemaDiff>


squash()​

squash(migrations, squashedName?): MigrationModule

Defined in: src/codegen/SchemaGenerator.ts:380

Squashes multiple migration modules into a single consolidated baseline migration.

Parameters​

ParameterTypeDefault value
migrationsMigrationModule[]undefined
squashedNamestring'SquashedBaseline'

Returns​

MigrationModule


push()​

push(dryRun?): Promise<string[]>

Defined in: src/codegen/SchemaGenerator.ts:388

Applies the diff (missing tables / columns) to the live database.

Parameters​

ParameterTypeDefault value
dryRunbooleanfalse

Returns​

Promise<string[]>