@malloydata/db-postgres 0.0.96-dev231025215721 → 0.0.96

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.
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,143 @@
1
+ "use strict";
2
+ /*
3
+ * Copyright 2023 Google LLC
4
+ *
5
+ * Permission is hereby granted, free of charge, to any person obtaining
6
+ * a copy of this software and associated documentation files
7
+ * (the "Software"), to deal in the Software without restriction,
8
+ * including without limitation the rights to use, copy, modify, merge,
9
+ * publish, distribute, sublicense, and/or sell copies of the Software,
10
+ * and to permit persons to whom the Software is furnished to do so,
11
+ * subject to the following conditions:
12
+ *
13
+ * The above copyright notice and this permission notice shall be
14
+ * included in all copies or substantial portions of the Software.
15
+ *
16
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19
+ * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20
+ * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22
+ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
+ */
24
+ Object.defineProperty(exports, "__esModule", { value: true });
25
+ const postgres_connection_1 = require("./postgres_connection");
26
+ /*
27
+ * !IMPORTANT
28
+ *
29
+ * The connection is reused for each test, so if you do not name your tables
30
+ * and keys uniquely for each test you will see cross test interactions.
31
+ */
32
+ describe('PostgresConnection', () => {
33
+ let connection;
34
+ let getTableSchema;
35
+ let getSQLBlockSchema;
36
+ beforeAll(async () => {
37
+ connection = new postgres_connection_1.PostgresConnection('duckdb');
38
+ await connection.runSQL('SELECT 1');
39
+ });
40
+ afterAll(async () => {
41
+ await connection.close();
42
+ });
43
+ beforeEach(async () => {
44
+ getTableSchema = jest
45
+ .spyOn(postgres_connection_1.PostgresConnection.prototype, 'getTableSchema')
46
+ .mockResolvedValue({
47
+ type: 'struct',
48
+ dialect: 'postgres',
49
+ name: 'name',
50
+ structSource: { type: 'table', tablePath: 'test' },
51
+ structRelationship: {
52
+ type: 'basetable',
53
+ connectionName: 'postgres',
54
+ },
55
+ fields: [],
56
+ });
57
+ getSQLBlockSchema = jest
58
+ .spyOn(postgres_connection_1.PostgresConnection.prototype, 'getSQLBlockSchema')
59
+ .mockResolvedValue({
60
+ type: 'struct',
61
+ dialect: 'postgres',
62
+ name: 'name',
63
+ structSource: {
64
+ type: 'sql',
65
+ method: 'subquery',
66
+ sqlBlock: SQL_BLOCK_1,
67
+ },
68
+ structRelationship: {
69
+ type: 'basetable',
70
+ connectionName: 'postgres',
71
+ },
72
+ fields: [],
73
+ });
74
+ });
75
+ afterEach(() => {
76
+ jest.resetAllMocks();
77
+ });
78
+ it('caches table schema', async () => {
79
+ await connection.fetchSchemaForTables({ 'test1': 'table1' }, {});
80
+ expect(getTableSchema).toBeCalledTimes(1);
81
+ await new Promise(resolve => setTimeout(resolve));
82
+ await connection.fetchSchemaForTables({ 'test1': 'table1' }, {});
83
+ expect(getTableSchema).toBeCalledTimes(1);
84
+ });
85
+ it('refreshes table schema', async () => {
86
+ await connection.fetchSchemaForTables({ 'test2': 'table2' }, {});
87
+ expect(getTableSchema).toBeCalledTimes(1);
88
+ await new Promise(resolve => setTimeout(resolve));
89
+ await connection.fetchSchemaForTables({ 'test2': 'table2' }, { refreshTimestamp: Date.now() });
90
+ expect(getTableSchema).toBeCalledTimes(2);
91
+ });
92
+ it('caches sql schema', async () => {
93
+ await connection.fetchSchemaForSQLBlock(SQL_BLOCK_1, {});
94
+ expect(getSQLBlockSchema).toBeCalledTimes(1);
95
+ await new Promise(resolve => setTimeout(resolve));
96
+ await connection.fetchSchemaForSQLBlock(SQL_BLOCK_1, {});
97
+ expect(getSQLBlockSchema).toBeCalledTimes(1);
98
+ });
99
+ it('refreshes sql schema', async () => {
100
+ await connection.fetchSchemaForSQLBlock(SQL_BLOCK_2, {});
101
+ expect(getSQLBlockSchema).toBeCalledTimes(1);
102
+ await new Promise(resolve => setTimeout(resolve));
103
+ await connection.fetchSchemaForSQLBlock(SQL_BLOCK_2, {
104
+ refreshTimestamp: Date.now(),
105
+ });
106
+ expect(getSQLBlockSchema).toBeCalledTimes(2);
107
+ });
108
+ });
109
+ const SQL_BLOCK_1 = {
110
+ type: 'sqlBlock',
111
+ name: 'block1',
112
+ selectStr: `
113
+ SELECT
114
+ created_at,
115
+ sale_price,
116
+ inventory_item_id
117
+ FROM 'order_items.parquet'
118
+ SELECT
119
+ id,
120
+ product_department,
121
+ product_category,
122
+ created_at AS inventory_items_created_at
123
+ FROM "inventory_items.parquet"
124
+ `,
125
+ };
126
+ const SQL_BLOCK_2 = {
127
+ type: 'sqlBlock',
128
+ name: 'block2',
129
+ selectStr: `
130
+ SELECT
131
+ created_at,
132
+ sale_price,
133
+ inventory_item_id
134
+ FROM read_parquet('order_items2.parquet', arg='value')
135
+ SELECT
136
+ id,
137
+ product_department,
138
+ product_category,
139
+ created_at AS inventory_items_created_at
140
+ FROM read_parquet("inventory_items2.parquet")
141
+ `,
142
+ };
143
+ //# sourceMappingURL=postgres.spec.js.map
@@ -1,5 +1,6 @@
1
1
  import { Connection, MalloyQueryData, PersistSQLResults, PooledConnection, QueryData, QueryDataRow, QueryRunStats, RunSQLOptions, SQLBlock, StreamingConnection, StructDef } from '@malloydata/malloy';
