UNPKG

agnostic-sql-migrator

Version:

Database agnostic migrator tool for SQL databases

93 lines 3.92 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.migrator = exports.createConfig = void 0; const migrations_1 = require("./migrations"); const adapters_1 = require("./adapters"); const path_1 = require("path"); const createConfig = (defaultVersion) => { const args = process.argv.slice(2); const argMap = new Map(); args.map((arg) => { const keyVal = arg.split('='); argMap.set(keyVal[0], keyVal[1]); }); const adapter = argMap.get('ADAPTER') ? argMap.get('ADAPTER').toLowerCase() : process.env.ADAPTER ? process.env.ADAPTER.toLowerCase() : undefined; const user = argMap.get('USER') ? argMap.get('USER') : process.env.USER || ''; const password = argMap.get('PASSWORD') ? argMap.get('PASSWORD') : process.env.PASSWORD ? process.env.PASSWORD : undefined; const host = argMap.get('HOST') ? argMap.get('HOST') : process.env.HOST ? process.env.HOST : undefined; const port = argMap.get('PORT') ? Number.parseInt(argMap.get('PORT')) : process.env.PORT ? Number.parseInt(process.env.PORT) : undefined; const database = argMap.get('DATABASE') ? argMap.get('DATABASE') : process.env.DATABASE ? process.env.DATABASE : undefined; const version = argMap.get('VERSION') ? Number.parseInt(argMap.get('VERSION')) : defaultVersion ? defaultVersion : undefined; const migrationsPath = argMap.get('MIGRATIONS_PATH') ? path_1.resolve(argMap.get('MIGRATIONS_PATH')) : process.env.MIGRATIONS_PATH ? path_1.resolve(process.env.MIGRATIONS_PATH) : undefined; return { ClientConfig: { user, password, host, port, database }, MigrationConfig: { adapter, version, migrationsPath }, }; }; exports.createConfig = createConfig; const migrator = (userConfig) => __awaiter(void 0, void 0, void 0, function* () { let config = exports.createConfig(); config = userConfig ? { ClientConfig: Object.assign(Object.assign({}, config.ClientConfig), userConfig.ClientConfig), MigrationConfig: Object.assign(Object.assign({}, config.MigrationConfig), userConfig.MigrationConfig), } : { ClientConfig: config.ClientConfig, MigrationConfig: config.MigrationConfig, }; const migrationFiles = migrations_1.getMigrationFiles(config); if (!config.MigrationConfig.version) { Object.assign(config.MigrationConfig, { version: migrationFiles.RollForward[migrationFiles.RollForward.length - 1] .VersionTo, }); } const adapter = yield adapters_1.adapters(config.MigrationConfig.adapter); const client = yield adapter.createClient(config.ClientConfig); try { yield migrations_1.migrateDb(client, adapter, config.MigrationConfig.version, migrationFiles, config.MigrationConfig.adapter); } finally { yield adapter.closeConnection(client); } }); exports.migrator = migrator; //# sourceMappingURL=config.js.map