@saltcorn/postgres 1.3.1-beta.9 → 1.4.0-beta.0
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 +3 -3
- package/postgres.js +22 -9
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@saltcorn/postgres",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.4.0-beta.0",
|
|
4
4
|
"description": "Postgres structures for Saltcorn, open-source no-code platform",
|
|
5
5
|
"homepage": "https://saltcorn.com",
|
|
6
6
|
"scripts": {
|
|
@@ -12,8 +12,8 @@
|
|
|
12
12
|
"license": "MIT",
|
|
13
13
|
"main": "index.js",
|
|
14
14
|
"dependencies": {
|
|
15
|
-
"@saltcorn/db-common": "1.
|
|
16
|
-
"@saltcorn/plain-date": "0.
|
|
15
|
+
"@saltcorn/db-common": "1.4.0-beta.0",
|
|
16
|
+
"@saltcorn/plain-date": "0.2.0",
|
|
17
17
|
"pg": "^8.13.1",
|
|
18
18
|
"pg-copy-streams": "^6.0.6",
|
|
19
19
|
"replacestream": "4.0.3"
|
package/postgres.js
CHANGED
|
@@ -142,7 +142,7 @@ const drop_reset_schema = async (schema) => {
|
|
|
142
142
|
* @param {object} - whereObj - where object
|
|
143
143
|
* @returns {Promise<number>} count of tables
|
|
144
144
|
*/
|
|
145
|
-
const count = async (tbl, whereObj) => {
|
|
145
|
+
const count = async (tbl, whereObj, opts) => {
|
|
146
146
|
const { where, values } = mkWhere(whereObj);
|
|
147
147
|
if (!where) {
|
|
148
148
|
try {
|
|
@@ -156,9 +156,9 @@ const count = async (tbl, whereObj) => {
|
|
|
156
156
|
/ pg_catalog.current_setting('block_size')::int)
|
|
157
157
|
)::bigint
|
|
158
158
|
FROM pg_catalog.pg_class c
|
|
159
|
-
WHERE c.oid = '"${getTenantSchema()}"."${sqlsanitize(tbl)}"'::regclass`;
|
|
159
|
+
WHERE c.oid = '"${opts?.schema || getTenantSchema()}"."${sqlsanitize(tbl)}"'::regclass`;
|
|
160
160
|
sql_log(sql);
|
|
161
|
-
const tq = await getMyClient().query(sql, []);
|
|
161
|
+
const tq = await getMyClient(opts).query(sql, []);
|
|
162
162
|
const n = +tq.rows[0].int8;
|
|
163
163
|
if (n && n > 10000) return n;
|
|
164
164
|
} catch {
|
|
@@ -166,11 +166,11 @@ WHERE c.oid = '"${getTenantSchema()}"."${sqlsanitize(tbl)}"'::regclass`;
|
|
|
166
166
|
}
|
|
167
167
|
}
|
|
168
168
|
|
|
169
|
-
const sql = `SELECT COUNT(*) FROM "${getTenantSchema()}"."${sqlsanitize(
|
|
169
|
+
const sql = `SELECT COUNT(*) FROM "${opts?.schema || getTenantSchema()}"."${sqlsanitize(
|
|
170
170
|
tbl
|
|
171
171
|
)}" ${where}`;
|
|
172
172
|
sql_log(sql, values);
|
|
173
|
-
const tq = await getMyClient().query(sql, values);
|
|
173
|
+
const tq = await getMyClient(opts).query(sql, values);
|
|
174
174
|
|
|
175
175
|
return parseInt(tq.rows[0].count);
|
|
176
176
|
};
|
|
@@ -201,7 +201,7 @@ const getVersion = async (short) => {
|
|
|
201
201
|
*/
|
|
202
202
|
const deleteWhere = async (tbl, whereObj, opts = Object.create(null)) => {
|
|
203
203
|
const { where, values } = mkWhere(whereObj);
|
|
204
|
-
const sql = `delete FROM "${getTenantSchema()}"."${sqlsanitize(
|
|
204
|
+
const sql = `delete FROM "${opts.schema || getTenantSchema()}"."${sqlsanitize(
|
|
205
205
|
tbl
|
|
206
206
|
)}" ${where}`;
|
|
207
207
|
sql_log(sql, values);
|
|
@@ -281,10 +281,23 @@ const update = async (tbl, obj, id, opts = Object.create(null)) => {
|
|
|
281
281
|
let valList = kvs.map(([k, v]) => v);
|
|
282
282
|
// TBD check that is correct - because in insert function opts.noid ? "*" : opts.pk_name || "id"
|
|
283
283
|
//valList.push(id === "undefined"? obj[opts.pk_name]: id);
|
|
284
|
-
|
|
285
|
-
|
|
284
|
+
let whereS;
|
|
285
|
+
if (id && typeof id == "object") {
|
|
286
|
+
let n = kvs.length + 1;
|
|
287
|
+
const whereStrs = [];
|
|
288
|
+
Object.keys(id).forEach((k) => {
|
|
289
|
+
valList.push(id[k]);
|
|
290
|
+
whereStrs.push(`"${k}"=$${n}`);
|
|
291
|
+
n += 1;
|
|
292
|
+
});
|
|
293
|
+
whereS = whereStrs.join(" and ");
|
|
294
|
+
} else {
|
|
295
|
+
valList.push(id === "undefined" ? obj[opts.pk_name || "id"] : id);
|
|
296
|
+
whereS = `${ppPK(opts.pk_name)}=$${kvs.length + 1}`;
|
|
297
|
+
}
|
|
298
|
+
const q = `update "${opts.schema || getTenantSchema()}"."${sqlsanitize(
|
|
286
299
|
tbl
|
|
287
|
-
)}" set ${assigns} where ${
|
|
300
|
+
)}" set ${assigns} where ${whereS}`;
|
|
288
301
|
sql_log(q, valList);
|
|
289
302
|
await getMyClient(opts).query(q, valList);
|
|
290
303
|
};
|