zet-lib 1.5.6 → 1.5.8
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 +22 -3
- package/lib/zRoute.js +4 -4
- package/package.json +1 -1
package/lib/connection.js
CHANGED
|
@@ -152,7 +152,6 @@ const whereFn = (obj) => {
|
|
|
152
152
|
connection.results = async (obj) => {
|
|
153
153
|
const select = obj.select || '*'
|
|
154
154
|
const table = obj.table || ''
|
|
155
|
-
//[{field:"your_field",option:"=>",value:"12",operator:"AND",type:"text,json,date"}]
|
|
156
155
|
const statement = obj.statement || ''
|
|
157
156
|
const limit = obj.limit ? ` LIMIT ${obj.limit} ` : ''
|
|
158
157
|
const offset = obj.hasOwnProperty('offset') ? ` OFFSET ${obj.offset} ` : obj.limit ? 'OFFSET 0' : ''
|
|
@@ -166,11 +165,31 @@ connection.results = async (obj) => {
|
|
|
166
165
|
const whereObj = whereFn(obj)
|
|
167
166
|
const wheres = whereObj.where
|
|
168
167
|
const arr = whereObj.arr
|
|
168
|
+
|
|
169
|
+
// --- OPTIMIZATION: Fast count for count(id) as count ---
|
|
170
|
+
if (select.trim().toLowerCase() === 'count(id) as count' && !wheres) {
|
|
171
|
+
// No filter, use fast count from pg_class
|
|
172
|
+
const sql = `SELECT reltuples::bigint AS count FROM pg_class WHERE relname = '${table}'`;
|
|
173
|
+
try {
|
|
174
|
+
const start = Date.now();
|
|
175
|
+
const result = await pool.query(sql);
|
|
176
|
+
const elapsed = Date.now() - start;
|
|
177
|
+
console.log(`[zet-lib] Fast count for ${table}: ${elapsed}ms`);
|
|
178
|
+
return result.rows;
|
|
179
|
+
} catch (e) {
|
|
180
|
+
console.log(sql)
|
|
181
|
+
console.log(e.toString())
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
|
|
169
185
|
const sql = `SELECT ${select} FROM "${table}" ${join} ${wheres} ${statement} ${orderBy} ${limit} ${offset}`
|
|
170
|
-
/*console.log(sql)
|
|
171
|
-
console.log(arr);*/
|
|
172
186
|
try {
|
|
187
|
+
const start = Date.now();
|
|
173
188
|
const result = await pool.query(sql, arr.length ? arr : values.length ? values : null)
|
|
189
|
+
const elapsed = Date.now() - start;
|
|
190
|
+
/*if (select.trim().toLowerCase() === 'count(id) as count') {
|
|
191
|
+
console.log(`[zet-lib] Count query for ${table}: ${elapsed}ms`);
|
|
192
|
+
}*/
|
|
174
193
|
return !result.rows ? [] : result.rows
|
|
175
194
|
} catch (e) {
|
|
176
195
|
console.log(sql)
|
package/lib/zRoute.js
CHANGED
|
@@ -5878,11 +5878,11 @@ zRoute.typeahead = async (req, res) => {
|
|
|
5878
5878
|
}
|
|
5879
5879
|
let MYMODEL = MYMODELS[table];
|
|
5880
5880
|
let WIDGET = MYMODEL.widgets;
|
|
5881
|
-
let query =
|
|
5882
|
-
//employee_payroll_salary___employee_id_1
|
|
5881
|
+
let query = req.query.query;
|
|
5883
5882
|
let name = `${MYMODEL.widgets[keys].table}_${MYMODEL.table}___${keys}_${res.locals.companyId}`;
|
|
5884
|
-
let
|
|
5885
|
-
|
|
5883
|
+
let sql = `select id,${MYMODEL.widgets[keys].fields[1]} as zname from ${MYMODEL.widgets[keys].table} where ${MYMODEL.widgets[keys].fields[1]} ILIKE '%${query}%' limit 50`
|
|
5884
|
+
let results = await connection.query(sql);
|
|
5885
|
+
res.json(results);
|
|
5886
5886
|
} catch (e) {
|
|
5887
5887
|
console.log(e);
|
|
5888
5888
|
res.json(e + "");
|