@saltcorn/postgres 1.0.0-beta.2 → 1.0.0-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.
- package/package.json +2 -2
- package/postgres.js +14 -33
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@saltcorn/postgres",
|
|
3
|
-
"version": "1.0.0-beta.
|
|
3
|
+
"version": "1.0.0-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": "1.0.0-beta.
|
|
15
|
+
"@saltcorn/db-common": "1.0.0-beta.3",
|
|
16
16
|
"pg": "^8.2.1",
|
|
17
17
|
"pg-copy-streams": "^5.1.1"
|
|
18
18
|
},
|
package/postgres.js
CHANGED
|
@@ -7,7 +7,9 @@
|
|
|
7
7
|
const { Pool } = require("pg");
|
|
8
8
|
const copyStreams = require("pg-copy-streams");
|
|
9
9
|
const { promisify } = require("util");
|
|
10
|
-
const { pipeline } = require("stream");
|
|
10
|
+
const { pipeline } = require("stream/promises");
|
|
11
|
+
const { Transform } = require("stream");
|
|
12
|
+
|
|
11
13
|
const {
|
|
12
14
|
sqlsanitize,
|
|
13
15
|
mkWhere,
|
|
@@ -390,36 +392,6 @@ const drop_index = async (table_name, field_name) => {
|
|
|
390
392
|
await pool.query(sql);
|
|
391
393
|
};
|
|
392
394
|
|
|
393
|
-
/**
|
|
394
|
-
* Copy data from CSV to table?
|
|
395
|
-
* Only for PG
|
|
396
|
-
* @param {object} fileStream - file stream
|
|
397
|
-
* @param {string} tableName - table name
|
|
398
|
-
* @param {string[]} fieldNames - list of columns
|
|
399
|
-
* @param {object} client - db connection
|
|
400
|
-
* @returns {Promise<function>} new Promise
|
|
401
|
-
*/
|
|
402
|
-
const copyFrom1 = (fileStream, tableName, fieldNames, client) => {
|
|
403
|
-
// TBD describe difference between CopyFrom and CopyFrom1
|
|
404
|
-
// 1. No tenant support
|
|
405
|
-
// 2. Manual promisification.
|
|
406
|
-
// 3. ???
|
|
407
|
-
// 4. Not exported nor used anywhere
|
|
408
|
-
const quote = (s) => `"${s}"`;
|
|
409
|
-
const sql = `COPY "${sqlsanitize(tableName)}" (${fieldNames
|
|
410
|
-
.map(quote)
|
|
411
|
-
.join(",")}) FROM STDIN CSV HEADER`;
|
|
412
|
-
sql_log(sql);
|
|
413
|
-
|
|
414
|
-
var stream = client.query(copyStreams.from(sql));
|
|
415
|
-
|
|
416
|
-
return new Promise((resolve, reject) => {
|
|
417
|
-
fileStream.on("error", reject);
|
|
418
|
-
stream.on("error", reject);
|
|
419
|
-
stream.on("finish", resolve);
|
|
420
|
-
fileStream.pipe(stream).on("error", reject);
|
|
421
|
-
});
|
|
422
|
-
};
|
|
423
395
|
/**
|
|
424
396
|
* Copy data from CSV to table?
|
|
425
397
|
* Only for PG
|
|
@@ -430,7 +402,6 @@ const copyFrom1 = (fileStream, tableName, fieldNames, client) => {
|
|
|
430
402
|
* @returns {Promise<void>} no results
|
|
431
403
|
*/
|
|
432
404
|
const copyFrom = async (fileStream, tableName, fieldNames, client) => {
|
|
433
|
-
// TBD describe difference between CopyFrom and CopyFrom1
|
|
434
405
|
const quote = (s) => `"${s}"`;
|
|
435
406
|
const sql = `COPY "${getTenantSchema()}"."${sqlsanitize(
|
|
436
407
|
tableName
|
|
@@ -438,7 +409,16 @@ const copyFrom = async (fileStream, tableName, fieldNames, client) => {
|
|
|
438
409
|
sql_log(sql);
|
|
439
410
|
|
|
440
411
|
const stream = client.query(copyStreams.from(sql));
|
|
441
|
-
return await
|
|
412
|
+
return await pipeline(fileStream, stream);
|
|
413
|
+
};
|
|
414
|
+
|
|
415
|
+
const copyToJson = async (fileStream, tableName, client) => {
|
|
416
|
+
const sql = `COPY (SELECT (row_to_json("${sqlsanitize(tableName)}".*) || ',')
|
|
417
|
+
FROM "${getTenantSchema()}"."${sqlsanitize(tableName)}") TO STDOUT`;
|
|
418
|
+
sql_log(sql);
|
|
419
|
+
const stream = (client || pool).query(copyStreams.to(sql));
|
|
420
|
+
|
|
421
|
+
return await pipeline(stream, fileStream);
|
|
442
422
|
};
|
|
443
423
|
|
|
444
424
|
const slugify = (s) =>
|
|
@@ -529,6 +509,7 @@ const postgresExports = {
|
|
|
529
509
|
reset_sequence,
|
|
530
510
|
getVersion,
|
|
531
511
|
copyFrom,
|
|
512
|
+
copyToJson,
|
|
532
513
|
slugify,
|
|
533
514
|
time,
|
|
534
515
|
listTables,
|