-
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(migrations): add migrations module (#1143)
- Loading branch information
1 parent
ae20c34
commit 576ac21
Showing
7 changed files
with
124 additions
and
2 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// https://github.com/tj/node-migrate/blob/main/examples/custom-state-storage/mongo-state-storage.js | ||
|
||
interface MigrationSet { | ||
lastRun: string; | ||
migrations: any[]; | ||
} | ||
|
||
export interface MigrationStore { | ||
load: (callback: (error: any, data: MigrationSet) => any) => any; | ||
save: (set: MigrationSet, callback: (error: any, result: any) => any) => any; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { getConnectionToken } from '@nestjs/mongoose'; | ||
import { Connection } from 'mongoose'; | ||
import { MigrationsService } from './services/migrations.service'; | ||
import { MongoDbStore } from './stores/mongo-db.store'; | ||
|
||
@Module({ | ||
providers: [ | ||
{ | ||
provide: 'MIGRATION_STORE', | ||
inject: [getConnectionToken()], | ||
useFactory: (connection: Connection) => new MongoDbStore(connection), | ||
}, | ||
MigrationsService, | ||
], | ||
}) | ||
export class MigrationsModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { | ||
Inject, | ||
Injectable, | ||
Logger, | ||
OnApplicationBootstrap, | ||
} from '@nestjs/common'; | ||
import { MigrationStore } from '../migration.store'; | ||
import { load } from 'migrate'; | ||
import { resolve as pathResolve } from 'path'; | ||
import * as appRoot from 'app-root-path'; | ||
|
||
@Injectable() | ||
export class MigrationsService implements OnApplicationBootstrap { | ||
private logger = new Logger(MigrationsService.name); | ||
|
||
constructor( | ||
@Inject('MIGRATION_STORE') private migrationStore: MigrationStore, | ||
) {} | ||
|
||
async onApplicationBootstrap() { | ||
this.logger.log('running migrations...'); | ||
await this.runMigrations(); | ||
this.logger.log('migrations run successfully'); | ||
} | ||
|
||
private async runMigrations() { | ||
return new Promise<void>((resolve, reject) => { | ||
load( | ||
{ | ||
stateStore: this.migrationStore, | ||
migrationsDirectory: pathResolve(appRoot.toString(), 'migrations'), | ||
}, | ||
(error, set) => { | ||
if (error) { | ||
return reject(error); | ||
} | ||
|
||
set.up((error) => { | ||
if (error) { | ||
return reject(error); | ||
} | ||
resolve(); | ||
}); | ||
}, | ||
); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { Connection } from 'mongoose'; | ||
import { MigrationStore } from '../migration.store'; | ||
|
||
export class MongoDbStore implements MigrationStore { | ||
constructor(private connection: Connection) {} | ||
|
||
async load(callback) { | ||
const data = await this.connection.db.collection('migrations').findOne({}); | ||
return callback(null, data ?? {}); | ||
} | ||
|
||
async save(data, callback) { | ||
const result = await this.connection.db.collection('migrations').updateOne( | ||
{}, | ||
{ | ||
$set: { | ||
lastRun: data.lastRun, | ||
}, | ||
$push: { | ||
migrations: { $each: data.migrations }, | ||
}, | ||
}, | ||
{ | ||
upsert: true, | ||
}, | ||
); | ||
|
||
return callback(null, result); | ||
} | ||
} |