zet-lib 1.0.61 → 1.0.63
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/connection.js +240 -233
- package/lib/zRoute.js +8 -6
- package/package.json +1 -1
package/lib/connection.js
CHANGED
|
@@ -1,247 +1,255 @@
|
|
|
1
|
-
const {Pool} = require('pg')
|
|
2
|
-
const config = require('dotenv').config()
|
|
3
|
-
const Util = require('./Util')
|
|
1
|
+
const { Pool } = require('pg')
|
|
2
|
+
const config = require('dotenv').config()
|
|
3
|
+
const Util = require('./Util')
|
|
4
4
|
|
|
5
5
|
const pool = new Pool({
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
})
|
|
6
|
+
user: process.env.PGUSER,
|
|
7
|
+
host: process.env.PGHOST,
|
|
8
|
+
database: process.env.PGDATABASE,
|
|
9
|
+
password: process.env.PGPASSWORD,
|
|
10
|
+
port: process.env.PGPORT,
|
|
11
|
+
})
|
|
12
12
|
|
|
13
|
+
const connection = {}
|
|
13
14
|
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
try {
|
|
18
|
-
/*console.log(string);
|
|
15
|
+
connection.query = async (string, arr) => {
|
|
16
|
+
try {
|
|
17
|
+
/*console.log(string);
|
|
19
18
|
console.log(JSON.stringify(arr))*/
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
}
|
|
19
|
+
const result = await pool.query(string, arr)
|
|
20
|
+
return result.rows
|
|
21
|
+
} catch (e) {
|
|
22
|
+
console.log(string)
|
|
23
|
+
console.log(e)
|
|
24
|
+
}
|
|
25
|
+
}
|
|
27
26
|
|
|
28
27
|
const orderByFn = (obj) => {
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
}
|
|
43
|
-
}
|
|
28
|
+
const objOrderby = !obj.orderBy ? [] : obj.orderBy
|
|
29
|
+
let orderBy = ''
|
|
30
|
+
if (objOrderby.length) {
|
|
31
|
+
orderBy = ` ORDER BY `
|
|
32
|
+
for (var i = 0; i < objOrderby.length; i++) {
|
|
33
|
+
if (i % 2 == 0) {
|
|
34
|
+
orderBy += ` "${obj.orderBy[i]}" `
|
|
35
|
+
} else {
|
|
36
|
+
orderBy += ` ${obj.orderBy[i]} `
|
|
37
|
+
if (i == objOrderby.length - 1) {
|
|
38
|
+
orderBy += ` `
|
|
39
|
+
} else {
|
|
40
|
+
orderBy += `, `
|
|
44
41
|
}
|
|
42
|
+
}
|
|
45
43
|
}
|
|
44
|
+
}
|
|
46
45
|
|
|
47
|
-
|
|
48
|
-
}
|
|
46
|
+
return orderBy
|
|
47
|
+
}
|
|
49
48
|
|
|
50
49
|
const whereFn = (obj) => {
|
|
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
|
-
//console.log(arr)
|
|
85
|
-
}
|
|
86
|
-
const wheres = arr.length ? `WHERE ${wherequery}` : "";
|
|
87
|
-
return {
|
|
88
|
-
where : wheres,
|
|
89
|
-
arr : arr,
|
|
90
|
-
increment : increment
|
|
91
|
-
};
|
|
92
|
-
};
|
|
50
|
+
const where = obj.where || {}
|
|
51
|
+
//[{field:"your_field",option:"=>",value:"12",operator:"AND",isJSON : false}]
|
|
52
|
+
const whereArray = obj.whereArray || []
|
|
53
|
+
let increment = 1
|
|
54
|
+
let arr = [],
|
|
55
|
+
wherequery = []
|
|
56
|
+
for (const key in where) {
|
|
57
|
+
wherequery.push(key.indexOf('.') > -1 ? ` ${key} = $${increment} ` : ` "${key}" = $${increment}`)
|
|
58
|
+
arr.push(where[key])
|
|
59
|
+
increment++
|
|
60
|
+
}
|
|
61
|
+
wherequery = arr.length ? wherequery.join(' AND ') : ''
|
|
62
|
+
if (whereArray.length) {
|
|
63
|
+
let andOr = wherequery ? ' AND ' : ''
|
|
64
|
+
whereArray.forEach((item, index) => {
|
|
65
|
+
if (index > 0) andOr = ''
|
|
66
|
+
let operator = !item.operator ? ' AND ' : item.operator
|
|
67
|
+
if (index == whereArray.length - 1) operator = ''
|
|
68
|
+
const field = item.field.indexOf('.') > -1 ? item.field : ` "${item.field}" `
|
|
69
|
+
//is JSON is field is JSON
|
|
70
|
+
//JSON_CONTAINS(color, '"Red"' ,'$')
|
|
71
|
+
if (item.isJSON) {
|
|
72
|
+
wherequery += andOr + ` JSON_CONTAINS(${field}, '"${item.value}"' ,'$') ${operator}`
|
|
73
|
+
} else {
|
|
74
|
+
if (item.option.includes('{{value}}')) {
|
|
75
|
+
item.option = item.option.replace('{{value}}', `$${increment}`)
|
|
76
|
+
wherequery += `${andOr} ${field} ${item.option} ${operator}`
|
|
77
|
+
} else if (item.option.includes('$')) {
|
|
78
|
+
wherequery += `${andOr} ${field} ${item.option} ${operator}`
|
|
79
|
+
} else {
|
|
80
|
+
wherequery += `${andOr} ${field} ${item.option} $${increment} ${operator}`
|
|
81
|
+
}
|
|
93
82
|
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
83
|
+
let itemValue = item.value
|
|
84
|
+
if (item.option == '=') {
|
|
85
|
+
itemValue = Util.replaceAll(itemValue, '%', '')
|
|
86
|
+
}
|
|
87
|
+
arr.push(itemValue)
|
|
88
|
+
}
|
|
89
|
+
increment++
|
|
90
|
+
})
|
|
91
|
+
//console.log(arr)
|
|
92
|
+
}
|
|
93
|
+
const wheres = arr.length ? `WHERE ${wherequery}` : ''
|
|
94
|
+
return {
|
|
95
|
+
where: wheres,
|
|
96
|
+
arr: arr,
|
|
97
|
+
increment: increment,
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
connection.results = async (obj) => {
|
|
102
|
+
const select = obj.select || '*'
|
|
103
|
+
const table = obj.table || ''
|
|
104
|
+
//[{field:"your_field",option:"=>",value:"12",operator:"AND",isJSON : false}]
|
|
105
|
+
const statement = obj.statement || ''
|
|
106
|
+
const limit = obj.limit ? ` LIMIT ${obj.limit} ` : ''
|
|
107
|
+
const offset = obj.hasOwnProperty('offset') ? ` OFFSET ${obj.offset} ` : obj.limit ? 'OFFSET 0' : ''
|
|
108
|
+
const orderBy = orderByFn(obj)
|
|
109
|
+
const values = obj.values || []
|
|
110
|
+
const objJoin = obj.joins || []
|
|
111
|
+
let join = ''
|
|
112
|
+
if (objJoin.length) {
|
|
113
|
+
join = objJoin.join(' ')
|
|
114
|
+
}
|
|
115
|
+
const whereObj = whereFn(obj)
|
|
116
|
+
const wheres = whereObj.where
|
|
117
|
+
const arr = whereObj.arr
|
|
118
|
+
const sql = `SELECT ${select} FROM "${table}" ${join} ${wheres} ${statement} ${orderBy} ${limit} ${offset}`
|
|
119
|
+
/*console.log(sql)
|
|
113
120
|
console.log(arr);*/
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
}
|
|
121
|
+
try {
|
|
122
|
+
const result = await pool.query(sql, arr.length ? arr : values.length ? values : null)
|
|
123
|
+
return !result.rows ? [] : result.rows
|
|
124
|
+
} catch (e) {
|
|
125
|
+
console.log(sql)
|
|
126
|
+
console.log(arr)
|
|
127
|
+
console.log(e.toString())
|
|
128
|
+
}
|
|
129
|
+
}
|
|
123
130
|
|
|
124
|
-
connection.result = async(obj) => {
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
}
|
|
131
|
+
connection.result = async (obj) => {
|
|
132
|
+
const results = await connection.results(obj)
|
|
133
|
+
if (results.length) {
|
|
134
|
+
return results[0]
|
|
135
|
+
} else {
|
|
136
|
+
return []
|
|
137
|
+
}
|
|
138
|
+
}
|
|
132
139
|
|
|
133
|
-
connection.insert = async(obj) => {
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
140
|
+
connection.insert = async (obj) => {
|
|
141
|
+
let result
|
|
142
|
+
const table = obj.table
|
|
143
|
+
const data = obj.data
|
|
144
|
+
let increment = 1
|
|
145
|
+
const datas = []
|
|
146
|
+
const values = []
|
|
147
|
+
const arr = []
|
|
148
|
+
const returning = data.returning || 'RETURNING *'
|
|
149
|
+
delete data.returning
|
|
150
|
+
for (const key in data) {
|
|
151
|
+
datas.push(key)
|
|
152
|
+
values.push(`$${increment}`)
|
|
153
|
+
arr.push(data[key])
|
|
154
|
+
increment++
|
|
155
|
+
}
|
|
156
|
+
const sql = `INSERT INTO "${table}" ("${datas.join('","')}") VALUES (${values.join(',')}) ${returning}`
|
|
157
|
+
/* console.log("ON INSERT " + sql)
|
|
151
158
|
console.log(arr)
|
|
152
159
|
*/
|
|
153
160
|
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
}
|
|
161
|
+
try {
|
|
162
|
+
const results = await pool.query(sql, arr)
|
|
163
|
+
return results.rows[0]
|
|
164
|
+
} catch (e) {
|
|
165
|
+
console.log(sql)
|
|
166
|
+
console.log(arr)
|
|
167
|
+
console.log(e.toString())
|
|
168
|
+
throw e
|
|
169
|
+
}
|
|
170
|
+
}
|
|
164
171
|
|
|
165
|
-
connection.update = async(obj) => {
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
172
|
+
connection.update = async (obj) => {
|
|
173
|
+
const table = obj.table
|
|
174
|
+
const data = obj.data
|
|
175
|
+
const returning = data.returning || 'RETURNING *'
|
|
176
|
+
delete data.returning
|
|
170
177
|
|
|
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
|
-
|
|
178
|
+
const where = obj.where || {}
|
|
179
|
+
//[{field:"your_field",option:"=>",value:"12",operator:"AND"}]
|
|
180
|
+
let whereArray = obj.whereArray || []
|
|
181
|
+
const arr = [],
|
|
182
|
+
dataArr = []
|
|
183
|
+
let wherequery = []
|
|
184
|
+
let increment = 1
|
|
185
|
+
for (let key in data) {
|
|
186
|
+
dataArr.push(` "${key}" = $${increment}`)
|
|
187
|
+
arr.push(data[key])
|
|
188
|
+
increment++
|
|
189
|
+
}
|
|
190
|
+
for (var key in where) {
|
|
191
|
+
wherequery.push(` "${key}" = $${increment}`)
|
|
192
|
+
arr.push(where[key])
|
|
193
|
+
increment++
|
|
194
|
+
}
|
|
195
|
+
wherequery = arr.length ? wherequery.join(' AND ') : ''
|
|
196
|
+
if (whereArray.length) {
|
|
197
|
+
let andOr = wherequery ? ' AND ' : ''
|
|
198
|
+
whereArray.forEach((item, index) => {
|
|
199
|
+
if (index > 0) andOr = ''
|
|
200
|
+
const operator = !item.operator ? ' AND ' : item.operator
|
|
201
|
+
const field = item.field.indexOf('.') > -1 ? item.field : `"${item.field}"`
|
|
202
|
+
wherequery += `${andOr} ${field} ${item.option} $${increment} ${operator}`
|
|
203
|
+
arr.push(item.value)
|
|
204
|
+
increment++
|
|
205
|
+
})
|
|
206
|
+
wherequery = wherequery.slice(0, -5)
|
|
207
|
+
}
|
|
208
|
+
const wheres = arr.length ? ' WHERE ' + wherequery : ''
|
|
209
|
+
const sql = `UPDATE "${table}" SET ${dataArr.join(', ')} ${wheres} ${returning}`
|
|
210
|
+
/* console.log(sql);
|
|
204
211
|
console.log(arr);*/
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
}
|
|
212
|
+
try {
|
|
213
|
+
const result = await pool.query(sql, arr)
|
|
214
|
+
return result.rows[0]
|
|
215
|
+
} catch (e) {
|
|
216
|
+
console.log(sql)
|
|
217
|
+
console.log(arr)
|
|
218
|
+
console.log(e.toString())
|
|
219
|
+
throw e
|
|
220
|
+
}
|
|
221
|
+
}
|
|
215
222
|
|
|
216
|
-
connection.delete = async(obj) => {
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
223
|
+
connection.delete = async (obj) => {
|
|
224
|
+
const table = obj.table
|
|
225
|
+
const where = obj.where || {}
|
|
226
|
+
let arr = [],
|
|
227
|
+
wherequery = []
|
|
228
|
+
let increment = 1
|
|
229
|
+
for (const key in where) {
|
|
230
|
+
wherequery.push(` "${key}" = $${increment}`)
|
|
231
|
+
arr.push(where[key])
|
|
232
|
+
increment++
|
|
233
|
+
}
|
|
234
|
+
wherequery = arr.length ? wherequery.join(' AND ') : ''
|
|
235
|
+
const wheres = arr.length ? ' WHERE ' + wherequery : ''
|
|
236
|
+
const sql = `DELETE FROM "${table}" ${wheres}`
|
|
237
|
+
/*console.log(sql);
|
|
230
238
|
console.log(arr)*/
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
+
try {
|
|
240
|
+
return await pool.query(sql, arr)
|
|
241
|
+
} catch (e) {
|
|
242
|
+
console.log(sql)
|
|
243
|
+
console.log(arr)
|
|
244
|
+
console.log(e.toString())
|
|
245
|
+
throw e
|
|
246
|
+
}
|
|
239
247
|
}
|
|
240
248
|
|
|
241
|
-
connection.driver = config.driver
|
|
242
|
-
connection.showTables = "SELECT tablename FROM pg_catalog.pg_tables WHERE schemaname != 'pg_catalog' AND schemaname != 'information_schema'"
|
|
249
|
+
connection.driver = config.driver
|
|
250
|
+
connection.showTables = "SELECT tablename FROM pg_catalog.pg_tables WHERE schemaname != 'pg_catalog' AND schemaname != 'information_schema'"
|
|
243
251
|
connection.showFullFields = (tableRelations) => {
|
|
244
|
-
|
|
252
|
+
return `SELECT
|
|
245
253
|
column_name AS "Field", concat(data_type,'(',character_maximum_length,')') AS "Type" , is_nullable AS "Null"
|
|
246
254
|
FROM
|
|
247
255
|
information_schema.COLUMNS
|
|
@@ -250,20 +258,20 @@ connection.showFullFields = (tableRelations) => {
|
|
|
250
258
|
}
|
|
251
259
|
|
|
252
260
|
connection.describeTable = (table) => {
|
|
253
|
-
|
|
261
|
+
return connection.showFullFields(table)
|
|
254
262
|
}
|
|
255
263
|
|
|
256
264
|
connection.showComments = (table) => {
|
|
257
|
-
|
|
265
|
+
return ` SELECT c.table_schema,c.table_name,c.column_name as "COLUMN_NAME",pgd.description as "COLUMN_COMMENT"
|
|
258
266
|
FROM pg_catalog.pg_statio_all_tables as st
|
|
259
267
|
inner join pg_catalog.pg_description pgd on (pgd.objoid=st.relid)
|
|
260
268
|
inner join information_schema.columns c on (pgd.objsubid=c.ordinal_position
|
|
261
269
|
and c.table_schema=st.schemaname and c.table_name=st.relname)
|
|
262
|
-
WHERE c.table_name = '${table}' ORDER BY c.column_name
|
|
270
|
+
WHERE c.table_name = '${table}' ORDER BY c.column_name`
|
|
263
271
|
}
|
|
264
272
|
|
|
265
273
|
connection.showFields = (table) => {
|
|
266
|
-
|
|
274
|
+
return `
|
|
267
275
|
SELECT
|
|
268
276
|
tc.table_name AS "TABLE_NAME",
|
|
269
277
|
kcu.column_name AS "COLUMN_NAME",
|
|
@@ -279,32 +287,31 @@ connection.showFields = (table) => {
|
|
|
279
287
|
JOIN information_schema.constraint_column_usage AS ccu
|
|
280
288
|
ON ccu.constraint_name = tc.constraint_name
|
|
281
289
|
AND ccu.table_schema = tc.table_schema
|
|
282
|
-
WHERE tc.constraint_type = 'FOREIGN KEY' AND tc.table_name='${table}'
|
|
290
|
+
WHERE tc.constraint_type = 'FOREIGN KEY' AND tc.table_name='${table}';`
|
|
283
291
|
}
|
|
284
292
|
|
|
285
293
|
//list constraint list
|
|
286
|
-
connection.constraintList = (table, schema =
|
|
287
|
-
|
|
294
|
+
connection.constraintList = (table, schema = 'public') => {
|
|
295
|
+
return `
|
|
288
296
|
SELECT con.*
|
|
289
297
|
FROM pg_catalog.pg_constraint con
|
|
290
298
|
INNER JOIN pg_catalog.pg_class rel
|
|
291
299
|
ON rel.oid = con.conrelid
|
|
292
300
|
INNER JOIN pg_catalog.pg_namespace nsp
|
|
293
301
|
ON nsp.oid = connamespace
|
|
294
|
-
WHERE nsp.nspname = '${schema}' AND rel.relname = '${table}';
|
|
302
|
+
WHERE nsp.nspname = '${schema}' AND rel.relname = '${table}'; `
|
|
295
303
|
}
|
|
296
304
|
|
|
297
305
|
var toNumber = function (num) {
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
306
|
+
num = num + ''
|
|
307
|
+
var t = replaceAll(num, '.', '')
|
|
308
|
+
if (t) {
|
|
309
|
+
return parseFloat(t)
|
|
310
|
+
} else return 0
|
|
303
311
|
}
|
|
304
312
|
|
|
305
313
|
function replaceAll(str, find, replace) {
|
|
306
|
-
|
|
314
|
+
return str.replace(new RegExp(find, 'g'), replace)
|
|
307
315
|
}
|
|
308
316
|
|
|
309
|
-
|
|
310
|
-
module.exports = connection;
|
|
317
|
+
module.exports = connection
|
package/lib/zRoute.js
CHANGED
|
@@ -1641,12 +1641,14 @@ zRoute.listData = async (req, res, MYMODEL, zRole, actionButtonsFn) => {
|
|
|
1641
1641
|
const select = Util.selectParser(fields, MYMODEL)
|
|
1642
1642
|
let whereArray = []
|
|
1643
1643
|
const columns = body.columns
|
|
1644
|
-
|
|
1645
|
-
|
|
1646
|
-
|
|
1647
|
-
|
|
1648
|
-
|
|
1649
|
-
|
|
1644
|
+
if (MYMODEL.keys.includes('company_id')) {
|
|
1645
|
+
whereArray.push({
|
|
1646
|
+
field: 'company_id',
|
|
1647
|
+
option: '=',
|
|
1648
|
+
value: res.locals.companyId,
|
|
1649
|
+
operator: 'AND',
|
|
1650
|
+
})
|
|
1651
|
+
}
|
|
1650
1652
|
columns.forEach(function (item) {
|
|
1651
1653
|
if (item.search.value) {
|
|
1652
1654
|
whereArray.push({
|