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.
| Option | Type | Description |
|---|---|---|
databaseUrl | string or object | Connection string or client config which is passed to new pg.Client |
dbClient | pg.Client | Instance of new pg.Client. Instance should be connected to DB, and after finishing migration, user is responsible to close connection |
migrationsTable | string | The table storing which migrations have been run |
migrationsSchema | string | The schema storing table which migrations have been run (defaults to same value as schema) |
schema | string or array[string] | The schema on which migration will be run (defaults to public) |
dir | string 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 |
useGlob | boolean | Use 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 |
checkOrder | boolean | Check order of migrations before running them |
direction | enum | up or down |
count | number | Amount of migration to run |
timestamp | boolean | Treats count as timestamp |
ignorePattern | string 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 |
file | string | Run-only migration with this name |
singleTransaction | boolean | Combines all pending migrations into a single transaction so that if any migration fails, all will be rolled back (defaults to true) |
createSchema | boolean | Creates the configured schema if it doesn't exist |
createMigrationsSchema | boolean | Creates the configured migration schema if it doesn't exist |
noLock | boolean | Disables locking mechanism and checks |
lockValue | number | Value to use for the lock |
fake | boolean | Mark migrations as run without actually performing them (use with caution!) |
dryRun | boolean | |
log | function | Redirect log messages to this function, rather than console |
logger | object with debug/info/warn/error methods | Redirect messages to this logger object, rather than console |
verbose | boolean | Print all debug messages like DB queries run (if you switch it on, it will disable logger.debug method) |
decamelize | boolean | Runs decamelize on table/column/etc. names |
migrationLoaderStrategies | MigrationLoaderStrategy[] | Allows custom loading strategies based on file extensions. If omitted, default behavior is used. See Migration Loading Strategies. |
MigrationLoaderStrategy
ts
export interface MigrationLoaderStrategy {
// File extensions handled by this strategy.
extensions: string[];
/**
* Loader that handles conversion of file paths to migration units.
*
* @param filePaths - The file paths to load migrations from.
* @returns The migration units.
*/
loader: MigrationLoader | 'default' | 'legacySql' | 'sql';
}MigrationUnit
ts
export interface MigrationUnit {
// The unique identifier for the migration unit. Represents the significant part of the file name used for tracking which migrations have been performed.
id: string;
// File paths that are part of the migration unit.
filePaths: string[];
// The migration builder actions that are contained within the migration files.
actions: MigrationBuilderActions;
}