Skip to content

Commit 9e85219

Browse files
authored
fix: prefer usage of projectId from the Dataset (#1326)
1 parent 5e3e540 commit 9e85219

File tree

8 files changed

+87
-33
lines changed

8 files changed

+87
-33
lines changed

src/bigquery.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1420,7 +1420,7 @@ export class BigQuery extends Service {
14201420

14211421
query.destinationTable = {
14221422
datasetId: options.destination.dataset.id,
1423-
projectId: options.destination.dataset.bigQuery.projectId,
1423+
projectId: options.destination.dataset.projectId,
14241424
tableId: options.destination.id,
14251425
};
14261426

src/dataset.ts

+6-3
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ export type TableCallback = ResourceCallback<Table, bigquery.ITable>;
123123
class Dataset extends ServiceObject {
124124
bigQuery: BigQuery;
125125
location?: string;
126-
projectId?: string;
126+
projectId: string;
127127
getModelsStream(options?: GetModelsOptions): ResourceStream<Model> {
128128
// placeholder body, overwritten in constructor
129129
return new ResourceStream<Model>({}, () => {});
@@ -389,6 +389,8 @@ class Dataset extends ServiceObject {
389389

390390
if (options?.projectId) {
391391
this.projectId = options.projectId;
392+
} else {
393+
this.projectId = bigQuery.projectId;
392394
}
393395

394396
this.bigQuery = bigQuery;
@@ -642,7 +644,7 @@ class Dataset extends ServiceObject {
642644
routineReference: {
643645
routineId: id,
644646
datasetId: this.id,
645-
projectId: this.bigQuery.projectId,
647+
projectId: this.projectId,
646648
},
647649
});
648650

@@ -740,7 +742,7 @@ class Dataset extends ServiceObject {
740742
// eslint-disable-next-line @typescript-eslint/no-explicit-any
741743
(body as any).tableReference = {
742744
datasetId: this.id,
743-
projectId: this.bigQuery.projectId,
745+
projectId: this.projectId,
744746
tableId: id,
745747
};
746748

@@ -1303,6 +1305,7 @@ class Dataset extends ServiceObject {
13031305
options = extend(
13041306
{
13051307
location: this.location,
1308+
projectId: this.projectId,
13061309
},
13071310
options
13081311
);

src/model.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -430,7 +430,7 @@ class Model extends ServiceObject {
430430
extract: extend(true, options, {
431431
sourceModel: {
432432
datasetId: this.dataset.id,
433-
projectId: this.bigQuery.projectId,
433+
projectId: this.dataset.projectId,
434434
modelId: this.id,
435435
},
436436
}),

src/table.ts

+9-9
Original file line numberDiff line numberDiff line change
@@ -929,12 +929,12 @@ class Table extends ServiceObject {
929929
copy: extend(true, metadata, {
930930
destinationTable: {
931931
datasetId: destination.dataset.id,
932-
projectId: destination.bigQuery.projectId,
932+
projectId: destination.dataset.projectId,
933933
tableId: destination.id,
934934
},
935935
sourceTable: {
936936
datasetId: this.dataset.id,
937-
projectId: this.bigQuery.projectId,
937+
projectId: this.dataset.projectId,
938938
tableId: this.id,
939939
},
940940
}),
@@ -1051,14 +1051,14 @@ class Table extends ServiceObject {
10511051
copy: extend(true, metadata, {
10521052
destinationTable: {
10531053
datasetId: this.dataset.id,
1054-
projectId: this.bigQuery.projectId,
1054+
projectId: this.dataset.projectId,
10551055
tableId: this.id,
10561056
},
10571057

10581058
sourceTables: sourceTables.map(sourceTable => {
10591059
return {
10601060
datasetId: sourceTable.dataset.id,
1061-
projectId: sourceTable.bigQuery.projectId,
1061+
projectId: sourceTable.dataset.projectId,
10621062
tableId: sourceTable.id,
10631063
};
10641064
}),
@@ -1224,7 +1224,7 @@ class Table extends ServiceObject {
12241224
extract: extend(true, options, {
12251225
sourceTable: {
12261226
datasetId: this.dataset.id,
1227-
projectId: this.bigQuery.projectId,
1227+
projectId: this.dataset.projectId,
12281228
tableId: this.id,
12291229
},
12301230
}),
@@ -1404,7 +1404,7 @@ class Table extends ServiceObject {
14041404
configuration: {
14051405
load: {
14061406
destinationTable: {
1407-
projectId: this.bigQuery.projectId,
1407+
projectId: this.dataset.projectId,
14081408
datasetId: this.dataset.id,
14091409
tableId: this.id,
14101410
},
@@ -1510,7 +1510,7 @@ class Table extends ServiceObject {
15101510
true,
15111511
{
15121512
destinationTable: {
1513-
projectId: this.bigQuery.projectId,
1513+
projectId: this.dataset.projectId,
15141514
datasetId: this.dataset.id,
15151515
tableId: this.id,
15161516
},
@@ -1542,12 +1542,12 @@ class Table extends ServiceObject {
15421542
},
15431543
jobReference: {
15441544
jobId,
1545-
projectId: this.bigQuery.projectId,
1545+
projectId: this.dataset.projectId,
15461546
location: this.location,
15471547
},
15481548
} as {},
15491549
request: {
1550-
uri: `${this.bigQuery.apiEndpoint}/upload/bigquery/v2/projects/${this.bigQuery.projectId}/jobs`,
1550+
uri: `${this.bigQuery.apiEndpoint}/upload/bigquery/v2/projects/${this.dataset.projectId}/jobs`,
15511551
},
15521552
},
15531553
// eslint-disable-next-line @typescript-eslint/no-explicit-any

test/bigquery.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1977,7 +1977,7 @@ describe('BigQuery', () => {
19771977
reqOpts.json.configuration.query.destinationTable,
19781978
{
19791979
datasetId: dataset.id,
1980-
projectId: dataset.bigQuery.projectId,
1980+
projectId: dataset.projectId,
19811981
tableId: TABLE_ID,
19821982
}
19831983
);

test/dataset.ts

+56-5
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ describe('BigQuery/Dataset', () => {
8787
} as {} as _root.BigQuery;
8888
const DATASET_ID = 'kittens';
8989
const LOCATION = 'asia-northeast1';
90+
const ANOTHER_PROJECT_ID = 'another-test-project';
9091

9192
// tslint:disable-next-line variable-name
9293
let Dataset: typeof _root.Dataset;
@@ -148,6 +149,12 @@ describe('BigQuery/Dataset', () => {
148149
assert.strictEqual(ds.location, LOCATION);
149150
});
150151

152+
it('should set the client projectId by default', () => {
153+
const ds = new Dataset(BIGQUERY, DATASET_ID);
154+
155+
assert.strictEqual(ds.projectId, BIGQUERY.projectId);
156+
});
157+
151158
it('should capture user provided projectId', () => {
152159
const projectIdOverride = 'octavia';
153160
const options = {projectId: projectIdOverride};
@@ -171,7 +178,9 @@ describe('BigQuery/Dataset', () => {
171178
});
172179

173180
it('should call through to BigQuery#createDataset', done => {
174-
const OPTIONS = {};
181+
const OPTIONS = {
182+
projectId: BIGQUERY.projectId,
183+
};
175184

176185
bq.createDataset = (id: string, options: {}, callback: Function) => {
177186
assert.strictEqual(id, DATASET_ID);
@@ -249,6 +258,7 @@ describe('BigQuery/Dataset', () => {
249258
json: {
250259
etag: FAKE_ETAG,
251260
},
261+
uri: `/projects/${BIGQUERY.projectId}/`,
252262
};
253263

254264
const reqOpts = interceptor.request(fakeReqOpts);
@@ -266,6 +276,7 @@ describe('BigQuery/Dataset', () => {
266276
json: {
267277
etag: FAKE_ETAG,
268278
},
279+
uri: `/projects/${BIGQUERY.projectId}/`,
269280
};
270281

271282
const expectedHeaders = Object.assign({}, fakeReqOpts.headers, {
@@ -284,6 +295,7 @@ describe('BigQuery/Dataset', () => {
284295
json: {
285296
etag: FAKE_ETAG,
286297
},
298+
uri: `/projects/${BIGQUERY.projectId}/`,
287299
};
288300

289301
const reqOpts = interceptor.request(fakeReqOpts);
@@ -428,6 +440,7 @@ describe('BigQuery/Dataset', () => {
428440
const API_RESPONSE = {
429441
tableReference: {
430442
tableId: TABLE_ID,
443+
projectId: BIGQUERY.projectId,
431444
},
432445
};
433446

@@ -443,10 +456,7 @@ describe('BigQuery/Dataset', () => {
443456
const body = reqOpts.json;
444457
assert.deepStrictEqual(body.schema, SCHEMA_OBJECT);
445458
assert.strictEqual(body.tableReference.datasetId, DATASET_ID);
446-
assert.strictEqual(
447-
body.tableReference.projectId,
448-
ds.bigQuery.projectId
449-
);
459+
assert.strictEqual(body.tableReference.projectId, ds.projectId);
450460
assert.strictEqual(body.tableReference.tableId, TABLE_ID);
451461

452462
done();
@@ -455,6 +465,31 @@ describe('BigQuery/Dataset', () => {
455465
ds.createTable(TABLE_ID, options, assert.ifError);
456466
});
457467

468+
it('should create a table on a different project', done => {
469+
const options = {
470+
schema: SCHEMA_OBJECT,
471+
};
472+
const anotherDs = new Dataset(BIGQUERY, DATASET_ID, {
473+
projectId: ANOTHER_PROJECT_ID,
474+
}) as any;
475+
anotherDs.request = (reqOpts: DecorateRequestOptions) => {
476+
assert.strictEqual(reqOpts.method, 'POST');
477+
assert.strictEqual(reqOpts.uri, '/tables');
478+
479+
const body = reqOpts.json;
480+
assert.deepStrictEqual(body.schema, SCHEMA_OBJECT);
481+
assert.strictEqual(body.tableReference.datasetId, DATASET_ID);
482+
assert.strictEqual(body.tableReference.projectId, ANOTHER_PROJECT_ID);
483+
assert.strictEqual(body.tableReference.tableId, TABLE_ID);
484+
485+
done();
486+
};
487+
488+
// Under the hood dataset.createTable is called
489+
const table = anotherDs.table(TABLE_ID);
490+
table.create(options, assert.ifError);
491+
});
492+
458493
it('should not require options', done => {
459494
ds.request = (reqOpts: DecorateRequestOptions, callback: Function) => {
460495
callback(null, API_RESPONSE);
@@ -577,6 +612,22 @@ describe('BigQuery/Dataset', () => {
577612
ds.createTable(TABLE_ID, {schema: SCHEMA_OBJECT}, assert.ifError);
578613
});
579614

615+
it('should pass the projectId to the Table', done => {
616+
const response = Object.assign({location: LOCATION}, API_RESPONSE);
617+
618+
ds.request = (reqOpts: DecorateRequestOptions, callback: Function) => {
619+
callback(null, response);
620+
};
621+
622+
ds.table = (id: string, options: TableOptions) => {
623+
assert.strictEqual(options.location, LOCATION);
624+
setImmediate(done);
625+
return {};
626+
};
627+
628+
ds.createTable(TABLE_ID, {schema: SCHEMA_OBJECT}, assert.ifError);
629+
});
630+
580631
it('should return an apiResponse', done => {
581632
const opts = {id: TABLE_ID, schema: SCHEMA_OBJECT};
582633

test/model.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,9 @@ describe('BigQuery/Model', () => {
5656

5757
const DATASET = {
5858
id: 'dataset-id',
59+
projectId: 'project-id',
5960
createTable: util.noop,
6061
bigQuery: {
61-
projectId: 'project-id',
6262
job: (id: string) => {
6363
return {id};
6464
},
@@ -137,7 +137,7 @@ describe('BigQuery/Model', () => {
137137
model.bigQuery.createJob = (reqOpts: JobOptions) => {
138138
assert.deepStrictEqual(reqOpts.configuration!.extract!.sourceModel, {
139139
datasetId: model.dataset.id,
140-
projectId: model.bigQuery.projectId,
140+
projectId: model.dataset.projectId,
141141
modelId: model.id,
142142
});
143143

test/table.ts

+11-11
Original file line numberDiff line numberDiff line change
@@ -173,9 +173,9 @@ describe('BigQuery/Table', () => {
173173

174174
const DATASET = {
175175
id: 'dataset-id',
176+
projectId: 'project-id',
176177
createTable: util.noop,
177178
bigQuery: {
178-
projectId: 'project-id',
179179
job: (id: string) => {
180180
return {id};
181181
},
@@ -721,12 +721,12 @@ describe('BigQuery/Table', () => {
721721
c: 'd',
722722
destinationTable: {
723723
datasetId: DEST_TABLE.dataset.id,
724-
projectId: DEST_TABLE.bigQuery.projectId,
724+
projectId: DEST_TABLE.dataset.projectId,
725725
tableId: DEST_TABLE.id,
726726
},
727727
sourceTable: {
728728
datasetId: table.dataset.id,
729-
projectId: table.bigQuery.projectId,
729+
projectId: table.dataset.projectId,
730730
tableId: table.id,
731731
},
732732
},
@@ -842,13 +842,13 @@ describe('BigQuery/Table', () => {
842842
c: 'd',
843843
destinationTable: {
844844
datasetId: table.dataset.id,
845-
projectId: table.bigQuery.projectId,
845+
projectId: table.dataset.projectId,
846846
tableId: table.id,
847847
},
848848
sourceTables: [
849849
{
850850
datasetId: SOURCE_TABLE.dataset.id,
851-
projectId: SOURCE_TABLE.bigQuery.projectId,
851+
projectId: SOURCE_TABLE.dataset.projectId,
852852
tableId: SOURCE_TABLE.id,
853853
},
854854
],
@@ -867,12 +867,12 @@ describe('BigQuery/Table', () => {
867867
assert.deepStrictEqual(reqOpts.configuration!.copy!.sourceTables, [
868868
{
869869
datasetId: SOURCE_TABLE.dataset.id,
870-
projectId: SOURCE_TABLE.bigQuery.projectId,
870+
projectId: SOURCE_TABLE.dataset.projectId,
871871
tableId: SOURCE_TABLE.id,
872872
},
873873
{
874874
datasetId: SOURCE_TABLE.dataset.id,
875-
projectId: SOURCE_TABLE.bigQuery.projectId,
875+
projectId: SOURCE_TABLE.dataset.projectId,
876876
tableId: SOURCE_TABLE.id,
877877
},
878878
]);
@@ -1002,7 +1002,7 @@ describe('BigQuery/Table', () => {
10021002
table.bigQuery.createJob = (reqOpts: JobOptions) => {
10031003
assert.deepStrictEqual(reqOpts.configuration!.extract!.sourceTable, {
10041004
datasetId: table.dataset.id,
1005-
projectId: table.bigQuery.projectId,
1005+
projectId: table.dataset.projectId,
10061006
tableId: table.id,
10071007
});
10081008

@@ -1685,14 +1685,14 @@ describe('BigQuery/Table', () => {
16851685
a: 'b',
16861686
c: 'd',
16871687
destinationTable: {
1688-
projectId: table.bigQuery.projectId,
1688+
projectId: table.dataset.projectId,
16891689
datasetId: table.dataset.id,
16901690
tableId: table.id,
16911691
},
16921692
},
16931693
},
16941694
jobReference: {
1695-
projectId: table.bigQuery.projectId,
1695+
projectId: table.dataset.projectId,
16961696
jobId: fakeJobId,
16971697
location: undefined,
16981698
},
@@ -1711,7 +1711,7 @@ describe('BigQuery/Table', () => {
17111711
const uri =
17121712
table.bigQuery.apiEndpoint +
17131713
'/upload/bigquery/v2/projects/' +
1714-
table.bigQuery.projectId +
1714+
table.dataset.projectId +
17151715
'/jobs';
17161716
assert.strictEqual(options.request.uri, uri);
17171717
done();

0 commit comments

Comments
 (0)