Skip to content

Commit 1af49c1

Browse files
fix: Resolving issues with multi-DB support (#1864)
* Make named DB work. * override of gax.routingHeader.fromParams
1 parent 1e913db commit 1af49c1

File tree

4 files changed

+44
-16
lines changed

4 files changed

+44
-16
lines changed

dev/src/index.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ import {
8484
RECURSIVE_DELETE_MIN_PENDING_OPS,
8585
RecursiveDelete,
8686
} from './recursive-delete';
87+
import {stringify} from 'querystring';
8788

8889
export {
8990
CollectionReference,
@@ -590,6 +591,21 @@ export class Firestore implements firestore.Firestore {
590591
}
591592
}
592593

594+
// TODO (multi-db) Revert this override of gax.routingHeader.fromParams
595+
// after a permanent fix is applied. See b/292075646
596+
// This override of the routingHeader.fromParams does not
597+
// encode forward slash characters. This is a temporary fix for b/291780066
598+
gax.routingHeader.fromParams = params => {
599+
return stringify(params, undefined, undefined, {
600+
encodeURIComponent: (val: string) => {
601+
return val
602+
.split('/')
603+
.map(component => encodeURIComponent(component))
604+
.join('/');
605+
},
606+
});
607+
};
608+
593609
if (this._settings.ssl === false) {
594610
const grpcModule = this._settings.grpc ?? require('google-gax').grpc;
595611
const sslCreds = grpcModule.credentials.createInsecure();

dev/src/path.ts

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -308,17 +308,14 @@ export class ResourcePath extends Path<ResourcePath> {
308308
*
309309
* @private
310310
* @internal
311-
* @param projectIdIfMissing The project ID of the current Firestore project.
312-
* The project ID is only used if it's not provided as part of this
313-
* ResourcePath.
311+
* @param projectId The project ID of the current Firestore project.
314312
* @return A fully-qualified resource path pointing to the same element.
315313
*/
316-
toQualifiedResourcePath(projectIdIfMissing: string): QualifiedResourcePath {
317-
return new QualifiedResourcePath(
318-
projectIdIfMissing,
319-
DEFAULT_DATABASE_ID,
320-
...this.segments
321-
);
314+
toQualifiedResourcePath(
315+
projectId: string,
316+
databaseId: string
317+
): QualifiedResourcePath {
318+
return new QualifiedResourcePath(projectId, databaseId, ...this.segments);
322319
}
323320
}
324321

dev/src/reference.ts

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,9 @@ export class DocumentReference<T = firestore.DocumentData>
155155
*/
156156
get formattedName(): string {
157157
const projectId = this.firestore.projectId;
158-
return this._path.toQualifiedResourcePath(projectId).formattedName;
158+
const databaseId = this.firestore.databaseId;
159+
return this._path.toQualifiedResourcePath(projectId, databaseId)
160+
.formattedName;
159161
}
160162

161163
/**
@@ -2328,8 +2330,11 @@ export class Query<T = firestore.DocumentData> implements firestore.Query<T> {
23282330
transactionIdOrReadTime?: Uint8Array | Timestamp
23292331
): api.IRunQueryRequest {
23302332
const projectId = this.firestore.projectId;
2331-
const parentPath =
2332-
this._queryOptions.parentPath.toQualifiedResourcePath(projectId);
2333+
const databaseId = this.firestore.databaseId;
2334+
const parentPath = this._queryOptions.parentPath.toQualifiedResourcePath(
2335+
projectId,
2336+
databaseId
2337+
);
23332338

23342339
const structuredQuery = this.toStructuredQuery();
23352340

@@ -2387,8 +2392,11 @@ export class Query<T = firestore.DocumentData> implements firestore.Query<T> {
23872392
*/
23882393
_toBundledQuery(): protos.firestore.IBundledQuery {
23892394
const projectId = this.firestore.projectId;
2390-
const parentPath =
2391-
this._queryOptions.parentPath.toQualifiedResourcePath(projectId);
2395+
const databaseId = this.firestore.databaseId;
2396+
const parentPath = this._queryOptions.parentPath.toQualifiedResourcePath(
2397+
projectId,
2398+
databaseId
2399+
);
23922400
const structuredQuery = this.toStructuredQuery();
23932401

23942402
const bundledQuery: protos.firestore.IBundledQuery = {
@@ -2858,7 +2866,8 @@ export class CollectionReference<T = firestore.DocumentData>
28582866
const tag = requestTag();
28592867
return this.firestore.initializeIfNeeded(tag).then(() => {
28602868
const parentPath = this._queryOptions.parentPath.toQualifiedResourcePath(
2861-
this.firestore.projectId
2869+
this.firestore.projectId,
2870+
this.firestore.databaseId
28622871
);
28632872

28642873
const request: api.IListDocumentsRequest = {

dev/system-test/firestore.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,14 @@ if (process.env.NODE_ENV === 'DEBUG') {
8282
}
8383

8484
function getTestRoot(settings: Settings = {}) {
85+
const internalSettings: Settings = {};
86+
if (process.env.FIRESTORE_NAMED_DATABASE) {
87+
internalSettings.databaseId = process.env.FIRESTORE_NAMED_DATABASE;
88+
}
89+
8590
const firestore = new Firestore({
86-
...settings,
91+
...internalSettings,
92+
...settings, // caller settings take precedent over internal settings
8793
});
8894
return firestore.collection(`node_${version}_${autoId()}`);
8995
}

0 commit comments

Comments
 (0)