Skip to content

Commit

Permalink
feat: Integrate migrate-mongo library for automatic db migrations
Browse files Browse the repository at this point in the history
* On application start, uses migrate-mongo to check for and execute any pending db migrations.
* Added `rollback-migrations` utility script to simplify rolling back db migrations.
  • Loading branch information
jrassa committed May 1, 2024
1 parent 6432bf8 commit a733687
Show file tree
Hide file tree
Showing 13 changed files with 590 additions and 23 deletions.
3 changes: 3 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
"plugins": ["deprecation", "jsdoc", "prettier"],

"extends": ["eslint:recommended"],

"ignorePatterns": ["src/migrations/sample-migration.ts"],

"rules": {
"arrow-parens": ["error", "always"],
"eqeqeq": ["error", "smart"],
Expand Down
20 changes: 20 additions & 0 deletions config/default.js
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,26 @@ module.exports = {
},
mongooseFailOnIndexOptionsConflict: true,

migrateMongo: {
enabled: true,

// The migrations dir, can be an relative or absolute path. Only edit this when really necessary.
migrationsDir: 'src/migrations',

// The mongodb collection where the applied changes are stored. Only edit this when really necessary.
changelogCollectionName: 'changelog',

// The file extension to create migrations and search for in migration dir
migrationFileExtension: '.js',

// Enable the algorithm to create a checksum of the file contents and use that in the comparison to determine
// if the file should be run. Requires that scripts are coded to be run multiple times.
useFileHash: false,

// Don't change this, unless you know what you're doing
moduleSystem: 'commonjs'
},

/*
* The maximum time in milliseconds allowed for processing operation on the cursor by a mongo query
*/
Expand Down
5 changes: 5 additions & 0 deletions config/development.template.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ module.exports = {
},
mongooseFailOnIndexOptionsConflict: false,

migrateMongo: {
enabled: false,
migrationFileExtension: '.ts'
},

/**
* Environment Settings
*/
Expand Down
36 changes: 36 additions & 0 deletions migrate-mongo-config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// In this file you can configure migrate-mongo

const config = {
mongodb: {
// TODO Change (or review) the url to your MongoDB:
url: 'mongodb://localhost:27017',

// TODO Change this to your database name:
databaseName: 'YOURDATABASENAME',

options: {
useNewUrlParser: true, // removes a deprecation warning when connecting
useUnifiedTopology: true // removes a deprecating warning when connecting
// connectTimeoutMS: 3600000, // increase connection timeout to 1 hour
// socketTimeoutMS: 3600000, // increase socket timeout to 1 hour
}
},

// The migrations dir, can be an relative or absolute path. Only edit this when really necessary.
migrationsDir: 'src/migrations',

// The mongodb collection where the applied changes are stored. Only edit this when really necessary.
changelogCollectionName: 'changelog',

// The file extension to create migrations and search for in migration dir
migrationFileExtension: '.ts',

// Enable the algorithm to create a checksum of the file contents and use that in the comparison to determine
// if the file should be run. Requires that scripts are coded to be run multiple times.
useFileHash: false,

// Don't change this, unless you know what you're doing
moduleSystem: 'commonjs'
};

module.exports = config;
Loading

0 comments on commit a733687

Please sign in to comment.