@saltcorn/postgres 0.8.8-beta.1 → 0.8.8-beta.3

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.
Files changed (2) hide show
  1. package/package.json +2 -2
  2. package/postgres.js +36 -8
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@saltcorn/postgres",
3
- "version": "0.8.8-beta.1",
3
+ "version": "0.8.8-beta.3",
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.8.8-beta.1",
15
+ "@saltcorn/db-common": "0.8.8-beta.3",
16
16
  "pg": "^8.2.1",
17
17
  "pg-copy-streams": "^5.1.1"
18
18
  },
package/postgres.js CHANGED
@@ -17,6 +17,7 @@ const {
17
17
  let getTenantSchema;
18
18
  let getConnectObject = null;
19
19
  let pool = null;
20
+ let client = null;
20
21
 
21
22
  let log_sql_enabled = false;
22
23
 
@@ -66,6 +67,23 @@ const changeConnection = async (connObj = {}) => {
66
67
  pool = new Pool(getConnectObject(connObj));
67
68
  };
68
69
 
70
+ const begin = async () => {
71
+ client = await getClient();
72
+ await client.query("BEGIN");
73
+ };
74
+
75
+ const commit = async () => {
76
+ await client.query("COMMIT");
77
+ client.release(true);
78
+ client = null;
79
+ };
80
+
81
+ const rollback = async () => {
82
+ await client.query("ROLLBACK");
83
+ client.release(true);
84
+ client = null;
85
+ };
86
+
69
87
  /**
70
88
  * Execute Select statement
71
89
  * @param {string} tbl - table name
@@ -82,7 +100,7 @@ const select = async (tbl, whereObj, selectopts = {}) => {
82
100
  selectopts
83
101
  )}`;
84
102
  sql_log(sql, values);
85
- const tq = await pool.query(sql, values);
103
+ const tq = await (client || selectopts.client || pool).query(sql, values);
86
104
 
87
105
  return tq.rows;
88
106
  };
@@ -116,7 +134,7 @@ const count = async (tbl, whereObj) => {
116
134
  tbl
117
135
  )}" ${where}`;
118
136
  sql_log(sql, values);
119
- const tq = await pool.query(sql, values);
137
+ const tq = await (client || pool).query(sql, values);
120
138
 
121
139
  return parseInt(tq.rows[0].count);
122
140
  };
@@ -152,7 +170,7 @@ const deleteWhere = async (tbl, whereObj, opts = {}) => {
152
170
  )}" ${where}`;
153
171
  sql_log(sql, values);
154
172
 
155
- const tq = await (opts.client || pool).query(sql, values);
173
+ const tq = await (client || opts.client || pool).query(sql, values);
156
174
 
157
175
  return tq.rows;
158
176
  };
@@ -193,7 +211,7 @@ const insert = async (tbl, obj, opts = {}) => {
193
211
  tbl
194
212
  )}" DEFAULT VALUES returning ${opts.noid ? "*" : opts.pk_name || "id"}`;
195
213
  sql_log(sql, valList);
196
- const { rows } = await (opts.client || pool).query(sql, valList);
214
+ const { rows } = await (client || opts.client || pool).query(sql, valList);
197
215
  if (opts.noid) return;
198
216
  else return rows[0][opts.pk_name || "id"];
199
217
  };
@@ -220,7 +238,7 @@ const update = async (tbl, obj, id, opts = {}) => {
220
238
  tbl
221
239
  )}" set ${assigns} where ${opts.pk_name || "id"}=$${kvs.length + 1}`;
222
240
  sql_log(q, valList);
223
- await (opts.client || pool).query(q, valList);
241
+ await (client || opts.client || pool).query(q, valList);
224
242
  };
225
243
 
226
244
  /**
@@ -244,7 +262,7 @@ const updateWhere = async (tbl, obj, whereObj, opts = {}) => {
244
262
  tbl
245
263
  )}" set ${assigns} ${where}`;
246
264
  sql_log(q, valList);
247
- await (opts.client || pool).query(q, valList);
265
+ await (client || opts.client || pool).query(q, valList);
248
266
  };
249
267
 
250
268
  /**
@@ -296,7 +314,7 @@ const reset_sequence = async (tblname) => {
296
314
  )}"', 'id'), coalesce(max(id),0) + 1, false) FROM "${getTenantSchema()}"."${sqlsanitize(
297
315
  tblname
298
316
  )}";`;
299
- await pool.query(sql);
317
+ await (client || pool).query(sql);
300
318
  };
301
319
 
302
320
  /**
@@ -424,6 +442,12 @@ const slugify = (s) =>
424
442
  .replace(/\s+/g, "-")
425
443
  .replace(/[^\w-]/g, "");
426
444
 
445
+ const time = async () => {
446
+ const result = await (client || pool).query("select now()");
447
+ const row = result.rows[0];
448
+ return new Date(row.now);
449
+ };
450
+
427
451
  /**
428
452
  *
429
453
  * @returns
@@ -472,8 +496,11 @@ const postgresExports = {
472
496
  */
473
497
  query: (text, params) => {
474
498
  sql_log(text, params);
475
- return pool.query(text, params);
499
+ return (client || pool).query(text, params);
476
500
  },
501
+ begin,
502
+ commit,
503
+ rollback,
477
504
  select,
478
505
  selectOne,
479
506
  selectMaybeOne,
@@ -498,6 +525,7 @@ const postgresExports = {
498
525
  getVersion,
499
526
  copyFrom,
500
527
  slugify,
528
+ time,
501
529
  listTables,
502
530
  listScTables,
503
531
  listUserDefinedTables,