@malloydata/db-bigquery 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.
@@ -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,7 @@ 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
+ return (0, malloy_1.makeDigest)('bigquery', this.projectId, this.setupSQL);
160
167
  }
161
168
  get supportsNesting() {
162
169
  return true;
@@ -214,7 +221,7 @@ class BigQueryConnection extends connection_1.BaseConnection {
214
221
  try {
215
222
  const [result] = await this.bigQuery.createQueryJob({
216
223
  location: this.location,
217
- query: sqlCommand,
224
+ query: this.prependSetupSQL(sqlCommand),
218
225
  dryRun: true,
219
226
  });
220
227
  return result;
@@ -354,7 +361,7 @@ class BigQueryConnection extends connection_1.BaseConnection {
354
361
  throw new Error(`Table ${tableName} already exists`);
355
362
  }
356
363
  const [job] = await this.bigQuery.createQueryJob({
357
- query: sqlCommand,
364
+ query: this.prependSetupSQL(sqlCommand),
358
365
  location: this.location,
359
366
  destination: table,
360
367
  });
@@ -468,7 +475,7 @@ class BigQueryConnection extends connection_1.BaseConnection {
468
475
  try {
469
476
  const [job] = await this.bigQuery.createQueryJob({
470
477
  location: this.location,
471
- query: sqlRef.selectStr,
478
+ query: this.prependSetupSQL(sqlRef.selectStr),
472
479
  dryRun: true,
473
480
  });
474
481
  return job.metadata.statistics.query.schema;
@@ -529,11 +536,15 @@ class BigQueryConnection extends connection_1.BaseConnection {
529
536
  }
530
537
  }
531
538
  async createBigQueryJob(createQueryJobOptions) {
539
+ const options = { ...createQueryJobOptions };
540
+ if (options.query) {
541
+ options.query = this.prependSetupSQL(options.query);
542
+ }
532
543
  const [job] = await this.bigQuery.createQueryJob({
533
544
  location: this.location,
534
545
  maximumBytesBilled: this.config.maximumBytesBilled || MAXIMUM_BYTES_BILLED,
535
546
  jobTimeoutMs: Number(this.config.timeoutMs) || TIMEOUT_MS,
536
- ...createQueryJobOptions,
547
+ ...options,
537
548
  });
538
549
  return job;
539
550
  }
@@ -557,7 +568,7 @@ class BigQueryConnection extends connection_1.BaseConnection {
557
568
  }
558
569
  }
559
570
  this.bigQuery
560
- .createQueryStream(sqlCommand)
571
+ .createQueryStream(this.prependSetupSQL(sqlCommand))
561
572
  .on('error', onError)
562
573
  .on('data', handleData)
563
574
  .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.339",
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.339",
29
29
  "gaxios": "^4.2.0"
30
30
  }
31
31
  }