@malloydata/db-postgres 0.0.24-dev230224184907 → 0.0.24-dev230228224434
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.d.ts +1 -1
- package/dist/postgres_connection.d.ts +2 -2
- package/dist/postgres_connection.js +80 -80
- package/package.json +2 -2
package/dist/index.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export { PostgresConnection, PooledPostgresConnection } from
|
|
1
|
+
export { PostgresConnection, PooledPostgresConnection, } from './postgres_connection';
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { Connection, MalloyQueryData, PersistSQLResults, PooledConnection, QueryData, QueryDataRow, RunSQLOptions, SQLBlock, StreamingConnection, StructDef } from
|
|
2
|
-
import { Client } from
|
|
1
|
+
import { Connection, MalloyQueryData, PersistSQLResults, PooledConnection, QueryData, QueryDataRow, RunSQLOptions, SQLBlock, StreamingConnection, StructDef } from '@malloydata/malloy';
|
|
2
|
+
import { Client } from 'pg';
|
|
3
3
|
interface PostgresQueryConfiguration {
|
|
4
4
|
rowLimit?: number;
|
|
5
5
|
}
|
|
@@ -60,31 +60,31 @@ const pg_1 = require("pg");
|
|
|
60
60
|
const pg_query_stream_1 = __importDefault(require("pg-query-stream"));
|
|
61
61
|
const crypto_1 = require("crypto");
|
|
62
62
|
const postgresToMalloyTypes = {
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
63
|
+
'character varying': 'string',
|
|
64
|
+
name: 'string',
|
|
65
|
+
text: 'string',
|
|
66
|
+
date: 'date',
|
|
67
|
+
integer: 'number',
|
|
68
|
+
bigint: 'number',
|
|
69
|
+
'double precision': 'number',
|
|
70
|
+
'timestamp without time zone': 'timestamp',
|
|
71
|
+
oid: 'string',
|
|
72
|
+
boolean: 'boolean',
|
|
73
73
|
// ARRAY: "string",
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
'"char"':
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
74
|
+
'timestamp with time zone': 'timestamp',
|
|
75
|
+
timestamp: 'timestamp',
|
|
76
|
+
'"char"': 'string',
|
|
77
|
+
character: 'string',
|
|
78
|
+
smallint: 'number',
|
|
79
|
+
xid: 'string',
|
|
80
|
+
real: 'number',
|
|
81
|
+
interval: 'string',
|
|
82
|
+
inet: 'string',
|
|
83
|
+
regtype: 'string',
|
|
84
|
+
numeric: 'number',
|
|
85
|
+
bytea: 'string',
|
|
86
|
+
pg_ndistinct: 'number',
|
|
87
|
+
uuid: 'string',
|
|
88
88
|
};
|
|
89
89
|
const DEFAULT_PAGE_SIZE = 1000;
|
|
90
90
|
const SCHEMA_PAGE_SIZE = 1000;
|
|
@@ -113,7 +113,7 @@ class PostgresConnection {
|
|
|
113
113
|
}
|
|
114
114
|
}
|
|
115
115
|
get dialectName() {
|
|
116
|
-
return
|
|
116
|
+
return 'postgres';
|
|
117
117
|
}
|
|
118
118
|
isPool() {
|
|
119
119
|
return false;
|
|
@@ -132,12 +132,12 @@ class PostgresConnection {
|
|
|
132
132
|
if (!inCache) {
|
|
133
133
|
try {
|
|
134
134
|
inCache = {
|
|
135
|
-
|
|
135
|
+
schema: await this.getTableSchema(tableURL),
|
|
136
136
|
};
|
|
137
137
|
this.schemaCache.set(tableURL, inCache);
|
|
138
138
|
}
|
|
139
139
|
catch (error) {
|
|
140
|
-
inCache = {
|
|
140
|
+
inCache = { error: error.message };
|
|
141
141
|
}
|
|
142
142
|
}
|
|
143
143
|
if (inCache.schema !== undefined) {
|
|
@@ -155,11 +155,11 @@ class PostgresConnection {
|
|
|
155
155
|
if (!inCache) {
|
|
156
156
|
try {
|
|
157
157
|
inCache = {
|
|
158
|
-
|
|
158
|
+
structDef: await this.getSQLBlockSchema(sqlRef),
|
|
159
159
|
};
|
|
160
160
|
}
|
|
161
161
|
catch (error) {
|
|
162
|
-
inCache = {
|
|
162
|
+
inCache = { error: error.message };
|
|
163
163
|
}
|
|
164
164
|
this.sqlSchemaCache.set(key, inCache);
|
|
165
165
|
}
|
|
@@ -168,11 +168,11 @@ class PostgresConnection {
|
|
|
168
168
|
async getClient() {
|
|
169
169
|
const config = await this.readConfig();
|
|
170
170
|
return new pg_1.Client({
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
171
|
+
user: config.username,
|
|
172
|
+
password: config.password,
|
|
173
|
+
database: config.databaseName,
|
|
174
|
+
port: config.port,
|
|
175
|
+
host: config.host,
|
|
176
176
|
});
|
|
177
177
|
}
|
|
178
178
|
async runPostgresQuery(sqlCommand, _pageSize, _rowIndex, deJSON) {
|
|
@@ -189,27 +189,27 @@ class PostgresConnection {
|
|
|
189
189
|
}
|
|
190
190
|
await client.end();
|
|
191
191
|
return {
|
|
192
|
-
|
|
193
|
-
|
|
192
|
+
rows: result.rows,
|
|
193
|
+
totalRows: result.rows.length,
|
|
194
194
|
};
|
|
195
195
|
}
|
|
196
196
|
async getSQLBlockSchema(sqlRef) {
|
|
197
197
|
const structDef = {
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
198
|
+
type: 'struct',
|
|
199
|
+
dialect: 'postgres',
|
|
200
|
+
name: sqlRef.name,
|
|
201
|
+
structSource: {
|
|
202
|
+
type: 'sql',
|
|
203
|
+
method: 'subquery',
|
|
204
|
+
sqlBlock: sqlRef,
|
|
205
205
|
},
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
206
|
+
structRelationship: {
|
|
207
|
+
type: 'basetable',
|
|
208
|
+
connectionName: this.name,
|
|
209
209
|
},
|
|
210
|
-
|
|
210
|
+
fields: [],
|
|
211
211
|
};
|
|
212
|
-
const tempTableName = `tmp${(0, crypto_1.randomUUID)()}`.replace(/-/g,
|
|
212
|
+
const tempTableName = `tmp${(0, crypto_1.randomUUID)()}`.replace(/-/g, '');
|
|
213
213
|
const infoQuery = `
|
|
214
214
|
drop table if exists ${tempTableName};
|
|
215
215
|
create temp table ${tempTableName} as SELECT * FROM (
|
|
@@ -227,35 +227,35 @@ class PostgresConnection {
|
|
|
227
227
|
async schemaFromQuery(infoQuery, structDef) {
|
|
228
228
|
const result = await this.runPostgresQuery(infoQuery, SCHEMA_PAGE_SIZE, 0, false);
|
|
229
229
|
for (const row of result.rows) {
|
|
230
|
-
const postgresDataType = row[
|
|
230
|
+
const postgresDataType = row['data_type'];
|
|
231
231
|
let s = structDef;
|
|
232
232
|
let malloyType = postgresToMalloyTypes[postgresDataType];
|
|
233
|
-
let name = row[
|
|
234
|
-
if (postgresDataType ===
|
|
235
|
-
malloyType = postgresToMalloyTypes[row[
|
|
233
|
+
let name = row['column_name'];
|
|
234
|
+
if (postgresDataType === 'ARRAY') {
|
|
235
|
+
malloyType = postgresToMalloyTypes[row['element_type']];
|
|
236
236
|
s = {
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
237
|
+
type: 'struct',
|
|
238
|
+
name: row['column_name'],
|
|
239
|
+
dialect: this.dialectName,
|
|
240
|
+
structRelationship: {
|
|
241
|
+
type: 'nested',
|
|
242
|
+
field: name,
|
|
243
|
+
isArray: true,
|
|
244
244
|
},
|
|
245
|
-
|
|
246
|
-
|
|
245
|
+
structSource: { type: 'nested' },
|
|
246
|
+
fields: [],
|
|
247
247
|
};
|
|
248
248
|
structDef.fields.push(s);
|
|
249
|
-
name =
|
|
249
|
+
name = 'value';
|
|
250
250
|
}
|
|
251
251
|
if (malloyType) {
|
|
252
|
-
s.fields.push({
|
|
252
|
+
s.fields.push({ type: malloyType, name });
|
|
253
253
|
}
|
|
254
254
|
else {
|
|
255
255
|
s.fields.push({
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
name
|
|
256
|
+
type: 'unsupported',
|
|
257
|
+
rawType: postgresDataType.toLowerCase(),
|
|
258
|
+
name,
|
|
259
259
|
});
|
|
260
260
|
}
|
|
261
261
|
}
|
|
@@ -263,19 +263,19 @@ class PostgresConnection {
|
|
|
263
263
|
async getTableSchema(tableURL) {
|
|
264
264
|
const { tablePath } = (0, malloy_1.parseTableURI)(tableURL);
|
|
265
265
|
const structDef = {
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
266
|
+
type: 'struct',
|
|
267
|
+
name: tableURL,
|
|
268
|
+
dialect: 'postgres',
|
|
269
|
+
structSource: { type: 'table', tablePath },
|
|
270
|
+
structRelationship: {
|
|
271
|
+
type: 'basetable',
|
|
272
|
+
connectionName: this.name,
|
|
273
273
|
},
|
|
274
|
-
|
|
274
|
+
fields: [],
|
|
275
275
|
};
|
|
276
|
-
const [schema, table] = tablePath.split(
|
|
276
|
+
const [schema, table] = tablePath.split('.');
|
|
277
277
|
if (table === undefined) {
|
|
278
|
-
throw new Error(
|
|
278
|
+
throw new Error('Default schema not yet supported in Postgres');
|
|
279
279
|
}
|
|
280
280
|
const infoQuery = `
|
|
281
281
|
SELECT column_name, c.data_type, e.data_type as element_type
|
|
@@ -294,7 +294,7 @@ class PostgresConnection {
|
|
|
294
294
|
return queryData.rows;
|
|
295
295
|
}
|
|
296
296
|
async test() {
|
|
297
|
-
await this.executeSQLRaw(
|
|
297
|
+
await this.executeSQLRaw('SELECT 1');
|
|
298
298
|
}
|
|
299
299
|
async runSQL(sql, { rowLimit } = {}, rowIndex = 0) {
|
|
300
300
|
var _a;
|
|
@@ -318,7 +318,7 @@ class PostgresConnection {
|
|
|
318
318
|
await client.end();
|
|
319
319
|
}
|
|
320
320
|
async manifestTemporaryTable(sqlCommand) {
|
|
321
|
-
const hash = crypto.createHash(
|
|
321
|
+
const hash = crypto.createHash('md5').update(sqlCommand).digest('hex');
|
|
322
322
|
const tableName = `tt${hash}`;
|
|
323
323
|
const cmd = `CREATE TEMPORARY TABLE IF NOT EXISTS ${tableName} AS (${sqlCommand});`;
|
|
324
324
|
// console.log(cmd);
|
|
@@ -352,8 +352,8 @@ class PooledPostgresConnection extends PostgresConnection {
|
|
|
352
352
|
}
|
|
353
353
|
}
|
|
354
354
|
return {
|
|
355
|
-
|
|
356
|
-
|
|
355
|
+
rows: result.rows,
|
|
356
|
+
totalRows: result.rows.length,
|
|
357
357
|
};
|
|
358
358
|
}
|
|
359
359
|
async getClientFromPool() {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@malloydata/db-postgres",
|
|
3
|
-
"version": "0.0.24-
|
|
3
|
+
"version": "0.0.24-dev230228224434",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -19,7 +19,7 @@
|
|
|
19
19
|
"prepublishOnly": "npm run build"
|
|
20
20
|
},
|
|
21
21
|
"dependencies": {
|
|
22
|
-
"@malloydata/malloy": "^0.0.24-
|
|
22
|
+
"@malloydata/malloy": "^0.0.24-dev230228224434",
|
|
23
23
|
"@types/pg": "^8.6.1",
|
|
24
24
|
"pg": "^8.7.1",
|
|
25
25
|
"pg-query-stream": "4.2.3"
|