Skip to content

Commit

Permalink
feat(prepare): added function prepare to help interact with multiple …
Browse files Browse the repository at this point in the history
…dependencies into the migration
  • Loading branch information
Stradivario committed Apr 22, 2024
1 parent 6a111af commit 30ce4a8
Show file tree
Hide file tree
Showing 9 changed files with 109 additions and 56 deletions.
104 changes: 64 additions & 40 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -33,7 +33,7 @@ chmod +x xmigrate-linux
```

```bash
./xmigrate up|down|create|etc
./xmigrate up|down|create
```

Using `NodeJS`
Expand Down Expand Up @@ -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: {
Expand Down Expand 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')
Expand All @@ -183,7 +202,7 @@ module.exports = {
.updateOne({ artist: 'The Doors' }, { $set: { stars: 5 } });
},

async down(client) {
async down([client]) {
await client
.db()
.collection('albums')
Expand All @@ -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'];
},
};
Expand All @@ -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'];
}
```
Expand All @@ -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')
Expand All @@ -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')
Expand Down Expand Up @@ -378,6 +409,21 @@ export default async (): Promise<Config> => {
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: {
Expand Down Expand 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
}
`;
Expand Down Expand Up @@ -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...
6 changes: 3 additions & 3 deletions src/injection.tokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<any, any>) => unknown;
up: (options: Record<any, any>) => unknown;
prepare: (db: MongoClient) => Promise<any>;
};

export interface LoggerConfig {
Expand Down
8 changes: 4 additions & 4 deletions src/services/migration/migration.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export class MigrationsResolver {
}
return {
...migration,
prepare: migration.prepare || (() => Promise.resolve()),
prepare: migration.prepare || ((db) => Promise.resolve([db])),
};
}

Expand Down
7 changes: 5 additions & 2 deletions src/templates/es5.ts
Original file line number Diff line number Diff line change
@@ -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']
}
}
Expand Down
7 changes: 5 additions & 2 deletions src/templates/es6.ts
Original file line number Diff line number Diff line change
@@ -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'];
}
`;
15 changes: 15 additions & 0 deletions src/templates/migration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down
7 changes: 5 additions & 2 deletions src/templates/native.ts
Original file line number Diff line number Diff line change
@@ -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')
Expand All @@ -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')
Expand Down
9 changes: 7 additions & 2 deletions src/templates/typescript.ts
Original file line number Diff line number Diff line change
@@ -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')
Expand All @@ -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')
Expand Down

0 comments on commit 30ce4a8

Please sign in to comment.