Skip to content

Configuration

Avoid repeating --db-connection-uri and other flags on every command by using one of these approaches.

Options Reference

OptionCLI flagEnvironment variableConfig file keyDefault
MongoDB connection URI-d, --db-connection-uriMIGRATE_DB_CONNECTION_URIdbConnectionUri(required)
Migrations directory--md, --migrations-dirMIGRATE_MIGRATIONS_DIRmigrationsDir./migrations
State collection name--collectionMIGRATE_COLLECTIONcollectionmigrations
Migration template file-t, --template-fileMIGRATE_TEMPLATE_FILEtemplateFile
TypeScript migration files--typescriptMIGRATE_TYPESCRIPTtypescriptfalse
Change working directory-c, --change-dirMIGRATE_CHANGE_DIRchangeDir
Config file path--configmigrate.json

Environment Variables

Prefix the uppercase underscore-separated option name with MIGRATE_:

sh
export MIGRATE_DB_CONNECTION_URI=mongodb://localhost:27017/mydb

.env files are supported — variables are loaded automatically:

sh
# .env
MIGRATE_DB_CONNECTION_URI=mongodb://localhost:27017/mydb
MIGRATE_MIGRATIONS_DIR=./migrations

Config File

By default, @eventonehq/migrate-mongoose looks for migrate.json in the current directory:

json
{
  "dbConnectionUri": "mongodb://localhost:27017/mydb",
  "migrationsDir": "./migrations"
}

To use a custom path:

sh
npx migrate list --config path/to/myconfig.json

Override Order

CLI flags  >  Environment variables  >  Config file

Security

WARNING

Never hardcode connection URIs. Store MIGRATE_DB_CONNECTION_URI in environment variables or a config file excluded from version control.

  • Add sensitive config files to .gitignore. If migrate.json or .env contains a real connection URI, exclude it before committing.
  • Treat migration files as code. They are executed directly by Node.js with full database access. Review them with the same scrutiny as application code.
  • Limit database user permissions. The MongoDB user should have only the permissions needed to run migrations — avoid admin credentials.