UNPKG

ts-migrate-mongoose

Version:

A migration framework for Mongoose, built with TypeScript.

128 lines (124 loc) 3.61 kB
import { Logger, Module } from '@nestjs/common'; import { Migrator } from '../index.mjs'; import 'node:fs'; import 'node:path'; import 'node:url'; import 'mongoose'; import 'node:readline'; const MIGRATION_OPTIONS = Symbol("MIGRATION_OPTIONS"); class MigrationService { logger = new Logger(MigrationService.name); options; migrator; constructor(options) { this.options = options; } get instance() { return this.migrator; } async onApplicationBootstrap() { this.migrator = await Migrator.connect(this.options); this.logger.log("Connected to migration database"); if (this.options.onBootstrap) { await this.options.onBootstrap(this.migrator); } else { await this.runDefaultMigrations(); } } async onApplicationShutdown() { if (this.migrator) { await this.migrator.close(); } } async runDefaultMigrations() { const existing = await this.migrator.list(); const migrations = await this.migrator.run("up"); if (migrations.length === 0 && existing.length > 0) { this.logger.log(`All migrations are up to date (${existing.length} total)`); } else if (migrations.length === 0) { this.logger.log("No migrations found"); } else { for (const migration of migrations) { this.logger.log(`up: ${migration.filename}`); } this.logger.log(`Applied ${migrations.length} migration(s)`); } } } var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __decorateClass = (decorators, target, key, kind) => { var result = kind > 1 ? void 0 : kind ? __getOwnPropDesc(target, key) : target; for (var i = decorators.length - 1, decorator; i >= 0; i--) if (decorator = decorators[i]) result = (decorator(result)) || result; return result; }; let MigrationModule = class { static forRoot(options) { return { module: MigrationModule, global: options.isGlobal ?? false, providers: [ { provide: MIGRATION_OPTIONS, useValue: options }, { provide: MigrationService, useFactory: (opts) => new MigrationService(opts), inject: [MIGRATION_OPTIONS] } ], exports: [MigrationService] }; } static forRootAsync(options) { const asyncProviders = MigrationModule.createAsyncProviders(options); return { module: MigrationModule, global: options.isGlobal ?? false, imports: options.imports ?? [], providers: [ ...asyncProviders, { provide: MigrationService, useFactory: (opts) => new MigrationService(opts), inject: [MIGRATION_OPTIONS] } ], exports: [MigrationService] }; } static createAsyncProviders(options) { if (options.useFactory) { return [ { provide: MIGRATION_OPTIONS, useFactory: options.useFactory, inject: options.inject ?? [] } ]; } if (options.useClass) { return [ { provide: options.useClass, useClass: options.useClass }, { provide: MIGRATION_OPTIONS, useFactory: (factory) => factory.createMigrationOptions(), inject: [options.useClass] } ]; } if (options.useExisting) { return [ { provide: MIGRATION_OPTIONS, useFactory: (factory) => factory.createMigrationOptions(), inject: [options.useExisting] } ]; } return []; } }; MigrationModule = __decorateClass([ Module({}) ], MigrationModule); export { MIGRATION_OPTIONS, MigrationModule, MigrationService };