1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | const fs = require("fs-extra-plus");
|
4 | const p_spawn_1 = require("p-spawn");
|
5 | const defaultPsqlOptions = {
|
6 | toConsole: true
|
7 | };
|
8 |
|
9 | async 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 |
|
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 | }
|
40 | exports.psqlImport = psqlImport;
|
41 |
|
42 | async 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 | }
|
59 | exports.psqlExport = psqlExport;
|
60 | async function pgTest(pgOpts) {
|
61 | const status = await pgStatus(pgOpts);
|
62 | if (!status.accepting) {
|
63 | return { success: false, message: status.message };
|
64 | }
|
65 |
|
66 |
|
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 | }
|
81 | exports.pgTest = pgTest;
|
82 |
|
83 | async function pgStatus(pgOpts) {
|
84 | var args = buildPgArgs(pgOpts);
|
85 |
|
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 | }
|
92 | exports.pgStatus = pgStatus;
|
93 |
|
94 |
|
95 | function buildSpawnOptions(pgOpts) {
|
96 | let spawnOptions = { capture: ['stdout', 'stderr'] };
|
97 | if (pgOpts.toConsole) {
|
98 | spawnOptions.toConsole = true;
|
99 | }
|
100 | return spawnOptions;
|
101 | }
|
102 |
|
103 | function 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 |
|