@saltcorn/postgres 1.5.0-beta.1 → 1.5.0-beta.10
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/package.json +2 -2
- package/postgres.js +50 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@saltcorn/postgres",
|
|
3
|
-
"version": "1.5.0-beta.
|
|
3
|
+
"version": "1.5.0-beta.10",
|
|
4
4
|
"description": "Postgres structures for Saltcorn, open-source no-code platform",
|
|
5
5
|
"homepage": "https://saltcorn.com",
|
|
6
6
|
"scripts": {
|
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
"license": "MIT",
|
|
13
13
|
"main": "index.js",
|
|
14
14
|
"dependencies": {
|
|
15
|
-
"@saltcorn/db-common": "1.5.0-beta.
|
|
15
|
+
"@saltcorn/db-common": "1.5.0-beta.10",
|
|
16
16
|
"@saltcorn/plain-date": "0.2.5",
|
|
17
17
|
"pg": "^8.13.1",
|
|
18
18
|
"pg-copy-streams": "^6.0.6",
|
package/postgres.js
CHANGED
|
@@ -166,9 +166,14 @@ WHERE c.oid = '"${opts?.schema || getTenantSchema()}"."${sqlsanitize(tbl)}"'::r
|
|
|
166
166
|
}
|
|
167
167
|
}
|
|
168
168
|
|
|
169
|
-
const
|
|
169
|
+
const core_sql = `FROM "${opts?.schema || getTenantSchema()}"."${sqlsanitize(
|
|
170
170
|
tbl
|
|
171
171
|
)}" ${where}`;
|
|
172
|
+
//https://pganalyze.com/blog/5mins-postgres-limited-count
|
|
173
|
+
const sql = opts?.limit
|
|
174
|
+
? `SELECT count(*) AS count FROM (
|
|
175
|
+
SELECT 1 ${core_sql} limit ${+opts?.limit}) limited_count`
|
|
176
|
+
: `SELECT COUNT(*) ${core_sql}`;
|
|
172
177
|
sql_log(sql, values);
|
|
173
178
|
const tq = await getMyClient(opts).query(sql, values);
|
|
174
179
|
|
|
@@ -632,6 +637,48 @@ const tryCatchInTransaction = async (f, onError) => {
|
|
|
632
637
|
}
|
|
633
638
|
};
|
|
634
639
|
|
|
640
|
+
/**
|
|
641
|
+
* Should be used for code that is sometimes called from within a withTransaction block
|
|
642
|
+
* and sometimes not.
|
|
643
|
+
* @param {Function} f logic to execute
|
|
644
|
+
* @param {Function} onError error handler
|
|
645
|
+
* @returns
|
|
646
|
+
*/
|
|
647
|
+
const openOrUseTransaction = async (f, onError) => {
|
|
648
|
+
const reqCon = getRequestContext();
|
|
649
|
+
if (reqCon?.client) return await f();
|
|
650
|
+
else return await withTransaction(f, onError);
|
|
651
|
+
};
|
|
652
|
+
|
|
653
|
+
/**
|
|
654
|
+
* Wait some time until current transaction COMMITs,
|
|
655
|
+
* then open another transaction.
|
|
656
|
+
* @param {Function} f logic to execute
|
|
657
|
+
* @param {Function} onError error handler
|
|
658
|
+
* @returns
|
|
659
|
+
*/
|
|
660
|
+
const whenTransactionisFree = (f, onError) => {
|
|
661
|
+
return new Promise((resolve, reject) => {
|
|
662
|
+
// wait until transaction is free
|
|
663
|
+
let counter = 0;
|
|
664
|
+
const interval = setInterval(async () => {
|
|
665
|
+
const reqCon = getRequestContext();
|
|
666
|
+
if (!reqCon?.client) {
|
|
667
|
+
clearInterval(interval);
|
|
668
|
+
try {
|
|
669
|
+
resolve(await withTransaction(f, onError));
|
|
670
|
+
} catch (e) {
|
|
671
|
+
reject(e);
|
|
672
|
+
}
|
|
673
|
+
}
|
|
674
|
+
if (++counter > 100) {
|
|
675
|
+
clearInterval(interval);
|
|
676
|
+
reject(new Error("Timeout waiting for transaction to be free"));
|
|
677
|
+
}
|
|
678
|
+
}, 200);
|
|
679
|
+
});
|
|
680
|
+
};
|
|
681
|
+
|
|
635
682
|
const query = (text, params) => {
|
|
636
683
|
sql_log(text, params);
|
|
637
684
|
return getMyClient().query(text, params);
|
|
@@ -682,6 +729,8 @@ const postgresExports = {
|
|
|
682
729
|
truncate,
|
|
683
730
|
withTransaction,
|
|
684
731
|
tryCatchInTransaction,
|
|
732
|
+
openOrUseTransaction,
|
|
733
|
+
whenTransactionisFree,
|
|
685
734
|
commitAndBeginNewTransaction,
|
|
686
735
|
};
|
|
687
736
|
|