@malloydata/db-postgres 0.0.337 → 0.0.339

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 CHANGED
@@ -54,6 +54,13 @@ const postgres_connection_2 = require("./postgres_connection");
54
54
  type: 'string',
55
55
  optional: true,
56
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
+ },
57
64
  ],
58
65
  });
59
66
  //# sourceMappingURL=index.js.map
@@ -1,4 +1,4 @@
1
- import type { Connection, ConnectionConfig, MalloyQueryData, PersistSQLResults, PooledConnection, QueryDataRow, QueryOptionsReader, QueryRunStats, RunSQLOptions, SQLSourceDef, TableSourceDef, StreamingConnection, SQLSourceRequest } from '@malloydata/malloy';
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<QueryDataRow>;
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<QueryDataRow>;
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) {
@@ -84,12 +85,11 @@ class PostgresConnection extends connection_1.BaseConnection {
84
85
  getDigest() {
85
86
  // If configReader is an object (not a function), use its properties
86
87
  if (typeof this.configReader !== 'function') {
87
- const { host, port, databaseName, connectionString } = this.configReader;
88
- const data = `postgres:${host !== null && host !== void 0 ? host : ''}:${port !== null && port !== void 0 ? port : ''}:${databaseName !== null && databaseName !== void 0 ? databaseName : ''}:${connectionString !== null && connectionString !== void 0 ? connectionString : ''}`;
89
- return (0, malloy_1.makeDigest)(data);
88
+ const { host, port, username, databaseName, connectionString } = this.configReader;
89
+ return (0, malloy_1.makeDigest)('postgres', host, port !== undefined ? String(port) : undefined, username, databaseName, connectionString, this.setupSQL);
90
90
  }
91
91
  // Fall back to connection name if config is async
92
- return (0, malloy_1.makeDigest)(`postgres:${this.name}`);
92
+ return (0, malloy_1.makeDigest)('postgres', this.name);
93
93
  }
94
94
  get supportsNesting() {
95
95
  return true;
@@ -274,6 +274,14 @@ class PostgresConnection extends connection_1.BaseConnection {
274
274
  }
275
275
  async connectionSetup(client) {
276
276
  await client.query("SET TIME ZONE 'UTC'");
277
+ if (this.setupSQL) {
278
+ for (const stmt of this.setupSQL.split(';\n')) {
279
+ const trimmed = stmt.trim();
280
+ if (trimmed) {
281
+ await client.query(trimmed);
282
+ }
283
+ }
284
+ }
277
285
  }
278
286
  async runSQL(sql, { rowLimit } = {}, rowIndex = 0) {
279
287
  var _a;
@@ -284,6 +292,7 @@ class PostgresConnection extends connection_1.BaseConnection {
284
292
  const query = new pg_query_stream_1.default(sqlCommand);
285
293
  const client = await this.getClient();
286
294
  await client.connect();
295
+ await this.connectionSetup(client);
287
296
  const rowStream = client.query(query);
288
297
  let index = 0;
289
298
  for await (const row of rowStream) {
@@ -302,7 +311,7 @@ class PostgresConnection extends connection_1.BaseConnection {
302
311
  }
303
312
  async manifestTemporaryTable(sqlCommand) {
304
313
  const hash = (0, malloy_1.makeDigest)(sqlCommand);
305
- const tableName = `tt${hash}`;
314
+ const tableName = `tt${hash.slice(0, this.dialect.maxIdentifierLength - 2)}`;
306
315
  const cmd = `CREATE TEMPORARY TABLE IF NOT EXISTS ${tableName} AS (${sqlCommand});`;
307
316
  // console.log(cmd);
308
317
  await this.runPostgresQuery(cmd, 1000, 0, false);
@@ -340,7 +349,17 @@ class PooledPostgresConnection extends PostgresConnection {
340
349
  host,
341
350
  connectionString,
342
351
  });
343
- this._pool.on('acquire', client => client.query("SET TIME ZONE 'UTC'"));
352
+ this._pool.on('acquire', client => {
353
+ client.query("SET TIME ZONE 'UTC'");
354
+ if (this.setupSQL) {
355
+ for (const stmt of this.setupSQL.split(';\n')) {
356
+ const trimmed = stmt.trim();
357
+ if (trimmed) {
358
+ client.query(trimmed);
359
+ }
360
+ }
361
+ }
362
+ });
344
363
  }
345
364
  return this._pool;
346
365
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@malloydata/db-postgres",
3
- "version": "0.0.337",
3
+ "version": "0.0.339",
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.337",
25
+ "@malloydata/malloy": "0.0.339",
26
26
  "@types/pg": "^8.6.1",
27
27
  "pg": "^8.7.1",
28
28
  "pg-query-stream": "4.2.3"