zet-lib 1.5.5 → 1.5.7
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/index.js +1 -0
- 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/index.js
CHANGED