1 | "use strict";
|
2 | var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
3 | if (k2 === undefined) k2 = k;
|
4 | Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
|
5 | }) : (function(o, m, k, k2) {
|
6 | if (k2 === undefined) k2 = k;
|
7 | o[k2] = m[k];
|
8 | }));
|
9 | var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
10 | Object.defineProperty(o, "default", { enumerable: true, value: v });
|
11 | }) : function(o, v) {
|
12 | o["default"] = v;
|
13 | });
|
14 | var __importStar = (this && this.__importStar) || function (mod) {
|
15 | if (mod && mod.__esModule) return mod;
|
16 | var result = {};
|
17 | if (mod != null) for (var k in mod) if (k !== "default" && Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
18 | __setModuleDefault(result, mod);
|
19 | return result;
|
20 | };
|
21 | Object.defineProperty(exports, "__esModule", { value: true });
|
22 | exports.pgStatus = exports.pgTest = exports.psqlCommand = exports.psqlExport = exports.psqlImport = void 0;
|
23 | const fs = __importStar(require("fs-extra"));
|
24 | const p_spawn_1 = require("p-spawn");
|
25 | const defaultPsqlOptions = {
|
26 | toConsole: true
|
27 | };
|
28 |
|
29 | async function psqlImport(pgOpts, filePaths) {
|
30 | pgOpts = { ...defaultPsqlOptions, ...pgOpts };
|
31 | const items = [];
|
32 | if (typeof filePaths === "string") {
|
33 | filePaths = [filePaths];
|
34 | }
|
35 | for (let file of filePaths) {
|
36 | const item = { file };
|
37 | const { stdout, stderr } = await execPsql(pgOpts, ["-f", file]);
|
38 | item.stdout = stdout;
|
39 | item.stderr = stderr;
|
40 | items.push(item);
|
41 | if (stderr) {
|
42 | const err = new Error(`psqlImport ERROR for file ${file}:\n${item.stderr}`);
|
43 | err.items = items;
|
44 | throw err;
|
45 | }
|
46 | }
|
47 | return items;
|
48 | }
|
49 | exports.psqlImport = psqlImport;
|
50 |
|
51 | async function psqlExport(pgOpts, filepath) {
|
52 | var defaultArgs = ["--no-owner", "--no-acl"];
|
53 | var cmd = "pg_dump";
|
54 | const { args, env } = buildPgArgs(pgOpts);
|
55 | args.push.apply(args, defaultArgs);
|
56 | var fStream = fs.createWriteStream(filepath, { flags: 'w' });
|
57 | console.log("will execute >> " + cmd + " " + args.join(" ") + "\n\t into " + filepath);
|
58 | await p_spawn_1.spawn(cmd, args, {
|
59 | env,
|
60 | onStdout: (data) => {
|
61 | fStream.write(data);
|
62 | }
|
63 | });
|
64 | }
|
65 | exports.psqlExport = psqlExport;
|
66 | async function psqlCommand(pgOpts, command) {
|
67 | const { stdout, stderr } = await execPsql({ ...pgOpts, toConsole: true }, [`--command=${command}`]);
|
68 | if (stderr) {
|
69 | throw Error(`ERROR - cannot psqlCommand "${command}" because ${stderr}`);
|
70 | }
|
71 | return stdout;
|
72 | }
|
73 | exports.psqlCommand = psqlCommand;
|
74 | async function pgTest(pgOpts) {
|
75 | const status = await pgStatus(pgOpts);
|
76 | if (!status.accepting) {
|
77 | return { success: false, message: status.message };
|
78 | }
|
79 |
|
80 |
|
81 | const { args, env } = buildPgArgs(pgOpts);
|
82 | args.push("--command=SELECT version()");
|
83 | const p = await p_spawn_1.spawn('psql', args, { env, capture: ['stdout', 'stderr'], ignoreFail: true });
|
84 | if (p.code === 0) {
|
85 | return { success: true, message: p.stdout.trim() };
|
86 | }
|
87 | else {
|
88 | const r = { success: false, message: p.stdout };
|
89 | if (p.stderr) {
|
90 | r.err = p.stderr.trim();
|
91 | }
|
92 | return r;
|
93 | }
|
94 | }
|
95 | exports.pgTest = pgTest;
|
96 |
|
97 | async function pgStatus(pgOpts) {
|
98 | const { args, env } = buildPgArgs(pgOpts);
|
99 |
|
100 | const p = await p_spawn_1.spawn('pg_isready', args, { env, ignoreFail: true, capture: ['stdout', 'stderr'] });
|
101 | const code = p.code;
|
102 | const message = p.stdout.trim();
|
103 | const accepting = (0 === p.code) ? true : false;
|
104 | return { accepting, message, code };
|
105 | }
|
106 | exports.pgStatus = pgStatus;
|
107 |
|
108 |
|
109 | async function execPsql(pgOpts, args) {
|
110 | const { args: baseArgs, env } = buildPgArgs(pgOpts);
|
111 |
|
112 | args = [...baseArgs, ...args];
|
113 | const spawnOptions = buildSpawnOptions(pgOpts);
|
114 | const spawnResult = await p_spawn_1.spawn('psql', args, { env, ...spawnOptions, ignoreFail: true });
|
115 | let { stdout, stderr } = spawnResult;
|
116 | if (stderr) {
|
117 | const err = stderr.trim();
|
118 | let itemErr = null;
|
119 | for (const line of err.split('\n')) {
|
120 | if (!line.includes("NOTICE:")) {
|
121 | itemErr = (itemErr == null) ? line : `${itemErr}\n${line}`;
|
122 | }
|
123 | }
|
124 | stderr = itemErr !== null && itemErr !== void 0 ? itemErr : undefined;
|
125 | }
|
126 | return { stdout: stdout !== null && stdout !== void 0 ? stdout : '', stderr: stderr !== null && stderr !== void 0 ? stderr : '' };
|
127 | }
|
128 | function buildSpawnOptions(pgOpts) {
|
129 | let spawnOptions = { capture: ['stdout', 'stderr'] };
|
130 | if (pgOpts.toConsole) {
|
131 | spawnOptions.toConsole = true;
|
132 | }
|
133 | return spawnOptions;
|
134 | }
|
135 |
|
136 | function buildPgArgs(pgOpts) {
|
137 | var args = [];
|
138 | if (pgOpts.user) {
|
139 | args.push("-U", pgOpts.user);
|
140 | }
|
141 | if (pgOpts.database) {
|
142 | args.push("-d", pgOpts.database);
|
143 | }
|
144 | if (pgOpts.host) {
|
145 | args.push("-h", pgOpts.host);
|
146 | }
|
147 | if (pgOpts.port) {
|
148 | args.push("-p", pgOpts.port);
|
149 | }
|
150 | const env = (pgOpts.password == null) ? process.env : { ...process.env, PGPASSWORD: pgOpts.password };
|
151 | return { args, env };
|
152 | }
|
153 |
|