2
2
  import { Client } from 'pg';
3
+ import { FetchSchemaOptions } from '@malloydata/malloy-interfaces';
3
4
  interface PostgresQueryConfiguration {
4
5
  rowLimit?: number;
5
6
  }
@@ -28,11 +29,11 @@ export declare class PostgresConnection implements Connection, StreamingConnecti
28
29
  canPersist(): this is PersistSQLResults;
29
30
  canStream(): this is StreamingConnection;
30
31
  get supportsNesting(): boolean;
31
- fetchSchemaForTables(missing: Record<string, string>): Promise<{
32
+ fetchSchemaForTables(missing: Record<string, string>, { refreshTimestamp }: FetchSchemaOptions): Promise<{
32
33
  schemas: Record<string, StructDef>;
33
34
  errors: Record<string, string>;
34
35
  }>;
35
- fetchSchemaForSQLBlock(sqlRef: SQLBlock): Promise<{
36
+ fetchSchemaForSQLBlock(sqlRef: SQLBlock, { refreshTimestamp }: FetchSchemaOptions): Promise<{
36
37
  structDef: StructDef;
37
38
  error?: undefined;
38
39
  } | {
@@ -102,21 +102,24 @@ class PostgresConnection {
102
102
  get supportsNesting() {
103
103
  return true;
104
104
  }
105
- async fetchSchemaForTables(missing) {
105
+ async fetchSchemaForTables(missing, { refreshTimestamp }) {
106
106
  const schemas = {};
107
107
  const errors = {};
108
108
  for (const tableKey in missing) {
109
109
  let inCache = this.schemaCache.get(tableKey);
110
- if (!inCache) {
110
+ if (!inCache ||
111
+ (refreshTimestamp && refreshTimestamp > inCache.timestamp)) {
111
112
  const tablePath = missing[tableKey];
113
+ const timestamp = refreshTimestamp || Date.now();
112
114
  try {
113
115
  inCache = {
114
116
  schema: await this.getTableSchema(tableKey, tablePath),
117
+ timestamp,
115
118
  };
116
119
  this.schemaCache.set(tableKey, inCache);
117
120
  }
118
121
  catch (error) {
119
- inCache = { error: error.message };
122
+ inCache = { error: error.message, timestamp };
120
123
  }
121
124
  }
122
125
  if (inCache.schema !== undefined) {
@@ -128,17 +131,20 @@ class PostgresConnection {
128
131
  }
129
132
  return { schemas, errors };
130
133
  }
131
- async fetchSchemaForSQLBlock(sqlRef) {
134
+ async fetchSchemaForSQLBlock(sqlRef, { refreshTimestamp }) {
132
135
  const key = sqlRef.name;
133
136
  let inCache = this.sqlSchemaCache.get(key);
134
- if (!inCache) {
137
+ if (!inCache ||
138
+ (refreshTimestamp && refreshTimestamp > inCache.timestamp)) {
139
+ const timestamp = refreshTimestamp !== null && refreshTimestamp !== void 0 ? refreshTimestamp : Date.now();
135
140
  try {
136
141
  inCache = {
137
142
  structDef: await this.getSQLBlockSchema(sqlRef),
143
+ timestamp,
138
144
  };
139
145
  }
140
146
  catch (error) {
141
- inCache = { error: error.message };
147
+ inCache = { error: error.message, timestamp };
142
148
  }
143
149
  this.sqlSchemaCache.set(key, inCache);
144
150
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@malloydata/db-postgres",
3
- "version": "0.0.96-dev231025215721",
3
+ "version": "0.0.96",
4
4
  "license": "MIT",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -22,7 +22,8 @@
22
22
  "prepublishOnly": "npm run build"
23
23
  },
24
24
  "dependencies": {
25
- "@malloydata/malloy": "^0.0.96-dev231025215721",
25
+ "@malloydata/malloy": "^0.0.96",
26
+ "@malloydata/malloy-interfaces": "^0.0.96",
26
27
  "@types/pg": "^8.6.1",
27
28
  "pg": "^8.7.1",
28
29
  "pg-query-stream": "4.2.3"