zet-lib 1.0.61 → 1.0.64

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 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
- 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
- });
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
- const connection = {};
15
-
16
- connection.query = async(string, arr) => {
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
- const result = await pool.query(string, arr);
21
- return result.rows;
22
- } catch (e) {
23
- console.log(string);
24
- console.log(e)
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
- const objOrderby = !obj.orderBy ? [] : obj.orderBy;
30
- let orderBy = "";
31
- if(objOrderby.length) {
32
- orderBy = ` ORDER BY `;
33
- for(var i =0; i < objOrderby.length; i++) {
34
- if(i%2==0) {
35
- orderBy += ` "${obj.orderBy[i]}" `
36
- } else {
37
- orderBy += ` ${obj.orderBy[i]} `;
38
- if(i == (objOrderby.length -1)) {
39
- orderBy += ` `
40
- } else {
41
- orderBy += `, `
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
- return orderBy;
48
- };
46
+ return orderBy
47
+ }
49
48
 
50
49
  const whereFn = (obj) => {
51
- const where = obj.where || {};
52
- //[{field:"your_field",option:"=>",value:"12",operator:"AND",isJSON : false}]
53
- const whereArray = obj.whereArray || [];
54
- let increment = 1;
55
- let arr = [], 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))
68
- operator = "";
69
- const field = item.field.indexOf(".") > -1 ? item.field : ` "${item.field}" `;
70
- //is JSON is field is JSON
71
- //JSON_CONTAINS(color, '"Red"' ,'$')
72
- if (item.isJSON) {
73
- wherequery += andOr + ` JSON_CONTAINS(${field}, '"${item.value}"' ,'$') ${operator}`
74
- } else {
75
- wherequery += `${andOr} ${field} ${item.option} $${increment} ${operator}`;
76
- let itemValue = item.value;
77
- if(item.option == "="){
78
- itemValue = Util.replaceAll(itemValue,"%","");
79
- }
80
- arr.push(itemValue);
81
- }
82
- increment++;
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
- connection.results = async(obj) => {
95
- const select = obj.select || "*";
96
- const table = obj.table || "";
97
- //[{field:"your_field",option:"=>",value:"12",operator:"AND",isJSON : false}]
98
- const statement = obj.statement || "";
99
- const limit = obj.limit ? ` LIMIT ${obj.limit} ` : "";
100
- const offset = obj.hasOwnProperty("offset") ? ` OFFSET ${obj.offset} ` : obj.limit ? "OFFSET 0" : "";
101
- const orderBy = orderByFn(obj);
102
- const values = obj.values || [];
103
- const objJoin = obj.joins || [];
104
- let join = '';
105
- if (objJoin.length) {
106
- join = objJoin.join(" ");
107
- }
108
- const whereObj = whereFn(obj);
109
- const wheres = whereObj.where;
110
- const arr = whereObj.arr;
111
- const sql = `SELECT ${select} FROM "${table}" ${join} ${wheres} ${statement} ${orderBy} ${limit} ${offset}`;
112
- /*console.log(sql)
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
- try {
115
- const result = await pool.query(sql, arr.length ? arr : values.length ? values : null);
116
- return !result.rows ? [] : result.rows;
117
- } catch (e) {
118
- console.log(sql);
119
- console.log(arr);
120
- console.log(e.toString());
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
- const results = await connection.results(obj);
126
- if(results.length){
127
- return results[0];
128
- } else {
129
- return [];
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
- let result;
135
- const table = obj.table;
136
- const data = obj.data;
137
- let increment = 1;
138
- const datas = [];
139
- const values = [];
140
- const arr = [];
141
- const returning = data.returning || 'RETURNING *'
142
- delete data.returning;
143
- for (const key in data) {
144
- datas.push(key);
145
- values.push(`$${increment}`)
146
- arr.push(data[key]);
147
- increment++;
148
- }
149
- const sql = `INSERT INTO "${table}" ("${datas.join('","')}") VALUES (${values.join(",")}) ${returning}`;
150
- /* console.log("ON INSERT " + sql)
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
- try {
155
- const results = await pool.query(sql, arr);
156
- return results.rows[0];
157
- } catch (e) {
158
- console.log(sql);
159
- console.log(arr);
160
- console.log(e.toString());
161
- throw e;
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
- const table = obj.table;
167
- const data = obj.data;
168
- const returning = data.returning || 'RETURNING *'
169
- delete data.returning;
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
- const where = obj.where || {}
172
- //[{field:"your_field",option:"=>",value:"12",operator:"AND"}]
173
- let whereArray = obj.whereArray || [];
174
- const arr = [], dataArr = [];
175
- let wherequery = [];
176
- let increment = 1;
177
- for (let key in data) {
178
- dataArr.push(` "${key}" = $${increment}`)
179
- arr.push(data[key])
180
- increment++;
181
- }
182
- for (var key in where) {
183
- wherequery.push(` "${key}" = $${increment}`);
184
- arr.push(where[key]);
185
- increment++;
186
- }
187
- wherequery = arr.length ? wherequery.join(" AND ") : "";
188
- if (whereArray.length) {
189
- let andOr = wherequery ? " AND " : "";
190
- whereArray.forEach((item, index) => {
191
- if (index > 0)
192
- andOr = "";
193
- const operator = !item.operator ? " AND " : item.operator;
194
- const field = item.field.indexOf(".") > -1 ? item.field : `"${item.field}"`;
195
- wherequery += `${andOr} ${field} ${item.option} $${increment} ${operator}`;
196
- arr.push(item.value);
197
- increment++;
198
- });
199
- wherequery = wherequery.slice(0, -5);
200
- }
201
- const wheres = arr.length ? " WHERE " + wherequery : "";
202
- const sql = `UPDATE "${table}" SET ${dataArr.join(", ")} ${wheres} ${returning}`;
203
- /* console.log(sql);
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
- try {
206
- const result = await pool.query(sql, arr);
207
- return result.rows[0];
208
- } catch (e) {
209
- console.log(sql);
210
- console.log(arr);
211
- console.log(e.toString());
212
- throw e;
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
- const table = obj.table;
218
- const where = obj.where || {}
219
- let arr = [], wherequery = [];
220
- let increment = 1;
221
- for (const key in where) {
222
- wherequery.push(` "${key}" = $${increment}`);
223
- arr.push(where[key]);
224
- increment++;
225
- }
226
- wherequery = arr.length ? wherequery.join(" AND ") : "";
227
- const wheres = arr.length ? " WHERE " + wherequery : "";
228
- const sql = `DELETE FROM "${table}" ${wheres}`
229
- /*console.log(sql);
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
- try {
232
- return await pool.query(sql, arr);
233
- } catch (e) {
234
- console.log(sql);
235
- console.log(arr);
236
- console.log(e.toString());
237
- throw e;
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
- return `SELECT
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
- return connection.showFullFields(table);
261
+ return connection.showFullFields(table)
254
262
  }
255
263
 
256
264
  connection.showComments = (table) => {
257
- return ` SELECT c.table_schema,c.table_name,c.column_name as "COLUMN_NAME",pgd.description as "COLUMN_COMMENT"
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
- return `
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 = "public") => {
287
- return `
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
- num = num + "";
299
- var t = replaceAll(num, ".", "");
300
- if (t) {
301
- return parseFloat(t);
302
- } else return 0;
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
- return str.replace(new RegExp(find, 'g'), replace);
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
@@ -1077,7 +1077,7 @@ zRoute.selectAllowedField = (req, res, MYMODEL, arr = []) => {
1077
1077
  return arr
1078
1078
  }
1079
1079
 
1080
- zRoute.excelQuery = async (req, res, MYMODEL) => {
1080
+ zRoute.excelQuery = async (req, res, MYMODEL, paramsObject = {}) => {
1081
1081
  let virtuals = []
1082
1082
  for (var key in MYMODEL.widgets) {
1083
1083
  if (MYMODEL.widgets[key].name === 'virtual') {
@@ -1113,13 +1113,19 @@ zRoute.excelQuery = async (req, res, MYMODEL) => {
1113
1113
  body = result.filter
1114
1114
  fields = body.fields.filter((item) => item != 'no' && item != 'actionColumn')
1115
1115
  select = Util.selectParser(fields, MYMODEL)
1116
- console.log(virtuals)
1117
1116
  if (zall) {
1118
1117
  let difference = allfields.filter((x) => !fields.includes(x))
1119
1118
  arr = [...fields, ...difference].filter((x) => !virtuals.includes(x))
1120
1119
  select = `"${arr.join('","')}"`
1121
1120
  }
1122
1121
  let whereArray = []
1122
+ if (paramsObject.hasOwnProperty('whereArray')) {
1123
+ if (Array.isArray(paramsObject.whereArray)) {
1124
+ whereArray = paramsObject.whereArray
1125
+ } else {
1126
+ whereArray.push(paramsObject.whereArray)
1127
+ }
1128
+ }
1123
1129
  let columns = body.columns
1124
1130
  columns.forEach(function (item) {
1125
1131
  if (item.search.value) {
@@ -1641,12 +1647,14 @@ zRoute.listData = async (req, res, MYMODEL, zRole, actionButtonsFn) => {
1641
1647
  const select = Util.selectParser(fields, MYMODEL)
1642
1648
  let whereArray = []
1643
1649
  const columns = body.columns
1644
- whereArray.push({
1645
- field: 'company_id',
1646
- option: '=',
1647
- value: res.locals.companyId,
1648
- operator: 'AND',
1649
- })
1650
+ if (MYMODEL.keys.includes('company_id')) {
1651
+ whereArray.push({
1652
+ field: 'company_id',
1653
+ option: '=',
1654
+ value: res.locals.companyId,
1655
+ operator: 'AND',
1656
+ })
1657
+ }
1650
1658
  columns.forEach(function (item) {
1651
1659
  if (item.search.value) {
1652
1660
  whereArray.push({
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "zet-lib",
3
- "version": "1.0.61",
3
+ "version": "1.0.64",
4
4
  "description": "zet is a library that part of zet generator.",
5
5
  "engines": {
6
6
  "node": ">=18"