Skip to content

Configuration

mongo-migrate-kit resolves configuration from four sources, in priority order:

CLI flags  >  Environment variables  >  Config file  >  Defaults

A config file is never required — env vars alone are always sufficient.

Config file

On startup, mmk looks in the current working directory for the first of:

  1. mmk.config.ts
  2. mmk.config.js
  3. mmk.config.json

Generate one with mmk init. Override discovery with --config <path>.

js
export default {
  uri: process.env.MMK_URI ?? 'mongodb://localhost:27017',
  dbName: 'my_app',
  migrationsDir: './migrations',
  migrationsCollection: '_mmk_migrations',
  strict: false,
  useTransaction: false,
  createExtension: 'js',
};
ts
import type { MmkConfig } from 'mongo-migrate-kit';

const config: Partial<MmkConfig> = {
  uri: process.env.MMK_URI ?? 'mongodb://localhost:27017',
  dbName: 'my_app',
  migrationsDir: './migrations',
  createExtension: 'ts',
};

export default config;
json
{
  "uri": "mongodb://localhost:27017",
  "dbName": "my_app",
  "migrationsDir": "./migrations"
}

Async / factory config (secret managers)

A .ts/.js config may export default a function (sync or async) that returns the config. This is the dependency-free way to load a connection from a secret manager at runtime — the library ships no cloud SDKs, you bring your own inside the function:

ts
import type { MmkConfigInput } from 'mongo-migrate-kit';

const loadConfig: MmkConfigInput = async () => {
  const { uri, dbName } = await fetchFromSecretsManager(); // your code
  return { uri, dbName, migrationsDir: './migrations' };
};

export default loadConfig;

Generate a ready-made AWS Secrets Manager template with mmk init --secret-provider (swap the body for Google/Vault/Azure/any source — it just must return { uri, dbName }).

All options

OptionTypeDefaultDescription
uristringMongoDB connection URI (required)
dbNamestringDatabase name (required)
migrationsDirstring'./migrations'Directory holding migration files
migrationsCollectionstring'_mmk_migrations'Collection storing the changelog
lockCollectionstring'_mmk_locks'Collection used for the concurrency lock
lockTTLSecondsnumber60Seconds before a lock is considered stale
strictbooleanfalseAbort (vs. warn) on a checksum mismatch
useTransactionbooleanfalseWrap every migration in a transaction globally
fileExtensionsstring[]['.ts', '.js']Extensions scanned in the migrations dir
createExtension'ts' | 'js''js'Default file type for mmk create
sequentialbooleanfalseUse 0001- numbering instead of timestamps
templatePathstringPath to a custom migration template
mongooseMongooseMongoose instance, if your migrations use it
hooksMigrationHooksLifecycle hooks
loggerMmkLogger | nullbuilt-inCustom logger; null silences all output

Environment variables

Every core option has an MMK_* variable. These override the config file:

Env varMaps to
MMK_URIuri
MMK_DBdbName
MMK_MIGRATIONS_DIRmigrationsDir
MMK_COLLECTIONmigrationsCollection
MMK_LOCK_COLLECTIONlockCollection
MMK_LOCK_TTLlockTTLSeconds
MMK_STRICTstrict
MMK_USE_TRANSACTIONuseTransaction
MMK_SEQUENTIALsequential
MMK_CREATE_EXTENSIONcreateExtension

.env files are loaded automatically (via dotenv) before env vars are read.

bash
# .env
MMK_URI=mongodb://localhost:27017
MMK_DB=my_app

Global CLI flags

These flags work on every command and have the highest precedence:

FlagOverrides
--uri <uri>MMK_URI / uri
--db <name>MMK_DB / dbName
--dir <path>MMK_MIGRATIONS_DIR / migrationsDir
--config <path>Config file auto-discovery
bash
mmk up --uri "mongodb://localhost:27017" --db my_app --dir ./db/migrations

Released under the MIT License.