@saltcorn/postgres 0.7.4 → 0.8.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 +2 -2
- package/postgres.js +13 -12
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@saltcorn/postgres",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.8.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,7 +12,7 @@
|
|
|
12
12
|
"license": "MIT",
|
|
13
13
|
"main": "index.js",
|
|
14
14
|
"dependencies": {
|
|
15
|
-
"@saltcorn/db-common": "0.
|
|
15
|
+
"@saltcorn/db-common": "0.8.0-beta.0",
|
|
16
16
|
"pg": "^8.2.1",
|
|
17
17
|
"pg-copy-streams": "^5.1.1"
|
|
18
18
|
},
|
package/postgres.js
CHANGED
|
@@ -182,13 +182,12 @@ const insert = async (tbl, obj, opts = {}) => {
|
|
|
182
182
|
const sql =
|
|
183
183
|
valPosList.length > 0
|
|
184
184
|
? `insert into "${schema}"."${sqlsanitize(
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
}`
|
|
185
|
+
tbl
|
|
186
|
+
)}"(${fnameList}) values(${valPosList.join()}) returning ${opts.noid ? "*" : opts.pk_name || "id"
|
|
187
|
+
}`
|
|
189
188
|
: `insert into "${schema}"."${sqlsanitize(
|
|
190
|
-
|
|
191
|
-
|
|
189
|
+
tbl
|
|
190
|
+
)}" DEFAULT VALUES returning ${opts.noid ? "*" : opts.pk_name || "id"}`;
|
|
192
191
|
sql_log(sql, valList);
|
|
193
192
|
const { rows } = await (opts.client || pool).query(sql, valList);
|
|
194
193
|
if (opts.noid) return;
|
|
@@ -245,11 +244,12 @@ const updateWhere = async (tbl, obj, whereObj) => {
|
|
|
245
244
|
* Select one record
|
|
246
245
|
* @param {srting} tbl - table name
|
|
247
246
|
* @param {object} where - where object
|
|
247
|
+
* @param {object} [selectopts = {}] - select options
|
|
248
248
|
* @returns {Promise<object>} return first record from sql result
|
|
249
249
|
* @throws {Error}
|
|
250
250
|
*/
|
|
251
|
-
const selectOne = async (tbl, where) => {
|
|
252
|
-
const rows = await select(tbl, where);
|
|
251
|
+
const selectOne = async (tbl, where, selectopts = {}) => {
|
|
252
|
+
const rows = await select(tbl, where, { ...selectopts, limit: 1 });
|
|
253
253
|
if (rows.length === 0) {
|
|
254
254
|
const w = mkWhere(where);
|
|
255
255
|
throw new Error(`no ${tbl} ${w.where} are ${w.values}`);
|
|
@@ -260,10 +260,11 @@ const selectOne = async (tbl, where) => {
|
|
|
260
260
|
* Select one record or null if no records
|
|
261
261
|
* @param {string} tbl - table name
|
|
262
262
|
* @param {object} where - where object
|
|
263
|
+
* @param {object} [selectopts = {}] - select options
|
|
263
264
|
* @returns {Promise<null|object>} - null if no record or first record data
|
|
264
265
|
*/
|
|
265
|
-
const selectMaybeOne = async (tbl, where) => {
|
|
266
|
-
const rows = await select(tbl, where);
|
|
266
|
+
const selectMaybeOne = async (tbl, where, selectopts = {}) => {
|
|
267
|
+
const rows = await select(tbl, where, { ...selectopts, limit: 1 });
|
|
267
268
|
if (rows.length === 0) return null;
|
|
268
269
|
else return rows[0];
|
|
269
270
|
};
|
|
@@ -304,8 +305,8 @@ const add_unique_constraint = async (table_name, field_names) => {
|
|
|
304
305
|
)}" add CONSTRAINT "${sqlsanitize(table_name)}_${field_names
|
|
305
306
|
.map((f) => sqlsanitize(f))
|
|
306
307
|
.join("_")}_unique" UNIQUE (${field_names
|
|
307
|
-
|
|
308
|
-
|
|
308
|
+
.map((f) => `"${sqlsanitize(f)}"`)
|
|
309
|
+
.join(",")});`;
|
|
309
310
|
sql_log(sql);
|
|
310
311
|
await pool.query(sql);
|
|
311
312
|
};
|