diff --git a/README.md b/README.md index 0a78fab4..c91a09ec 100644 --- a/README.md +++ b/README.md @@ -21,3 +21,8 @@ If the playground is turned on, you can access it via `/graphql` route. To execute the request, enter it in the input field on the left and click on the request execution button. On the right side you will see the result of the query. + +## Migrations + +Run `yarn migrations:up` command to apply migration revisions or +`yarn migrations:down` to rollback the last revision. diff --git a/migrate-mongo-config.js b/migrate-mongo-config.js index 32a72a3e..92b55aaa 100644 --- a/migrate-mongo-config.js +++ b/migrate-mongo-config.js @@ -2,7 +2,7 @@ * @file Configuration for migrate-mongo (https://www.npmjs.com/package/migrate-mongo) */ -require('./src/env'); +require('dotenv').config(); const config = { mongodb: { @@ -16,8 +16,8 @@ const config = { /** * Removes a deprecation warning when connecting */ - useUnifiedTopology: true - } + useUnifiedTopology: true, + }, }, /** @@ -28,7 +28,7 @@ const config = { /** * The mongodb collection where the applied changes are stored. Only edit this when really necessary */ - changelogCollectionName: 'changelog' + changelogCollectionName: 'changelog', }; module.exports = config; diff --git a/migrations/20200224203115-add-index-to-users.js b/migrations/20200224203115-add-index-to-users.js new file mode 100644 index 00000000..70b8f791 --- /dev/null +++ b/migrations/20200224203115-add-index-to-users.js @@ -0,0 +1,22 @@ +/** + * @file Migration to add unique index for `email` field from users collection + */ +module.exports = { + async up(db) { + const collection = db.collection('users'); + + await collection.createIndex({ + email: 1, + }, { + unique: true, + }); + }, + + async down(db) { + const collection = db.collection('users'); + + await collection.dropIndex({ + email: 1, + }); + }, +}; diff --git a/package.json b/package.json index a300a99f..f79bed40 100644 --- a/package.json +++ b/package.json @@ -11,7 +11,9 @@ "dev:up": "docker-compose -f docker-compose.dev.yml up -d", "dev:down": "docker-compose -f docker-compose.dev.yml down", "build": "tsc && yarn copy-email-templates", - "copy-email-templates": "copyfiles src/email/templates/*/*.txt build" + "copy-email-templates": "copyfiles src/email/templates/*/*.txt build", + "migrations:up": "docker-compose exec api yarn migrate-mongo up", + "migrations:down": "docker-compose exec api yarn migrate-mongo down" }, "husky": { "hooks": {