diff --git a/README.md b/README.md index 8c9fdbb..d5bc9bf 100755 --- a/README.md +++ b/README.md @@ -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 +``` + +The command helps to create a migration script in the `src -> migration` directory as below: + +```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. diff --git a/bin/migrate.js b/bin/migrate.js index 6e63a85..0ded7e2 100755 --- a/bin/migrate.js +++ b/bin/migrate.js @@ -22,7 +22,6 @@ program program .command("create [description]") .description("create a new database migration with the provided description") - .option("-f --file ", "use a custom config file") .action((description, options) => { global.options = options; migrate