Skip to content

Commit f6a7456

Browse files
authored
feat: add cross-project support (#1079)
* add projectId override * update nodejs-common version * add tests * update test
1 parent ed13f5e commit f6a7456

File tree

3 files changed

+42
-1
lines changed

3 files changed

+42
-1
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
"precompile": "gts clean"
4949
},
5050
"dependencies": {
51-
"@google-cloud/common": "^3.1.0",
51+
"@google-cloud/common": "^3.9.0",
5252
"@google-cloud/paginator": "^3.0.0",
5353
"@google-cloud/promisify": "^2.0.0",
5454
"arrify": "^2.0.1",

src/dataset.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ export interface DatasetDeleteOptions {
5353

5454
export interface DatasetOptions {
5555
location?: string;
56+
projectId?: string;
5657
}
5758

5859
export type CreateDatasetOptions = bigquery.IDataset;
@@ -121,6 +122,7 @@ export type TableCallback = ResourceCallback<Table, bigquery.ITable>;
121122
class Dataset extends ServiceObject {
122123
bigQuery: BigQuery;
123124
location?: string;
125+
projectId?: string;
124126
getModelsStream(options?: GetModelsOptions): ResourceStream<Model> {
125127
// placeholder body, overwritten in constructor
126128
return new ResourceStream<Model>({}, () => {});
@@ -379,6 +381,10 @@ class Dataset extends ServiceObject {
379381
this.location = options.location;
380382
}
381383

384+
if (options?.projectId) {
385+
this.projectId = options.projectId;
386+
}
387+
382388
this.bigQuery = bigQuery;
383389

384390
// Catch all for read-modify-write cycle
@@ -389,6 +395,14 @@ class Dataset extends ServiceObject {
389395
reqOpts.headers = reqOpts.headers || {};
390396
reqOpts.headers['If-Match'] = reqOpts.json.etag;
391397
}
398+
399+
if (this.projectId) {
400+
// Override projectId if provided
401+
reqOpts.uri = reqOpts.uri.replace(
402+
`/projects/${this.bigQuery.projectId}/`,
403+
`/projects/${this.projectId}/`
404+
);
405+
}
392406
return reqOpts;
393407
},
394408
});

test/dataset.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,14 @@ describe('BigQuery/Dataset', () => {
148148
assert.strictEqual(ds.location, LOCATION);
149149
});
150150

151+
it('should capture user provided projectId', () => {
152+
const projectIdOverride = 'octavia';
153+
const options = {projectId: projectIdOverride};
154+
const ds = new Dataset(BIGQUERY, DATASET_ID, options);
155+
156+
assert.strictEqual(ds.projectId, projectIdOverride);
157+
});
158+
151159
describe('createMethod', () => {
152160
// eslint-disable-next-line @typescript-eslint/no-explicit-any
153161
let bq: any;
@@ -197,6 +205,25 @@ describe('BigQuery/Dataset', () => {
197205
});
198206
});
199207

208+
describe('projectId override interceptor', () => {
209+
const projectIdOverride = 'DuBois';
210+
211+
it('should use projectId override uri', () => {
212+
ds = new Dataset(BIGQUERY, DATASET_ID, {projectId: projectIdOverride});
213+
const interceptor = ds.interceptors.pop();
214+
const fakeReqOpts = {
215+
method: 'PATCH',
216+
json: {
217+
etag: '',
218+
},
219+
uri: `/projects/${ds.bigQuery.projectId}/`,
220+
};
221+
222+
const reqOpts = interceptor.request(fakeReqOpts);
223+
assert.deepStrictEqual(reqOpts.uri, `/projects/${projectIdOverride}/`);
224+
});
225+
});
226+
200227
describe('etag interceptor', () => {
201228
const FAKE_ETAG = 'abc';
202229

0 commit comments

Comments
 (0)