@malloydata/db-bigquery 0.0.337 → 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.
@@ -1,7 +1,7 @@
1
1
  import type { RowMetadata } from '@google-cloud/bigquery';
2
2
  import type bigquery from '@google-cloud/bigquery/build/src/types';
3
3
  import type { ResourceStream } from '@google-cloud/paginator';
4
- import type { Connection, ConnectionConfig, MalloyQueryData, PersistSQLResults, QueryData, QueryDataRow, QueryOptionsReader, QueryRunStats, RunSQLOptions, StreamingConnection, TableSourceDef, SQLSourceDef, SQLSourceRequest } from '@malloydata/malloy';
4
+ import type { Connection, ConnectionConfig, MalloyQueryData, PersistSQLResults, QueryData, QueryRecord, QueryOptionsReader, QueryRunStats, RunSQLOptions, StreamingConnection, TableSourceDef, SQLSourceDef, SQLSourceRequest } from '@malloydata/malloy';
5
5
  import type { TableMetadata } from '@malloydata/malloy/connection';
6
6
  import { BaseConnection } from '@malloydata/malloy/connection';
7
7
  export interface BigQueryManagerOptions {
@@ -26,6 +26,7 @@ interface BigQueryConnectionConfiguration {
26
26
  timeoutMs?: string;
27
27
  billingProjectId?: string;
28
28
  credentials?: CredentialBody;
29
+ setupSQL?: string;
29
30
  }
30
31
  interface BigQueryConnectionOptions extends ConnectionConfig {
31
32
  /** This ID is used for Bigquery Table Normalization */
@@ -37,6 +38,7 @@ interface BigQueryConnectionOptions extends ConnectionConfig {
37
38
  billingProjectId?: string;
38
39
  client_email?: string;
39
40
  private_key?: string;
41
+ setupSQL?: string;
40
42
  }
41
43
  interface SchemaInfo {
42
44
  schema: bigquery.ITableFieldSchema;
@@ -58,9 +60,11 @@ export declare class BigQueryConnection extends BaseConnection implements Connec
58
60
  private queryOptions?;
59
61
  private config;
60
62
  private location?;
63
+ private setupSQL;
61
64
  constructor(option: BigQueryConnectionOptions, queryOptions?: QueryOptionsReader);
62
65
  constructor(name: string, queryOptions?: QueryOptionsReader, config?: BigQueryConnectionConfiguration);
63
66
  get dialectName(): string;
67
+ private prependSetupSQL;
64
68
  private readQueryOptions;
65
69
  canPersist(): this is PersistSQLResults;
66
70
  canStream(): this is StreamingConnection;
@@ -88,7 +92,7 @@ export declare class BigQueryConnection extends BaseConnection implements Connec
88
92
  private createBigQueryJobAndGetResults;
89
93
  private createBigQueryJob;
90
94
  initiateJobAndGetLinkToConsole(sqlCommand: string, dryRun?: boolean): Promise<string>;
91
- runSQLStream(sqlCommand: string, { rowLimit, abortSignal }?: RunSQLOptions): AsyncIterableIterator<QueryDataRow>;
95
+ runSQLStream(sqlCommand: string, { rowLimit, abortSignal }?: RunSQLOptions): AsyncIterableIterator<QueryRecord>;
92
96
  fetchTableMetadata(tablePath: string): Promise<TableMetadata>;
93
97
  close(): Promise<void>;
94
98
  }
@@ -129,10 +129,19 @@ class BigQueryConnection extends connection_1.BaseConnection {
129
129
  this.queryOptions = queryOptions;
130
130
  this.config = config;
131
131
  this.location = config.location;
132
+ this.setupSQL = config.setupSQL;
132
133
  }
133
134
  get dialectName() {
134
135
  return 'standardsql';
135
136
  }
137
+ prependSetupSQL(sql) {
138
+ if (!this.setupSQL)
139
+ return sql;
140
+ const setup = this.setupSQL.trimEnd().endsWith(';')
141
+ ? this.setupSQL
142
+ : this.setupSQL + ';';
143
+ return setup + '\n' + sql;
144
+ }
136
145
  readQueryOptions() {
137
146
  const options = BigQueryConnection.DEFAULT_QUERY_OPTIONS;
138
147
  if (this.queryOptions) {
@@ -154,9 +163,8 @@ class BigQueryConnection extends connection_1.BaseConnection {
154
163
  return true;
155
164
  }
156
165
  getDigest() {
157
- var _a;
158
- const data = `bigquery:${this.projectId}:${(_a = this.location) !== null && _a !== void 0 ? _a : 'US'}`;
159
- return (0, malloy_1.makeDigest)(data);
166
+ var _a, _b;
167
+ return (0, malloy_1.makeDigest)('bigquery', this.billingProjectId, this.projectId, (_a = this.location) !== null && _a !== void 0 ? _a : 'US', (_b = this.setupSQL) !== null && _b !== void 0 ? _b : '');
160
168
  }
161
169
  get supportsNesting() {
162
170
  return true;
@@ -214,7 +222,7 @@ class BigQueryConnection extends connection_1.BaseConnection {
214
222
  try {
215
223
  const [result] = await this.bigQuery.createQueryJob({
216
224
  location: this.location,
217
- query: sqlCommand,
225
+ query: this.prependSetupSQL(sqlCommand),
218
226
  dryRun: true,
219
227
  });
220
228
  return result;
@@ -354,7 +362,7 @@ class BigQueryConnection extends connection_1.BaseConnection {
354
362
  throw new Error(`Table ${tableName} already exists`);
355
363
  }
356
364
  const [job] = await this.bigQuery.createQueryJob({
357
- query: sqlCommand,
365
+ query: this.prependSetupSQL(sqlCommand),
358
366
  location: this.location,
359
367
  destination: table,
360
368
  });
@@ -468,7 +476,7 @@ class BigQueryConnection extends connection_1.BaseConnection {
468
476
  try {
469
477
  const [job] = await this.bigQuery.createQueryJob({
470
478
  location: this.location,
471
- query: sqlRef.selectStr,
479
+ query: this.prependSetupSQL(sqlRef.selectStr),
472
480
  dryRun: true,
473
481
  });
474
482
  return job.metadata.statistics.query.schema;
@@ -529,11 +537,15 @@ class BigQueryConnection extends connection_1.BaseConnection {
529
537
  }
530
538
  }
531
539
  async createBigQueryJob(createQueryJobOptions) {
540
+ const options = { ...createQueryJobOptions };
541
+ if (options.query) {
542
+ options.query = this.prependSetupSQL(options.query);
543
+ }
532
544
  const [job] = await this.bigQuery.createQueryJob({
533
545
  location: this.location,
534
546
  maximumBytesBilled: this.config.maximumBytesBilled || MAXIMUM_BYTES_BILLED,
535
547
  jobTimeoutMs: Number(this.config.timeoutMs) || TIMEOUT_MS,
536
- ...createQueryJobOptions,
548
+ ...options,
537
549
  });
538
550
  return job;
539
551
  }
@@ -557,7 +569,7 @@ class BigQueryConnection extends connection_1.BaseConnection {
557
569
  }
558
570
  }
559
571
  this.bigQuery
560
- .createQueryStream(sqlCommand)
572
+ .createQueryStream(this.prependSetupSQL(sqlCommand))
561
573
  .on('error', onError)
562
574
  .on('data', handleData)
563
575
  .on('end', onEnd);
package/dist/index.js CHANGED
@@ -69,6 +69,13 @@ const bigquery_connection_2 = require("./bigquery_connection");
69
69
  type: 'string',
70
70
  optional: true,
71
71
  },
72
+ {
73
+ name: 'setupSQL',
74
+ displayName: 'Setup SQL',
75
+ type: 'text',
76
+ optional: true,
77
+ description: 'SQL statements to run when the connection is established',
78
+ },
72
79
  ],
73
80
  });
74
81
  //# sourceMappingURL=index.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@malloydata/db-bigquery",
3
- "version": "0.0.337",
3
+ "version": "0.0.338",
4
4
  "license": "MIT",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -25,7 +25,7 @@
25
25
  "@google-cloud/bigquery": "^7.3.0",
26
26
  "@google-cloud/common": "^5.0.1",
27
27
  "@google-cloud/paginator": "^5.0.0",
28
- "@malloydata/malloy": "0.0.337",
28
+ "@malloydata/malloy": "0.0.338",
29
29
  "gaxios": "^4.2.0"
30
30
  }
31
31
  }