UNPKG

ts-migrate-mongoose

Version:

A migration framework for Mongoose, built with TypeScript.

131 lines (126 loc) 3.73 kB
'use strict'; var common = require('@nestjs/common'); var index = require('../index.cjs'); require('node:fs'); require('node:path'); require('node:url'); require('mongoose'); require('node:readline'); const MIGRATION_OPTIONS = Symbol("MIGRATION_OPTIONS"); class MigrationService { logger = new common.Logger(MigrationService.name); options; migrator; constructor(options) { this.options = options; } get instance() { return this.migrator; } async onApplicationBootstrap() { this.migrator = await index.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; }; exports.MigrationModule = class MigrationModule { static forRoot(options) { return { module: exports.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 = exports.MigrationModule.createAsyncProviders(options); return { module: exports.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 []; } }; exports.MigrationModule = __decorateClass([ common.Module({}) ], exports.MigrationModule); exports.MIGRATION_OPTIONS = MIGRATION_OPTIONS; exports.MigrationService = MigrationService;