@malloydata/db-postgres 0.0.100-dev231106165927 → 0.0.100-dev231106210358
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,5 +1,5 @@
|
|
|
1
|
-
import { Connection, MalloyQueryData, PersistSQLResults, PooledConnection,
|
|
2
|
-
import { Client } from 'pg';
|
|
1
|
+
import { Connection, MalloyQueryData, PersistSQLResults, PooledConnection, QueryDataRow, QueryRunStats, RunSQLOptions, SQLBlock, StreamingConnection, StructDef } from '@malloydata/malloy';
|
|
2
|
+
import { Client, Pool, QueryResult } from 'pg';
|
|
3
3
|
import { FetchSchemaOptions } from '@malloydata/malloy-interfaces';
|
|
4
4
|
interface PostgresQueryConfiguration {
|
|
5
5
|
rowLimit?: number;
|
|
@@ -23,7 +23,7 @@ export declare class PostgresConnection implements Connection, StreamingConnecti
|
|
|
23
23
|
private sqlSchemaCache;
|
|
24
24
|
constructor(name: string, queryConfigReader?: PostgresQueryConfigurationReader, configReader?: PostgresConnectionConfigurationReader);
|
|
25
25
|
private readQueryConfig;
|
|
26
|
-
|
|
26
|
+
protected readConfig(): Promise<PostgresConnectionConfiguration>;
|
|
27
27
|
get dialectName(): string;
|
|
28
28
|
isPool(): this is PooledConnection;
|
|
29
29
|
canPersist(): this is PersistSQLResults;
|
|
@@ -45,9 +45,9 @@ export declare class PostgresConnection implements Connection, StreamingConnecti
|
|
|
45
45
|
private getSQLBlockSchema;
|
|
46
46
|
private schemaFromQuery;
|
|
47
47
|
private getTableSchema;
|
|
48
|
-
executeSQLRaw(query: string): Promise<
|
|
48
|
+
executeSQLRaw(query: string): Promise<QueryResult>;
|
|
49
49
|
test(): Promise<void>;
|
|
50
|
-
connectionSetup(): Promise<void>;
|
|
50
|
+
connectionSetup(client: Client): Promise<void>;
|
|
51
51
|
runSQL(sql: string, { rowLimit }?: RunSQLOptions, rowIndex?: number): Promise<MalloyQueryData>;
|
|
52
52
|
runSQLStream(sqlCommand: string, options?: {
|
|
53
53
|
rowLimit?: number;
|
|
@@ -57,12 +57,12 @@ export declare class PostgresConnection implements Connection, StreamingConnecti
|
|
|
57
57
|
close(): Promise<void>;
|
|
58
58
|
}
|
|
59
59
|
export declare class PooledPostgresConnection extends PostgresConnection implements PooledConnection {
|
|
60
|
-
private
|
|
60
|
+
private _pool;
|
|
61
61
|
constructor(name: string, queryConfigReader?: PostgresQueryConfigurationReader, configReader?: PostgresConnectionConfigurationReader);
|
|
62
62
|
isPool(): true;
|
|
63
63
|
drain(): Promise<void>;
|
|
64
|
+
getPool(): Promise<Pool>;
|
|
64
65
|
protected runPostgresQuery(sqlCommand: string, _pageSize: number, _rowIndex: number, deJSON: boolean): Promise<MalloyQueryData>;
|
|
65
|
-
private getClientFromPool;
|
|
66
66
|
runSQLStream(sqlCommand: string, options?: {
|
|
67
67
|
rowLimit?: number;
|
|
68
68
|
}): AsyncIterableIterator<QueryDataRow>;
|
|
@@ -163,7 +163,7 @@ class PostgresConnection {
|
|
|
163
163
|
async runPostgresQuery(sqlCommand, _pageSize, _rowIndex, deJSON) {
|
|
164
164
|
const client = await this.getClient();
|
|
165
165
|
await client.connect();
|
|
166
|
-
await this.connectionSetup();
|
|
166
|
+
await this.connectionSetup(client);
|
|
167
167
|
let result = await client.query(sqlCommand);
|
|
168
168
|
if (Array.isArray(result)) {
|
|
169
169
|
result = result.pop();
|
|
@@ -274,18 +274,17 @@ class PostgresConnection {
|
|
|
274
274
|
return structDef;
|
|
275
275
|
}
|
|
276
276
|
async executeSQLRaw(query) {
|
|
277
|
-
const
|
|
278
|
-
|
|
279
|
-
|
|
277
|
+
const client = await this.getClient();
|
|
278
|
+
await client.connect();
|
|
279
|
+
const results = await client.query(query);
|
|
280
|
+
await client.end();
|
|
281
|
+
return results;
|
|
280
282
|
}
|
|
281
283
|
async test() {
|
|
282
284
|
await this.executeSQLRaw('SELECT 1');
|
|
283
285
|
}
|
|
284
|
-
async connectionSetup() {
|
|
285
|
-
|
|
286
|
-
this.executeSQLRaw("SET TIME ZONE 'UTC'");
|
|
287
|
-
this.isSetup = true;
|
|
288
|
-
}
|
|
286
|
+
async connectionSetup(client) {
|
|
287
|
+
await client.query("SET TIME ZONE 'UTC'");
|
|
289
288
|
}
|
|
290
289
|
async runSQL(sql, { rowLimit } = {}, rowIndex = 0) {
|
|
291
290
|
var _a;
|
|
@@ -328,17 +327,31 @@ exports.PostgresConnection = PostgresConnection;
|
|
|
328
327
|
class PooledPostgresConnection extends PostgresConnection {
|
|
329
328
|
constructor(name, queryConfigReader = {}, configReader = {}) {
|
|
330
329
|
super(name, queryConfigReader, configReader);
|
|
331
|
-
this.pool = new pg_1.Pool();
|
|
332
|
-
this.pool.on('acquire', client => client.query("SET TIME ZONE 'UTC'"));
|
|
333
330
|
}
|
|
334
331
|
isPool() {
|
|
335
332
|
return true;
|
|
336
333
|
}
|
|
337
334
|
async drain() {
|
|
338
|
-
|
|
335
|
+
var _a;
|
|
336
|
+
await ((_a = this._pool) === null || _a === void 0 ? void 0 : _a.end());
|
|
337
|
+
}
|
|
338
|
+
async getPool() {
|
|
339
|
+
if (!this._pool) {
|
|
340
|
+
const { username: user, password, databaseName: database, port, host, } = await this.readConfig();
|
|
341
|
+
this._pool = new pg_1.Pool({
|
|
342
|
+
user,
|
|
343
|
+
password,
|
|
344
|
+
database,
|
|
345
|
+
port,
|
|
346
|
+
host,
|
|
347
|
+
});
|
|
348
|
+
this._pool.on('acquire', client => client.query("SET TIME ZONE 'UTC'"));
|
|
349
|
+
}
|
|
350
|
+
return this._pool;
|
|
339
351
|
}
|
|
340
352
|
async runPostgresQuery(sqlCommand, _pageSize, _rowIndex, deJSON) {
|
|
341
|
-
|
|
353
|
+
const pool = await this.getPool();
|
|
354
|
+
let result = await pool.query(sqlCommand);
|
|
342
355
|
if (Array.isArray(result)) {
|
|
343
356
|
result = result.pop();
|
|
344
357
|
}
|
|
@@ -352,16 +365,6 @@ class PooledPostgresConnection extends PostgresConnection {
|
|
|
352
365
|
totalRows: result.rows.length,
|
|
353
366
|
};
|
|
354
367
|
}
|
|
355
|
-
async getClientFromPool() {
|
|
356
|
-
return await new Promise((resolve, reject) => this.pool.connect((error, client, releaseClient) => {
|
|
357
|
-
if (error) {
|
|
358
|
-
reject(error);
|
|
359
|
-
}
|
|
360
|
-
else {
|
|
361
|
-
resolve([client, releaseClient]);
|
|
362
|
-
}
|
|
363
|
-
}));
|
|
364
|
-
}
|
|
365
368
|
async *runSQLStream(sqlCommand, options) {
|
|
366
369
|
const query = new pg_query_stream_1.default(sqlCommand);
|
|
367
370
|
let index = 0;
|
|
@@ -369,7 +372,8 @@ class PooledPostgresConnection extends PostgresConnection {
|
|
|
369
372
|
// type. Because `query` is a `QueryStream`, the result is supposed to be a
|
|
370
373
|
// `QueryStream` as well, but it's not. So instead, we get a client and call
|
|
371
374
|
// `client.query(query)`, which does what it's supposed to.
|
|
372
|
-
const
|
|
375
|
+
const pool = await this.getPool();
|
|
376
|
+
const client = await pool.connect();
|
|
373
377
|
const resultStream = client.query(query);
|
|
374
378
|
for await (const row of resultStream) {
|
|
375
379
|
yield row.row;
|
|
@@ -379,7 +383,7 @@ class PooledPostgresConnection extends PostgresConnection {
|
|
|
379
383
|
break;
|
|
380
384
|
}
|
|
381
385
|
}
|
|
382
|
-
|
|
386
|
+
client.release();
|
|
383
387
|
}
|
|
384
388
|
async close() {
|
|
385
389
|
await this.drain();
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@malloydata/db-postgres",
|
|
3
|
-
"version": "0.0.100-
|
|
3
|
+
"version": "0.0.100-dev231106210358",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -22,8 +22,8 @@
|
|
|
22
22
|
"prepublishOnly": "npm run build"
|
|
23
23
|
},
|
|
24
24
|
"dependencies": {
|
|
25
|
-
"@malloydata/malloy": "^0.0.100-
|
|
26
|
-
"@malloydata/malloy-interfaces": "^0.0.100-
|
|
25
|
+
"@malloydata/malloy": "^0.0.100-dev231106210358",
|
|
26
|
+
"@malloydata/malloy-interfaces": "^0.0.100-dev231106210358",
|
|
27
27
|
"@types/pg": "^8.6.1",
|
|
28
28
|
"pg": "^8.7.1",
|
|
29
29
|
"pg-query-stream": "4.2.3"
|