UNPKG

4 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const fs = require("fs-extra-plus");
4const p_spawn_1 = require("p-spawn");
5const defaultPsqlOptions = {
6 toConsole: true
7};
8// NOTE: for now, ignore pgOpts.pwd
9async function psqlImport(pgOpts, filePaths) {
10 pgOpts = Object.assign(Object.assign({}, defaultPsqlOptions), pgOpts);
11 const items = [];
12 if (typeof filePaths === "string") {
13 filePaths = [filePaths];
14 }
15 var baseArgs = buildPgArgs(pgOpts);
16 var cmd = "psql";
17 // TODO: add the password env var.
18 for (let file of filePaths) {
19 let args = baseArgs.slice(0);
20 args.push("-f", file);
21 if (pgOpts.toConsole) {
22 console.log("will execute >> " + cmd + " " + args.join(" "));
23 }
24 const spawnOptions = buildSpawnOptions(pgOpts);
25 const p = await p_spawn_1.spawn(cmd, args, spawnOptions);
26 const item = { file };
27 item.stdout = p.stdout;
28 if (p.stderr) {
29 item.stderr = p.stderr.trim();
30 }
31 items.push(item);
32 if (item.stderr) {
33 const err = new Error(`psqlImport ERROR for file ${file}:\n${item.stderr}`);
34 err.items = items;
35 throw err;
36 }
37 }
38 return items;
39}
40exports.psqlImport = psqlImport;
41// pgdump with no-owner, no-acl
42async function psqlExport(pgOpts, filepath) {
43 var defaultArgs = ["--no-owner", "--no-acl"];
44 var baseArgs = buildPgArgs(pgOpts);
45 var args = [];
46 var cmd = "pg_dump";
47 args = defaultArgs.concat(baseArgs);
48 var fStream = fs.createWriteStream(filepath, { flags: 'w' });
49 console.log("will execute >> " + cmd + " " + args.join(" ") + "\n\t into " + filepath);
50 var env = Object.assign({}, process.env);
51 env.PGPASSWORD = pgOpts.pwd;
52 await p_spawn_1.spawn(cmd, args, {
53 env,
54 onStdout: (data) => {
55 fStream.write(data);
56 }
57 });
58}
59exports.psqlExport = psqlExport;
60async function pgTest(pgOpts) {
61 const status = await pgStatus(pgOpts);
62 if (!status.accepting) {
63 return { success: false, message: status.message };
64 }
65 // --command="SELECT version();
66 // var args = buildPgArgs(pgOpts);
67 var args = buildPgArgs(pgOpts);
68 args.push("--command=SELECT version()");
69 const p = await p_spawn_1.spawn('psql', args, { capture: ['stdout', 'stderr'], ignoreFail: true });
70 if (p.code === 0) {
71 return { success: true, message: p.stdout.trim() };
72 }
73 else {
74 const r = { success: false, message: p.stdout };
75 if (p.stderr) {
76 r.err = p.stderr.trim();
77 }
78 return r;
79 }
80}
81exports.pgTest = pgTest;
82/** Return the status of a pg database process (without the database) */
83async function pgStatus(pgOpts) {
84 var args = buildPgArgs(pgOpts);
85 //args.push('-q'); // for the quiet mode, we just need to result code
86 const p = await p_spawn_1.spawn('pg_isready', args, { ignoreFail: true, capture: ['stdout', 'stderr'] });
87 const code = p.code;
88 const message = p.stdout.trim();
89 const accepting = (0 === p.code) ? true : false;
90 return { accepting, message, code };
91}
92exports.pgStatus = pgStatus;
93// --------- /Utils public API --------- //
94// --------- Private Utils --------- //
95function buildSpawnOptions(pgOpts) {
96 let spawnOptions = { capture: ['stdout', 'stderr'] };
97 if (pgOpts.toConsole) {
98 spawnOptions.toConsole = true;
99 }
100 return spawnOptions;
101}
102// private: Build a cmd line argument list from a pgOpts {user, db[, pwd][, host][, port: 5432]} and make it an command line arguments
103function buildPgArgs(pgOpts) {
104 var cmdArgs = [];
105 if (pgOpts.user) {
106 cmdArgs.push("-U", pgOpts.user);
107 }
108 if (pgOpts.db) {
109 cmdArgs.push("-d", pgOpts.db);
110 }
111 if (pgOpts.host) {
112 cmdArgs.push("-h", pgOpts.host);
113 }
114 if (pgOpts.port) {
115 cmdArgs.push("-p", pgOpts.port);
116 }
117 return cmdArgs;
118}
119// --------- /Private Utils --------- //