diff --git a/src/bigquery.ts b/src/bigquery.ts index 0cfc686a..8431ce1c 100644 --- a/src/bigquery.ts +++ b/src/bigquery.ts @@ -1199,7 +1199,12 @@ export class BigQuery extends Service { ); } else if (typeName === 'STRUCT') { queryParameter.parameterValue!.structValues = Object.keys(value).reduce( - (structValues, prop) => { + ( + structValues: { + [key: string]: bigquery.IQueryParameterValue; + }, + prop: string + ) => { let nestedQueryParameter; if (providedType) { nestedQueryParameter = BigQuery.valueToQueryParameter_( @@ -1209,8 +1214,9 @@ export class BigQuery extends Service { } else { nestedQueryParameter = BigQuery.valueToQueryParameter_(value[prop]); } - // eslint-disable-next-line @typescript-eslint/no-explicit-any - (structValues as any)[prop] = nestedQueryParameter.parameterValue; + if (nestedQueryParameter.parameterValue !== undefined) { + structValues[prop] = nestedQueryParameter.parameterValue; + } return structValues; }, {} @@ -1525,11 +1531,9 @@ export class BigQuery extends Service { delete query.params; } - // eslint-disable-next-line @typescript-eslint/no-explicit-any - const reqOpts: any = { - configuration: { - query, - }, + const reqOpts: JobOptions = {}; + reqOpts.configuration = { + query, }; if (typeof query.jobTimeoutMs === 'number') { @@ -1800,7 +1804,7 @@ export class BigQuery extends Service { if (options.projectId) { reqOpts.projectId = options.projectId; } - this.request(reqOpts, (err, resp) => { + this.request(reqOpts, (err, resp: bigquery.IDatasetList) => { if (err) { callback!(err, null, null, resp); return; @@ -1814,7 +1818,6 @@ export class BigQuery extends Service { }); } - // eslint-disable-next-line @typescript-eslint/no-explicit-any const datasets = (resp.datasets || []).map( (dataset: bigquery.IDataset) => { const dsOpts: DatasetOptions = { @@ -1932,7 +1935,8 @@ export class BigQuery extends Service { } const jobs = (resp.jobs || []).map((jobObject: bigquery.IJob) => { const job = this.job(jobObject.jobReference!.jobId!, { - location: jobObject.jobReference!.location!, + location: jobObject.jobReference?.location, + projectId: jobObject.jobReference?.projectId, }); job.metadata = jobObject!; return job; @@ -1962,6 +1966,7 @@ export class BigQuery extends Service { if (this.location) { options = extend({location: this.location}, options); } + options = extend({projectId: this.projectId}, options); return new Job(this, id, options); } diff --git a/src/dataset.ts b/src/dataset.ts index e2347436..3cb00202 100644 --- a/src/dataset.ts +++ b/src/dataset.ts @@ -739,8 +739,7 @@ class Dataset extends ServiceObject { const callback = typeof optionsOrCallback === 'function' ? optionsOrCallback : cb; const body = Table.formatMetadata_(options as TableMetadata); - // eslint-disable-next-line @typescript-eslint/no-explicit-any - (body as any).tableReference = { + body.tableReference = { datasetId: this.id, projectId: this.projectId, tableId: id, @@ -1163,7 +1162,7 @@ class Dataset extends ServiceObject { uri: '/tables', qs: options, }, - (err, resp) => { + (err, resp: bigquery.ITableList) => { if (err) { callback!(err, null, null, resp); return; @@ -1176,11 +1175,9 @@ class Dataset extends ServiceObject { }); } - // eslint-disable-next-line @typescript-eslint/no-explicit-any - const tables = (resp.tables || []).map((tableObject: any) => { - const table = this.table(tableObject.tableReference.tableId, { - location: tableObject.location, - }); + const tables = (resp.tables || []).map(tableObject => { + const tableRef = tableObject.tableReference!; + const table = this.table(tableRef.tableId!); table.metadata = tableObject; return table; }); diff --git a/src/model.ts b/src/model.ts index ea0ea73a..bd478c05 100644 --- a/src/model.ts +++ b/src/model.ts @@ -26,7 +26,7 @@ import { RequestCallback, JobRequest, } from '.'; -import {JobMetadata} from './job'; +import {JobMetadata, JobOptions} from './job'; import bigquery from './types'; // This is supposed to be a @google-cloud/storage `File` type. The storage npm @@ -424,8 +424,7 @@ class Model extends ServiceObject { } } - // eslint-disable-next-line @typescript-eslint/no-explicit-any - const body: any = { + const body: JobOptions = { configuration: { extract: extend(true, options, { sourceModel: { diff --git a/src/table.ts b/src/table.ts index 6897bebb..d78519ef 100644 --- a/src/table.ts +++ b/src/table.ts @@ -50,7 +50,7 @@ import { } from '.'; import {GoogleErrorBody} from '@google-cloud/common/build/src/util'; import {Duplex, Writable} from 'stream'; -import {JobMetadata} from './job'; +import {JobMetadata, JobOptions} from './job'; import bigquery from './types'; import {IntegerTypeCastOptions} from './bigquery'; import {RowQueue} from './rowQueue'; @@ -923,8 +923,7 @@ class Table extends ServiceObject { const callback = typeof metadataOrCallback === 'function' ? metadataOrCallback : cb; - // eslint-disable-next-line @typescript-eslint/no-explicit-any - const body: any = { + const body: JobOptions = { configuration: { copy: extend(true, metadata, { destinationTable: { @@ -1045,8 +1044,7 @@ class Table extends ServiceObject { const callback = typeof metadataOrCallback === 'function' ? metadataOrCallback : cb; - // eslint-disable-next-line @typescript-eslint/no-explicit-any - const body: any = { + const body: JobOptions = { configuration: { copy: extend(true, metadata, { destinationTable: { @@ -1218,8 +1216,7 @@ class Table extends ServiceObject { delete options.gzip; } - // eslint-disable-next-line @typescript-eslint/no-explicit-any - const body: any = { + const body: JobOptions = { configuration: { extract: extend(true, options, { sourceTable: { @@ -1399,15 +1396,13 @@ class Table extends ServiceObject { return [jobResponse, jobResponse.metadata]; } - // eslint-disable-next-line @typescript-eslint/no-explicit-any - const body: any = { - configuration: { - load: { - destinationTable: { - projectId: this.dataset.projectId, - datasetId: this.dataset.id, - tableId: this.id, - }, + const body: JobOptions = {}; + body.configuration = { + load: { + destinationTable: { + projectId: this.dataset.projectId, + datasetId: this.dataset.id, + tableId: this.id, }, }, }; @@ -1438,7 +1433,9 @@ class Table extends ServiceObject { // to CSV. const format = FORMATS[path.extname(src.name).substr(1).toLowerCase()]; if (!metadata.sourceFormat && format) { - body.configuration.load.sourceFormat = format; + if (body.configuration && body.configuration.load) { + body.configuration.load.sourceFormat = format; + } } return 'gs://' + src.bucket.name + '/' + src.name; }), @@ -1550,11 +1547,11 @@ class Table extends ServiceObject { uri: `${this.bigQuery.apiEndpoint}/upload/bigquery/v2/projects/${this.dataset.projectId}/jobs`, }, }, - // eslint-disable-next-line @typescript-eslint/no-explicit-any - (data: any) => { - const job = this.bigQuery.job(data.jobReference.jobId, { - location: data.jobReference.location, - projectId: data.jobReference.projectId, + (data: JobMetadata) => { + const jobRef = data.jobReference!; + const job = this.bigQuery.job(jobRef.jobId!, { + location: jobRef.location, + projectId: jobRef.projectId, }); job.metadata = data; dup.emit('job', job); diff --git a/test/bigquery.ts b/test/bigquery.ts index 8efa158d..032a8748 100644 --- a/test/bigquery.ts +++ b/test/bigquery.ts @@ -150,6 +150,7 @@ describe('BigQuery', () => { const PROJECT_ID = 'test-project'; const ANOTHER_PROJECT_ID = 'another-test-project'; const LOCATION = 'asia-northeast1'; + const ANOTHER_LOCATION = 'us-central1'; // eslint-disable-next-line @typescript-eslint/no-explicit-any let BigQueryCached: any; @@ -2667,6 +2668,7 @@ describe('BigQuery', () => { jobReference: { jobId: JOB_ID, location: LOCATION, + projectId: PROJECT_ID, }, }, ], @@ -2682,7 +2684,10 @@ describe('BigQuery', () => { assert(job instanceof FakeJob); assert.strictEqual(args[0], bq); assert.strictEqual(args[1], JOB_ID); - assert.deepStrictEqual(args[2], {location: LOCATION}); + assert.deepStrictEqual(args[2], { + location: LOCATION, + projectId: PROJECT_ID, + }); done(); }); }); @@ -2766,19 +2771,27 @@ describe('BigQuery', () => { it('should pass the options object', () => { const options = {a: 'b'}; + const expectedOptions = extend({projectId: PROJECT_ID}, options); const job = bq.job(JOB_ID, options); - assert.strictEqual(job.calledWith_[2], options); + assert.deepStrictEqual(job.calledWith_[2], expectedOptions); }); - it('should pass in the user specified location', () => { + it('should pass in the user specified location and project', () => { const bq = new BigQuery({ projectId: PROJECT_ID, location: LOCATION, }); - const options = {a: 'b'}; - const expectedOptions = Object.assign({location: LOCATION}, options); + const options = { + a: 'b', + location: ANOTHER_LOCATION, + projectId: ANOTHER_PROJECT_ID, + }; + const expectedOptions = Object.assign( + {location: ANOTHER_LOCATION, projectId: ANOTHER_PROJECT_ID}, + options + ); const job = bq.job(JOB_ID, options); const args = job.calledWith_; diff --git a/test/dataset.ts b/test/dataset.ts index 5d77e33d..c7187596 100644 --- a/test/dataset.ts +++ b/test/dataset.ts @@ -909,6 +909,7 @@ describe('BigQuery/Dataset', () => { }; beforeEach(() => { + ds = new Dataset(BIGQUERY, DATASET_ID, {location: LOCATION}); ds.request = (reqOpts: DecorateRequestOptions, callback: Function) => { callback(null, apiResponse); }; @@ -929,6 +930,7 @@ describe('BigQuery/Dataset', () => { assert(table instanceof Table); assert.strictEqual(table.id, tableId); assert.strictEqual(table.location, LOCATION); + assert.strictEqual(table.projectId, BIGQUERY.location); assert.strictEqual(apiResponse_, apiResponse); done(); }