Skip to content

Commit

Permalink
feat(prepare): added prepare step
Browse files Browse the repository at this point in the history
  • Loading branch information
Stradivario committed Apr 22, 2024
1 parent a66ff59 commit cf3b868
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 2 deletions.
13 changes: 13 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"@rxdi/core": "^0.7.37",
"chalk": "2.4.2",
"esbuild": "^0.20.2",
"esbuild-plugin-tsc": "^0.4.0",
"esm": "3.2.25",
"mongodb": "3.3.3",
"mongoose": "5.7.6"
Expand Down
2 changes: 2 additions & 0 deletions src/helpers/typescript-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export const TranspileTypescriptESBuild = async (
outdir: string,
) => {
try {
const tscPlugin = await import('esbuild-plugin-tsc');
return (await import('esbuild')).build({
entryPoints,
bundle: true,
Expand All @@ -35,6 +36,7 @@ export const TranspileTypescriptESBuild = async (
format: 'cjs',
outdir,
logLevel: 'info',
plugins: [tscPlugin.default()],
});
} catch (e) {
console.error(e);
Expand Down
1 change: 1 addition & 0 deletions src/injection.tokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export const Config = new InjectionToken('migrations-config');
export type MigrationSchema = {
down: (db: MongoClient) => unknown;
up: (db: MongoClient) => unknown;
prepare: (db: MongoClient) => unknown;
};

export interface LoggerConfig {
Expand Down
4 changes: 3 additions & 1 deletion src/services/migration/migration.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ export class MigrationService {
const migration = await this.migrationsResolver.loadMigration(
item.fileName,
);
await migration.prepare(client);
result = await migration.up(client);
} catch (err) {
const error = new ErrorMap(err.message);
Expand Down Expand Up @@ -87,7 +88,7 @@ export class MigrationService {
};
await logger.log(res);
migrated.push(res);
return await true;
return true;
};
for (const item of pendingItems) {
await migrateItem(item);
Expand Down Expand Up @@ -126,6 +127,7 @@ export class MigrationService {
const migration = await this.migrationsResolver.loadMigration(
lastAppliedItem.fileName,
);
await migration.prepare(client);
result = await migration.down(client);
} catch (err) {
const error = new ErrorMap(err.message);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export class MigrationsResolver {
// eslint-disable-next-line @typescript-eslint/no-var-requires
migration = require('esm')(module)(this.getFilePath(fileName));
}
migration.prepare = migration.prepare || (() => Promise.resolve());
return migration;
}

Expand Down Expand Up @@ -74,7 +75,7 @@ export class MigrationsResolver {
}

async loadTsCompiledMigration(fileName: string) {
return require(this.getTsCompiledFilePath(fileName));
return import(this.getTsCompiledFilePath(fileName));
}

async transpileMigrations(
Expand Down
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"experimentalDecorators": true,
"moduleResolution": "node",
"noUnusedLocals": true,
"esModuleInterop": true,
"outDir": "lib",
"lib": [
"es2017",
Expand Down

0 comments on commit cf3b868

Please sign in to comment.