Skip to content
Open
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
42 changes: 42 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,45 @@ await mongoose.connect(url, defaultMongoOpts)
const person = await PersonRepo.byID(id));
const people = await PersonRepo.all({});
```

## How to run a migration

```shell
yarn migrations init
```

The above command will initialize a `migrate-mongo-config.js` in the root directory and create a migration folder in your `src` folder.

Now run

```shell
yarn migrations create <name>
```

The command helps to create a migration script in the `src -> migration` directory as below:
Comment thread
noxecane marked this conversation as resolved.

```ts
import { Db, MongoClient } from "mongodb";

export async function up(db: Db, conn: MongoClient) {
// TODO write your migration here.
// Example:
// await db.collection('albums').updateOne({artist: 'The Beatles'}, {$set: {blacklisted: true}});
}

export async function down(db: Db, conn: MongoClient) {
// TODO write the statements to rollback your migration (if possible)
// Example:
// await db.collection('albums').updateOne({artist: 'The Beatles'}, {$set: {blacklisted: false}});
}
```

When the script is applied, the `up` function is responsible for changing the database schema, while the `down` function is responsible for going back to the previous database state.

Call `migrateUp` or `migrateDown` function to apply migration.

```ts
migrateUp(connection);
```

`connection` is a mongodb connection.
1 change: 0 additions & 1 deletion bin/migrate.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ program
program
.command("create [description]")
.description("create a new database migration with the provided description")
.option("-f --file <file>", "use a custom config file")
.action((description, options) => {
global.options = options;
migrate
Expand Down