pg-dump-restore
Version:
Nodejs wrapper around the pg_dump and pg_restore
88 lines (87 loc) • 4.12 kB
JavaScript
;
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());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.pgRestore = void 0;
const execa_1 = __importDefault(require("execa"));
const common_1 = require("./common");
const pgRestore = (connectionOptions, restoreOptions) => __awaiter(void 0, void 0, void 0, function* () {
const { filePath, dataOnly, clean, create, exitOnError, format, fileName, index, jobs, list, useListFile, schemaPattern, excludeSchemaPattern, noOwner, functionName, noReconnect, schemaOnly, superUser, table, trigger, verbose, version, noAcl, singleTransaction, disableTriggers, enableRowSecurity, ifExists, noComments, noDataForFailedTables, noPublications, noSecurityLabels, noSubscriptions, noTableSpaces, sectionName, strictNames, useSetSessionAuthorization, noPassword, roleName, noPrivileges, } = restoreOptions;
if (!filePath)
throw new Error('Needs filePath in the options');
const os = process.platform;
const { connectionArgs, sslMode } = (0, common_1.getConnectionArgs)(connectionOptions);
if (format)
connectionArgs.push(`--format=${format}`);
if (fileName)
connectionArgs.push(`--filename=${fileName}`);
if (index)
connectionArgs.push(`--index=${index}`);
if (jobs)
connectionArgs.push(`--jobs=${jobs}`);
if (useListFile)
connectionArgs.push(`--use-list=${useListFile}`);
if (schemaPattern)
connectionArgs.push(`--schema=${schemaPattern}`);
if (excludeSchemaPattern)
connectionArgs.push(`--exclude-schema=${excludeSchemaPattern}`);
if (functionName)
connectionArgs.push(`--function=${functionName}`);
if (superUser)
connectionArgs.push(`--superuser=${superUser}`);
if (table)
connectionArgs.push(`--table=${table}`);
if (trigger)
connectionArgs.push(`--trigger=${trigger}`);
if (sectionName)
connectionArgs.push(`--section=${sectionName}`);
if (roleName)
connectionArgs.push(`--role=${roleName}`);
const paramsMap = {
clean,
create,
list,
verbose,
version,
'data-only': dataOnly,
'disable-triggers': disableTriggers,
'enable-row-security': enableRowSecurity,
'exit-on-error': exitOnError,
'if-exists': ifExists,
'no-acl': noAcl,
'no-comments': noComments,
'no-data-for-failed-tables': noDataForFailedTables,
'no-owner': noOwner,
'no-password': noPassword,
'no-publications': noPublications,
'no-reconnect': noReconnect,
'no-security-labels': noSecurityLabels,
'no-subscriptions': noSubscriptions,
'no-tablespaces': noTableSpaces,
'no-privileges': noPrivileges,
'schema-only': schemaOnly,
'single-transaction': singleTransaction,
'strict-names': strictNames,
'use-set-session-authorization': useSetSessionAuthorization,
};
Object.keys(paramsMap).forEach((key) => {
if (paramsMap[key])
connectionArgs.push(`--${key}`);
});
connectionArgs.push(filePath);
const env = {};
if (sslMode)
env.PGSSLMODE = sslMode;
return (0, execa_1.default)(os == 'win32' ? 'pg_restore.exe' : 'pg_restore', connectionArgs, { env });
});
exports.pgRestore = pgRestore;