-
Notifications
You must be signed in to change notification settings - Fork 163
/
pgutil.js
226 lines (212 loc) · 6.01 KB
/
pgutil.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
const pg = require('pg')
const when = require('when')
const util = require('util')
let pool
const initPG = () => {
const pgUrl = process.env.DATABASE_URL
console.log('pgUrl', pgUrl)
pool = new pg.Pool({
connectionString: pgUrl,
ssl: { rejectUnauthorized: false }
})
return pool
}
const createTable = async () => {
if (!pool) throw new Error('No PG instance')
console.log('create pg tables')
const query = `
CREATE TABLE IF NOT EXISTS "eConfigs" (
id SERIAL PRIMARY KEY,
appname character varying(255) NOT NULL,
flows text,
credentials text,
packages text,
settings text,
"secureLink" text
);
CREATE TABLE IF NOT EXISTS "eLibs" (
id SERIAL PRIMARY KEY,
appname character varying(255) NOT NULL,
type text,
path text,
meta text,
body text
);
CREATE TABLE IF NOT EXISTS "ePrivateNodes" (
id SERIAL PRIMARY KEY,
appname character varying(255) NOT NULL,
"packageName" text,
data text
);
`
await doSQL(query, null)
}
const doSQL = async (query, values) => {
let client
try {
if (!pool) throw new Error('No PG instance')
client = await pool.connect()
return await client.query(query, values)
} finally {
if (client) {
client.release()
}
}
}
const loadConfig = async (appname) => {
const query = 'SELECT * FROM "eConfigs" WHERE appname = $1'
const data = await doSQL(query, [JSON.stringify(appname)])
if (data && data.rowCount > 0) {
let retData = data.rows[0]
for (let key in retData) {
if (retData[key]) {
retData[key] = JSON.parse(retData[key])
}
}
return retData
}
return null
}
const saveConfig = async (appname, params) => {
const columns = [
'appname',
'flows',
'credentials',
'packages',
'settings',
'secureLink',
'id'
]
let data = await loadConfig(appname)
let query
let values
if (data) {
data = Object.assign(data, params)
query =
'UPDATE "eConfigs" SET appname = $1, flows = $2, credentials = $3, packages = $4, settings = $5, "secureLink" = $6 WHERE id = $7 RETURNING *'
values = columns.map((c) => (data[c] ? JSON.stringify(data[c]) : ''))
} else {
data = params
query =
'INSERT INTO "eConfigs"(appname, flows, credentials, packages, settings, "secureLink") VALUES($1, $2, $3, $4, $5, $6) RETURNING *'
values = columns
.slice(0, 6)
.map((c) => (data[c] ? JSON.stringify(data[c]) : ''))
}
await doSQL(query, values)
}
const removeConfig = async (appname) => {
const query = 'DELETE FROM "eConfigs" WHERE appname = $1'
await doSQL(query, [JSON.stringify(appname)])
}
const loadLib = async (appname, type, path) => {
const query =
'SELECT * FROM "eLibs" WHERE appname = $1 and type = $2 and path = $3'
const data = await doSQL(query, [
JSON.stringify(appname),
JSON.stringify(type),
JSON.stringify(path)
])
if (data && data.rowCount > 0) {
let retData = data.rows[0]
for (let key in retData) {
if (retData[key]) {
retData[key] = JSON.parse(retData[key])
}
}
return retData
}
return null
}
const loadLibList = async (appname, type, dir) => {
const query =
'SELECT * FROM "eLibs" WHERE appname = $1 and type = $2 and path LIKE $3 ORDER BY path'
const data = await doSQL(query, [
JSON.stringify(appname),
JSON.stringify(type),
`"${dir}%`
])
let retDataList = data.rows.map((d) => {
let retData = {}
for (let key in d) {
if (d[key]) {
retData[key] = JSON.parse(d[key])
}
}
return retData
})
return retDataList
}
const saveLib = async (appname, params) => {
const columns = ['appname', 'type', 'path', 'meta', 'body', 'id']
let data = await loadLib(appname, params.type, params.path)
let query
let values
if (data) {
data = Object.assign(data, params)
query =
'UPDATE "eLibs" SET appname = $1, type = $2, path = $3, meta = $4, body = $5 WHERE id = $6 RETURNING *'
values = columns.map((c) => (data[c] ? JSON.stringify(data[c]) : ''))
} else {
data = params
query =
'INSERT INTO "eLibs"(appname, type, path, meta, body) VALUES($1, $2, $3, $4, $5) RETURNING *'
values = columns
.slice(0, 5)
.map((c) => (data[c] ? JSON.stringify(data[c]) : ''))
}
await doSQL(query, values)
}
const loadPrivateNodes = async (appname, packageName) => {
const query =
'SELECT * FROM "ePrivateNodes" WHERE appname = $1 and "packageName" = $2'
const data = await doSQL(query, [
JSON.stringify(appname),
JSON.stringify(packageName)
])
if (data && data.rowCount > 0) {
let retData = data.rows[0]
for (let key in retData) {
if (retData[key]) {
retData[key] = JSON.parse(retData[key])
}
}
return retData
}
return null
}
const savePrivateNodes = async (appname, params) => {
const columns = ['appname', 'packageName', 'data', 'id']
let data = await loadPrivateNodes(appname, params.packageName)
let query
let values
if (data) {
data = Object.assign(data, params)
query =
'UPDATE "ePrivateNodes" SET appname = $1, "packageName" = $2, data = $3 WHERE id = $4 RETURNING *'
values = columns.map((c) => (data[c] ? JSON.stringify(data[c]) : ''))
} else {
data = params
query =
'INSERT INTO "ePrivateNodes"(appname, "packageName", data) VALUES($1, $2, $3) RETURNING *'
values = columns
.slice(0, 3)
.map((c) => (data[c] ? JSON.stringify(data[c]) : ''))
}
await doSQL(query, values)
}
const removePrivateNodes = async (appname) => {
const query = 'DELETE FROM "ePrivateNodes" WHERE appname = $1'
await doSQL(query, [JSON.stringify(appname)])
}
exports.initPG = initPG
exports.createTable = createTable
exports.loadConfig = loadConfig
exports.saveConfig = saveConfig
exports.removeConfig = removeConfig
exports.loadLib = loadLib
exports.loadLibList = loadLibList
exports.saveLib = saveLib
exports.loadPrivateNodes = loadPrivateNodes
exports.savePrivateNodes = savePrivateNodes
exports.removePrivateNodes = removePrivateNodes