Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(prepare): added prepare step #38

Merged
merged 5 commits into from
Apr 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
152 changes: 97 additions & 55 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|status
```

Using `NodeJS`
Expand All @@ -55,16 +55,36 @@ Manual configuration
You can create a `xmigrate.js` file where you execute the `xmigrate` command:

```typescript
import { MongoClient } from 'mongodb';
import { connect } from 'mongoose';

export default async () => {
return {
changelogCollectionName: 'migrations',
migrationsDir: './migrations',
defaultTemplate: 'es6',
typescript: true,
builder: 'ESBUILD',
outDir: './.xmigrate',
/* Custom datetime formatting can be applied like so */
// dateTimeFormat: () => new Date().toISOString(),
/* If you do need some better bundling of your migrations when there are tsconfig paths namespaces @shared/my-namespace
You should consider using `bundler.build()` configuration.
*/
// 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 All @@ -76,11 +96,13 @@ export default async () => {
error: 'down.error.log',
},
},
mongodb: {
url: `mongodb://localhost:27017`,
databaseName: 'test',
options: {
useNewUrlParser: true,
database: {
async connect() {
const url = 'mongodb://localhost:27017';

await connect(url);
const client = await MongoClient.connect(url);
return client;
},
},
};
Expand Down Expand Up @@ -173,7 +195,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 +210,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 +226,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 +244,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 +266,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 +282,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 @@ -370,6 +409,8 @@ When you change your configuration file to `xmigrate.ts` it will automatically t

```typescript
import { Config } from '@rxdi/xmigrate';
import { MongoClient } from 'mongodb';
import { connect } from 'mongoose';

export default async (): Promise<Config> => {
return {
Expand All @@ -378,6 +419,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 All @@ -389,11 +445,14 @@ export default async (): Promise<Config> => {
error: 'down.error.log',
},
},
mongodb: {
url: `mongodb://localhost:27017`,
databaseName: 'test',
options: {
useNewUrlParser: true,
database: {
async connect() {
const url =
process.env.MONGODB_CONNECTION_STRING ?? 'mongodb://localhost:27017';

await connect(url);
const client = await MongoClient.connect(url);
return client;
},
},
};
Expand Down Expand Up @@ -421,6 +480,8 @@ import {
LoggerConfig,
Config,
} from '@rxdi/xmigrate';
import { MongoClient } from 'mongodb';
import { connect } from 'mongoose';

const config = {
changelogCollectionName: 'migrations',
Expand All @@ -439,11 +500,14 @@ const config = {
error: 'down.error.log',
},
},
mongodb: {
url: 'mongodb://localhost:27017',
databaseName: 'test',
options: {
useNewUrlParser: true,
database: {
async connect() {
const url =
process.env.MONGODB_CONNECTION_STRING ?? 'mongodb://localhost:27017';

await connect(url);
const client = await MongoClient.connect(url);
return client;
},
},
};
Expand All @@ -466,10 +530,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 +567,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...
40 changes: 22 additions & 18 deletions examples/api.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { Container, setup } from '@rxdi/core';
import { MongoClient } from 'mongodb';
import { connect } from 'mongoose';

import {
MigrationService,
Config,
ConfigService,
GenericRunner,
LogFactory,
ConfigService,
LoggerConfig,
Config
MigrationService,
} from '../src/index'; // equivalent to '@rxdi/xmigrate'

const config = {
Expand All @@ -19,20 +21,22 @@ const config = {
folder: './migrations-log',
up: {
success: 'up.success.log',
error: 'up.error.log'
error: 'up.error.log',
},
down: {
success: 'down.success.log',
error: 'down.error.log'
}
error: 'down.error.log',
},
},
database: {
async connect() {
const url = 'mongodb://localhost:27017';

await connect(url);
const client = await MongoClient.connect(url);
return client;
},
},
mongodb: {
url: 'mongodb://localhost:27017',
databaseName: 'test',
options: {
useNewUrlParser: true
}
}
};

setup({
Expand All @@ -42,13 +46,13 @@ setup({
ConfigService,
{
provide: Config,
useValue: config
useValue: config,
},
{
provide: LoggerConfig,
useValue: config.logger
}
]
useValue: config.logger,
},
],
}).subscribe(async () => {
const template = `
import { MongoClient } from 'mongodb';
Expand All @@ -67,7 +71,7 @@ export async function down(client: MongoClient) {
const filePath = await migrationService.createWithTemplate(
template as 'typescript',
'pesho1234',
{ raw: true, typescript: true }
{ raw: true, typescript: true },
);
console.log(filePath);

Expand Down
Loading
Loading