Skip to content

Programmatic API

Alongside command line, you can use node-pg-migrate also programmatically. It exports runner function, which takes options argument with the following structure (similar to command line arguments):

Options

NOTE

If you use dbClient, you should not use databaseUrl at the same time and vice versa.

OptionTypeDescription
databaseUrlstring or objectConnection string or client config which is passed to new pg.Client
dbClientpg.ClientInstance of new pg.Client. Instance should be connected to DB, and after finishing migration, user is responsible to close connection
migrationsTablestringThe table storing which migrations have been run
migrationsSchemastringThe schema storing table which migrations have been run (defaults to same value as schema)
schemastring or array[string]The schema on which migration will be run (defaults to public)
dirstring or array[string]The directory containing your migration files. This path is resolved from cwd(). Alternatively, provide a glob pattern or an array of glob patterns and set useGlob = true. Note: enabling glob will read both, dir and ignorePattern as glob patterns
useGlobbooleanUse glob to find migration files. This will use dir and ignorePattern to glob-search for migration files. Note: enabling glob will read both, dir and ignorePattern as glob patterns
checkOrderbooleanCheck order of migrations before running them
directionenumup or down
countnumberAmount of migration to run
timestampbooleanTreats count as timestamp
ignorePatternstring or array[string]Regex pattern for file names to ignore (ignores files starting with . by default). Alternatively, provide a glob pattern or an array of glob patterns and set isGlob = true. Note: enabling glob will read both, dir and ignorePattern as glob patterns
filestringRun-only migration with this name
singleTransactionbooleanCombines all pending migrations into a single transaction so that if any migration fails, all will be rolled back (defaults to true)
createSchemabooleanCreates the configured schema if it doesn't exist
createMigrationsSchemabooleanCreates the configured migration schema if it doesn't exist
noLockbooleanDisables locking mechanism and checks
fakebooleanMark migrations as run without actually performing them (use with caution!)
dryRunboolean
logfunctionRedirect log messages to this function, rather than console
loggerobject with debug/info/warn/error methodsRedirect messages to this logger object, rather than console
verbosebooleanPrint all debug messages like DB queries run (if you switch it on, it will disable logger.debug method)
decamelizebooleanRuns decamelize on table/column/etc. names