@malloydata/db-postgres 0.0.336 → 0.0.338
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/dist/index.js +37 -0
- package/dist/postgres_connection.d.ts +5 -3
- package/dist/postgres_connection.js +27 -7
- package/package.json +2 -2
package/dist/index.js
CHANGED
|
@@ -26,4 +26,41 @@ exports.PooledPostgresConnection = exports.PostgresConnection = void 0;
|
|
|
26
26
|
var postgres_connection_1 = require("./postgres_connection");
|
|
27
27
|
Object.defineProperty(exports, "PostgresConnection", { enumerable: true, get: function () { return postgres_connection_1.PostgresConnection; } });
|
|
28
28
|
Object.defineProperty(exports, "PooledPostgresConnection", { enumerable: true, get: function () { return postgres_connection_1.PooledPostgresConnection; } });
|
|
29
|
+
const malloy_1 = require("@malloydata/malloy");
|
|
30
|
+
const postgres_connection_2 = require("./postgres_connection");
|
|
31
|
+
(0, malloy_1.registerConnectionType)('postgres', {
|
|
32
|
+
factory: (config) => {
|
|
33
|
+
return new postgres_connection_2.PostgresConnection(config);
|
|
34
|
+
},
|
|
35
|
+
properties: [
|
|
36
|
+
{ name: 'host', displayName: 'Host', type: 'string', optional: true },
|
|
37
|
+
{ name: 'port', displayName: 'Port', type: 'number', optional: true },
|
|
38
|
+
{ name: 'username', displayName: 'Username', type: 'string', optional: true },
|
|
39
|
+
{
|
|
40
|
+
name: 'password',
|
|
41
|
+
displayName: 'Password',
|
|
42
|
+
type: 'password',
|
|
43
|
+
optional: true,
|
|
44
|
+
},
|
|
45
|
+
{
|
|
46
|
+
name: 'databaseName',
|
|
47
|
+
displayName: 'Database Name',
|
|
48
|
+
type: 'string',
|
|
49
|
+
optional: true,
|
|
50
|
+
},
|
|
51
|
+
{
|
|
52
|
+
name: 'connectionString',
|
|
53
|
+
displayName: 'Connection String',
|
|
54
|
+
type: 'string',
|
|
55
|
+
optional: true,
|
|
56
|
+
},
|
|
57
|
+
{
|
|
58
|
+
name: 'setupSQL',
|
|
59
|
+
displayName: 'Setup SQL',
|
|
60
|
+
type: 'text',
|
|
61
|
+
optional: true,
|
|
62
|
+
description: 'SQL statements to run when the connection is established',
|
|
63
|
+
},
|
|
64
|
+
],
|
|
65
|
+
});
|
|
29
66
|
//# sourceMappingURL=index.js.map
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { Connection, ConnectionConfig, MalloyQueryData, PersistSQLResults, PooledConnection,
|
|
1
|
+
import type { Connection, ConnectionConfig, MalloyQueryData, PersistSQLResults, PooledConnection, QueryRecord, QueryOptionsReader, QueryRunStats, RunSQLOptions, SQLSourceDef, TableSourceDef, StreamingConnection, SQLSourceRequest } from '@malloydata/malloy';
|
|
2
2
|
import { BaseConnection } from '@malloydata/malloy/connection';
|
|
3
3
|
import { Client, Pool } from 'pg';
|
|
4
4
|
interface PostgresConnectionConfiguration {
|
|
@@ -8,12 +8,14 @@ interface PostgresConnectionConfiguration {
|
|
|
8
8
|
password?: string;
|
|
9
9
|
databaseName?: string;
|
|
10
10
|
connectionString?: string;
|
|
11
|
+
setupSQL?: string;
|
|
11
12
|
}
|
|
12
13
|
type PostgresConnectionConfigurationReader = PostgresConnectionConfiguration | (() => Promise<PostgresConnectionConfiguration>);
|
|
13
14
|
export interface PostgresConnectionOptions extends ConnectionConfig, PostgresConnectionConfiguration {
|
|
14
15
|
}
|
|
15
16
|
export declare class PostgresConnection extends BaseConnection implements Connection, StreamingConnection, PersistSQLResults {
|
|
16
17
|
readonly name: string;
|
|
18
|
+
protected setupSQL: string | undefined;
|
|
17
19
|
private queryOptionsReader;
|
|
18
20
|
private configReader;
|
|
19
21
|
private readonly dialect;
|
|
@@ -35,7 +37,7 @@ export declare class PostgresConnection extends BaseConnection implements Connec
|
|
|
35
37
|
test(): Promise<void>;
|
|
36
38
|
connectionSetup(client: Client): Promise<void>;
|
|
37
39
|
runSQL(sql: string, { rowLimit }?: RunSQLOptions, rowIndex?: number): Promise<MalloyQueryData>;
|
|
38
|
-
runSQLStream(sqlCommand: string, { rowLimit, abortSignal }?: RunSQLOptions): AsyncIterableIterator<
|
|
40
|
+
runSQLStream(sqlCommand: string, { rowLimit, abortSignal }?: RunSQLOptions): AsyncIterableIterator<QueryRecord>;
|
|
39
41
|
estimateQueryCost(_: string): Promise<QueryRunStats>;
|
|
40
42
|
manifestTemporaryTable(sqlCommand: string): Promise<string>;
|
|
41
43
|
close(): Promise<void>;
|
|
@@ -48,7 +50,7 @@ export declare class PooledPostgresConnection extends PostgresConnection impleme
|
|
|
48
50
|
drain(): Promise<void>;
|
|
49
51
|
getPool(): Promise<Pool>;
|
|
50
52
|
protected runPostgresQuery(sqlCommand: string, _pageSize: number, _rowIndex: number, deJSON: boolean): Promise<MalloyQueryData>;
|
|
51
|
-
runSQLStream(sqlCommand: string, { rowLimit, abortSignal }?: RunSQLOptions): AsyncIterableIterator<
|
|
53
|
+
runSQLStream(sqlCommand: string, { rowLimit, abortSignal }?: RunSQLOptions): AsyncIterableIterator<QueryRecord>;
|
|
52
54
|
close(): Promise<void>;
|
|
53
55
|
}
|
|
54
56
|
export {};
|
|
@@ -45,8 +45,9 @@ class PostgresConnection extends connection_1.BaseConnection {
|
|
|
45
45
|
}
|
|
46
46
|
}
|
|
47
47
|
else {
|
|
48
|
-
const { name, ...configReader } = arg;
|
|
48
|
+
const { name, setupSQL, ...configReader } = arg;
|
|
49
49
|
this.name = name;
|
|
50
|
+
this.setupSQL = setupSQL;
|
|
50
51
|
this.configReader = configReader;
|
|
51
52
|
}
|
|
52
53
|
if (queryOptionsReader) {
|
|
@@ -82,14 +83,14 @@ class PostgresConnection extends connection_1.BaseConnection {
|
|
|
82
83
|
return true;
|
|
83
84
|
}
|
|
84
85
|
getDigest() {
|
|
86
|
+
var _a;
|
|
85
87
|
// If configReader is an object (not a function), use its properties
|
|
86
88
|
if (typeof this.configReader !== 'function') {
|
|
87
|
-
const { host, port, databaseName, connectionString } = this.configReader;
|
|
88
|
-
|
|
89
|
-
return (0, malloy_1.makeDigest)(data);
|
|
89
|
+
const { host, port, username, databaseName, connectionString } = this.configReader;
|
|
90
|
+
return (0, malloy_1.makeDigest)('postgres', host !== null && host !== void 0 ? host : '', String(port !== null && port !== void 0 ? port : ''), username !== null && username !== void 0 ? username : '', databaseName !== null && databaseName !== void 0 ? databaseName : '', connectionString !== null && connectionString !== void 0 ? connectionString : '', (_a = this.setupSQL) !== null && _a !== void 0 ? _a : '');
|
|
90
91
|
}
|
|
91
92
|
// Fall back to connection name if config is async
|
|
92
|
-
return (0, malloy_1.makeDigest)(
|
|
93
|
+
return (0, malloy_1.makeDigest)('postgres', this.name);
|
|
93
94
|
}
|
|
94
95
|
get supportsNesting() {
|
|
95
96
|
return true;
|
|
@@ -274,6 +275,14 @@ class PostgresConnection extends connection_1.BaseConnection {
|
|
|
274
275
|
}
|
|
275
276
|
async connectionSetup(client) {
|
|
276
277
|
await client.query("SET TIME ZONE 'UTC'");
|
|
278
|
+
if (this.setupSQL) {
|
|
279
|
+
for (const stmt of this.setupSQL.split(';\n')) {
|
|
280
|
+
const trimmed = stmt.trim();
|
|
281
|
+
if (trimmed) {
|
|
282
|
+
await client.query(trimmed);
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
}
|
|
277
286
|
}
|
|
278
287
|
async runSQL(sql, { rowLimit } = {}, rowIndex = 0) {
|
|
279
288
|
var _a;
|
|
@@ -284,6 +293,7 @@ class PostgresConnection extends connection_1.BaseConnection {
|
|
|
284
293
|
const query = new pg_query_stream_1.default(sqlCommand);
|
|
285
294
|
const client = await this.getClient();
|
|
286
295
|
await client.connect();
|
|
296
|
+
await this.connectionSetup(client);
|
|
287
297
|
const rowStream = client.query(query);
|
|
288
298
|
let index = 0;
|
|
289
299
|
for await (const row of rowStream) {
|
|
@@ -302,7 +312,7 @@ class PostgresConnection extends connection_1.BaseConnection {
|
|
|
302
312
|
}
|
|
303
313
|
async manifestTemporaryTable(sqlCommand) {
|
|
304
314
|
const hash = (0, malloy_1.makeDigest)(sqlCommand);
|
|
305
|
-
const tableName = `tt${hash}`;
|
|
315
|
+
const tableName = `tt${hash.slice(0, this.dialect.maxIdentifierLength - 2)}`;
|
|
306
316
|
const cmd = `CREATE TEMPORARY TABLE IF NOT EXISTS ${tableName} AS (${sqlCommand});`;
|
|
307
317
|
// console.log(cmd);
|
|
308
318
|
await this.runPostgresQuery(cmd, 1000, 0, false);
|
|
@@ -340,7 +350,17 @@ class PooledPostgresConnection extends PostgresConnection {
|
|
|
340
350
|
host,
|
|
341
351
|
connectionString,
|
|
342
352
|
});
|
|
343
|
-
this._pool.on('acquire', client =>
|
|
353
|
+
this._pool.on('acquire', client => {
|
|
354
|
+
client.query("SET TIME ZONE 'UTC'");
|
|
355
|
+
if (this.setupSQL) {
|
|
356
|
+
for (const stmt of this.setupSQL.split(';\n')) {
|
|
357
|
+
const trimmed = stmt.trim();
|
|
358
|
+
if (trimmed) {
|
|
359
|
+
client.query(trimmed);
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
});
|
|
344
364
|
}
|
|
345
365
|
return this._pool;
|
|
346
366
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@malloydata/db-postgres",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.338",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -22,7 +22,7 @@
|
|
|
22
22
|
"prepublishOnly": "npm run build"
|
|
23
23
|
},
|
|
24
24
|
"dependencies": {
|
|
25
|
-
"@malloydata/malloy": "0.0.
|
|
25
|
+
"@malloydata/malloy": "0.0.338",
|
|
26
26
|
"@types/pg": "^8.6.1",
|
|
27
27
|
"pg": "^8.7.1",
|
|
28
28
|
"pg-query-stream": "4.2.3"
|