@kosha-ai/integration-postgres 0.8.1 → 0.8.2
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.
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"integration-postgres.d.ts","sourceRoot":"","sources":["../../src/lib/integration-postgres.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"integration-postgres.d.ts","sourceRoot":"","sources":["../../src/lib/integration-postgres.ts"],"names":[],"mappings":"AASA,OAAO,KAAK,EAMV,gBAAgB,EAEjB,MAAM,2BAA2B,CAAC;AAInC,qBAAa,uBAAwB,SAAQ,KAAK;gBACpC,OAAO,EAAE,MAAM;CAI5B;AA4KD;;;GAGG;AACH,wBAAgB,QAAQ,IAAI,gBAAgB,CAqH3C;AAED,eAAe,QAAQ,CAAC"}
|
|
@@ -1,4 +1,6 @@
|
|
|
1
|
+
import { pipeline } from 'node:stream/promises';
|
|
1
2
|
import { Client } from 'pg';
|
|
3
|
+
import { from as copyFrom } from 'pg-copy-streams';
|
|
2
4
|
import { assertAllowedHost, basic as basicAuth, defineIntegration, packageDirs, } from '@kosha-ai/integration-sdk';
|
|
3
5
|
const { docsPath, migrationsPath } = packageDirs(import.meta.url);
|
|
4
6
|
export class PostgresConnectionError extends Error {
|
|
@@ -132,6 +134,9 @@ async function withQueryClient(fields, credential, accessMode, fn) {
|
|
|
132
134
|
await client.end();
|
|
133
135
|
}
|
|
134
136
|
}
|
|
137
|
+
function quoteIdent(name) {
|
|
138
|
+
return `"${name.replace(/"/g, '""')}"`;
|
|
139
|
+
}
|
|
135
140
|
/**
|
|
136
141
|
* A Postgres database, reached directly over its own wire protocol rather
|
|
137
142
|
* than proxied as HTTP — hence `query`, not `api`.
|
|
@@ -180,6 +185,22 @@ export function postgres() {
|
|
|
180
185
|
};
|
|
181
186
|
});
|
|
182
187
|
},
|
|
188
|
+
/**
|
|
189
|
+
* Streams the CSV with `COPY ... FROM STDIN`. The statement is built here
|
|
190
|
+
* rather than taken from the caller: COPY can also run programs and read
|
|
191
|
+
* server files, and anything but FROM STDIN would leave the stream
|
|
192
|
+
* waiting. A failed source or row aborts the COPY, so nothing is kept.
|
|
193
|
+
*/
|
|
194
|
+
async copy({ credential, fields, accessMode = 'readonly', schema, table, columns, data, }) {
|
|
195
|
+
const target = `${quoteIdent(schema)}.${quoteIdent(table)}`;
|
|
196
|
+
const columnList = columns.map(quoteIdent).join(', ');
|
|
197
|
+
return withQueryClient(fields, credential, accessMode, async (client) => {
|
|
198
|
+
const stream = copyFrom(`COPY ${target} (${columnList}) FROM STDIN WITH (FORMAT csv, HEADER true)`);
|
|
199
|
+
client.query(stream);
|
|
200
|
+
await pipeline(data, stream);
|
|
201
|
+
return { rowCount: stream.rowCount };
|
|
202
|
+
});
|
|
203
|
+
},
|
|
183
204
|
},
|
|
184
205
|
identity: {
|
|
185
206
|
/**
|
package/docs/setup.md
CHANGED
|
@@ -59,6 +59,24 @@ Nothing stops you from connecting a second, different credential under a
|
|
|
59
59
|
different owner later (per team, per environment, whatever you need) — it's
|
|
60
60
|
just that most setups only ever need the one.
|
|
61
61
|
|
|
62
|
+
### Bulk loading a CSV
|
|
63
|
+
|
|
64
|
+
A `readwrite` configuration can load a CSV with a header row into an existing
|
|
65
|
+
table. The body is streamed through `COPY ... FROM STDIN`, so every row is
|
|
66
|
+
loaded or none is. `columns` is a JSON array naming the table columns in the
|
|
67
|
+
order the CSV holds them.
|
|
68
|
+
|
|
69
|
+
```http
|
|
70
|
+
POST /api/query/postgres/pg-main/org/copy?schema=public&table=users&columns=["id","name"]
|
|
71
|
+
Content-Type: text/csv
|
|
72
|
+
|
|
73
|
+
id,name
|
|
74
|
+
1,Alice
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
Unquoted empty fields load as NULL. The 30-second statement timeout below
|
|
78
|
+
includes the time taken to send the file.
|
|
79
|
+
|
|
62
80
|
### Access mode, enforced by Postgres itself
|
|
63
81
|
|
|
64
82
|
`readonly` configurations run inside a `BEGIN READ ONLY` transaction that is
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kosha-ai/integration-postgres",
|
|
3
|
-
"version": "0.8.
|
|
3
|
+
"version": "0.8.2",
|
|
4
4
|
"description": "Postgres integration for Kosha: configurable read-only or read-write queries against a connected database",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"kosha",
|
|
@@ -40,18 +40,20 @@
|
|
|
40
40
|
"!**/*.tsbuildinfo"
|
|
41
41
|
],
|
|
42
42
|
"dependencies": {
|
|
43
|
-
"
|
|
44
|
-
"pg": "^
|
|
43
|
+
"pg": "^8.13.0",
|
|
44
|
+
"pg-copy-streams": "^7.0.0",
|
|
45
|
+
"tslib": "^2.3.0"
|
|
45
46
|
},
|
|
46
47
|
"kosha": {
|
|
47
48
|
"kind": "integration",
|
|
48
49
|
"key": "postgres"
|
|
49
50
|
},
|
|
50
51
|
"peerDependencies": {
|
|
51
|
-
"@kosha-ai/integration-sdk": "0.8.
|
|
52
|
+
"@kosha-ai/integration-sdk": "0.8.2"
|
|
52
53
|
},
|
|
53
54
|
"devDependencies": {
|
|
54
55
|
"@types/pg": "^8.11.0",
|
|
55
|
-
"@
|
|
56
|
+
"@types/pg-copy-streams": "^1.2.5",
|
|
57
|
+
"@kosha-ai/integration-sdk": "0.8.2"
|
|
56
58
|
}
|
|
57
59
|
}
|