diff --git a/README.md b/README.md index 2d0c655..282247b 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,7 @@ Migration library for `Mongodb` and `Mongoose` written in `TypeScript` - Mongoose and Mongodb compatibility - ACID transactions provided by MongoDB - `error` and `success` logs for `up`/`down` migrations -- Infinite rrror log with `append` NodeJS streaming technique +- Infinite error log with `append` NodeJS streaming technique - 100% TypeScript support with JIT compilation provided by [esbuild](https://esbuild.github.io/) ## Installation @@ -33,7 +33,7 @@ chmod +x xmigrate-linux ``` ```bash -./xmigrate up|down|create|etc +./xmigrate up|down|create ``` Using `NodeJS` @@ -61,10 +61,24 @@ export default async () => { migrationsDir: './migrations', defaultTemplate: 'es6', typescript: true, - builder: 'ESBUILD', outDir: './.xmigrate', /* Custom datetime formatting can be applied like so */ // dateTimeFormat: () => new Date().toISOString(), + // bundler: { + // build(entryPoints: string[], outdir: string) { + // return esbuild.build({ + // entryPoints, + // bundle: true, + // sourcemap: false, + // minify: false, + // platform: 'node', + // format: 'cjs', + // outdir, + // logLevel: 'info', + // plugins: [pluginTsc()], + // }) + // }, + // }, logger: { folder: './migrations-log', up: { @@ -173,7 +187,12 @@ Native mongo driver template ```typescript module.exports = { - async up(client) { + + async prepare(client) { + return [client] + } + + async up([client]) { await client .db() .collection('albums') @@ -183,7 +202,7 @@ module.exports = { .updateOne({ artist: 'The Doors' }, { $set: { stars: 5 } }); }, - async down(client) { + async down([client]) { await client .db() .collection('albums') @@ -199,11 +218,16 @@ module.exports = { ```typescript module.exports = { - async up(client) { + + async prepare(client) { + return [client] + } + + async up([client]) { return ['UP']; }, - async down(client) { + async down([client]) { return ['DOWN']; }, }; @@ -212,10 +236,13 @@ module.exports = { `ES6` template ```typescript -export async function up(client) { +export async function prepare(client) { + return [client]; +} +export async function up([client]) { return ['Up']; } -export async function down(client) { +export async function down([client]) { return ['Down']; } ``` @@ -231,7 +258,11 @@ npm install @types/mongodb @types/mongoose -D ```typescript import { MongoClient } from 'mongodb'; -export async function up(client: MongoClient) { +export async function prepare(client: mongoClient) { + return [client]; +} + +export async function up([client]: [MongoClient]) { await client .db() .collection('albums') @@ -243,7 +274,7 @@ export async function up(client: MongoClient) { .updateOne({ artist: 'The Doors' }, { $set: { stars: 5 } }); } -export async function down(client: MongoClient) { +export async function down([client]: [MongoClient]) { await client .db() .collection('albums') @@ -378,6 +409,21 @@ export default async (): Promise => { defaultTemplate: 'typescript', typescript: true, outDir: './.xmigrate', + // bundler: { + // build(entryPoints: string[], outdir: string) { + // return esbuild.build({ + // entryPoints, + // bundle: true, + // sourcemap: false, + // minify: false, + // platform: 'node', + // format: 'cjs', + // outdir, + // logLevel: 'info', + // plugins: [pluginTsc()], + // }); + // }, + // }, logger: { folder: './migrations-log', up: { @@ -466,10 +512,15 @@ setup({ const template = ` import { MongoClient } from 'mongodb'; -export async function up(client: MongoClient) { +export async function prepare(client: MongoClient) { + return [client] +} + +export async function up([client]: [MongoClient]) { return true } -export async function down(client: MongoClient) { + +export async function down([client]: [MongoClient]) { return true } `; @@ -498,30 +549,3 @@ export async function down(client: MongoClient) { process.exit(0); }, console.error.bind(console)); ``` - -### Minimal configuration - -```typescript -export default async () => { - return { - defaultTemplate: 'typescript', - outDir: './.xmigrate', - typescript: true, - mongodb: { - url: 'mongodb://localhost:27017', - databaseName: 'test', - options: { - useNewUrlParser: true, - }, - }, - }; -}; -``` - -### Performance tests - -Running 600 `migrations` takes less than 15 seconds in TypeScript compiled right down to Javascript ES5. - -Check [this](https://cloudflare-ipfs.com/ipfs/QmRsE9cRLxeVrya3eZUAheMRoxrM1RKn8MQwbczpicpvxK) video inside IPFS network - -Link is not working at the moment... diff --git a/src/injection.tokens.ts b/src/injection.tokens.ts index 53747af..873f9b4 100644 --- a/src/injection.tokens.ts +++ b/src/injection.tokens.ts @@ -13,9 +13,9 @@ export interface ReturnType { export const LoggerConfig = new InjectionToken('logger-config'); export const Config = new InjectionToken('migrations-config'); export type MigrationSchema = { - down: (db: MongoClient) => unknown; - up: (db: MongoClient) => unknown; - prepare: (db: MongoClient) => unknown; + down: (options: Record) => unknown; + up: (options: Record) => unknown; + prepare: (db: MongoClient) => Promise; }; export interface LoggerConfig { diff --git a/src/services/migration/migration.service.ts b/src/services/migration/migration.service.ts index fe60914..bc344e6 100644 --- a/src/services/migration/migration.service.ts +++ b/src/services/migration/migration.service.ts @@ -50,8 +50,8 @@ export class MigrationService { const migration = await this.migrationsResolver.loadMigration( item.fileName, ); - await migration.prepare(client); - result = await migration.up(client); + const prepare = await migration.prepare(client); + result = await migration.up(prepare); } catch (err) { const error = new ErrorMap(err.message); error.fileName = item.fileName; @@ -124,8 +124,8 @@ export class MigrationService { const migration = await this.migrationsResolver.loadMigration( lastAppliedItem.fileName, ); - await migration.prepare(client); - result = await migration.down(client); + const prepare = await migration.prepare(client); + result = await migration.down(prepare); } catch (err) { const error = new ErrorMap(err.message); error.fileName = lastAppliedItem.fileName; diff --git a/src/services/migrations-resolver/migrations-resolver.service.ts b/src/services/migrations-resolver/migrations-resolver.service.ts index de2659c..4b6ee1f 100644 --- a/src/services/migrations-resolver/migrations-resolver.service.ts +++ b/src/services/migrations-resolver/migrations-resolver.service.ts @@ -41,7 +41,7 @@ export class MigrationsResolver { } return { ...migration, - prepare: migration.prepare || (() => Promise.resolve()), + prepare: migration.prepare || ((db) => Promise.resolve([db])), }; } diff --git a/src/templates/es5.ts b/src/templates/es5.ts index 99efe38..e9a71e7 100644 --- a/src/templates/es5.ts +++ b/src/templates/es5.ts @@ -1,10 +1,13 @@ export default ` module.exports = { - async up (client) { + async prepare (client) { + return [client] + }, + async up ([client]) { return ['Up'] }, - async down (client) { + async down ([client]) { return ['Down'] } } diff --git a/src/templates/es6.ts b/src/templates/es6.ts index 83d3082..0e27ede 100644 --- a/src/templates/es6.ts +++ b/src/templates/es6.ts @@ -1,8 +1,11 @@ export default ` -export async function up(client) { +export async function prepare(client) { + return [client] +} +export async function up([client]) { return ['Up']; } -export async function down(client) { +export async function down([client]) { return ['Down']; } `; diff --git a/src/templates/migration.ts b/src/templates/migration.ts index 0ac74af..3b6afd3 100644 --- a/src/templates/migration.ts +++ b/src/templates/migration.ts @@ -5,6 +5,21 @@ export default `module.exports = async () => { defaultTemplate: 'es6', outDir: './.xmigrate', typescript: true, + // bundler: { + // build(entryPoints: string[], outdir: string) { + // return esbuild.build({ + // entryPoints, + // bundle: true, + // sourcemap: false, + // minify: false, + // platform: 'node', + // format: 'cjs', + // outdir, + // logLevel: 'info', + // plugins: [pluginTsc()], + // }) + // }, + // }, logger: { folder: './migrations-log', up: { diff --git a/src/templates/native.ts b/src/templates/native.ts index 1baf43c..6ce89d2 100644 --- a/src/templates/native.ts +++ b/src/templates/native.ts @@ -1,5 +1,8 @@ export default ` -export async function up (client) { +export async function prepare(client) { + return [client] +} +export async function up ([client]) { await client .db() .collection('albums') @@ -10,7 +13,7 @@ export async function up (client) { .updateOne({ artist: 'The Doors' }, { $set: { stars: 5 } }) }, -export async function down (client) { +export async function down ([client]) { await client .db() .collection('albums') diff --git a/src/templates/typescript.ts b/src/templates/typescript.ts index 1e2b08e..26930c7 100644 --- a/src/templates/typescript.ts +++ b/src/templates/typescript.ts @@ -1,7 +1,11 @@ export default ` import { MongoClient } from 'mongodb'; -export async function up(client: MongoClient) { +export async function prepare(client: MongoClient) { + return [client] +} + +export async function up([client]: [MongoClient]) { await client .db() .collection('albums') @@ -12,7 +16,8 @@ export async function up(client: MongoClient) { .collection('albums') .updateOne({ artist: 'The Doors' }, { $set: { stars: 5 } }); } -export async function down(client: MongoClient) { + +export async function down([client]: [MongoClient]) { await client .db() .collection('albums')