1818import { CredentialsProvider } from '../api/credentials' ;
1919import { User } from '../auth/user' ;
2020import { Aggregate } from '../core/aggregate' ;
21+ import { DatabaseId } from '../core/database_info' ;
2122import { queryToAggregateTarget , Query , queryToTarget } from '../core/query' ;
2223import { Document } from '../model/document' ;
2324import { DocumentKey } from '../model/document_key' ;
2425import { Mutation } from '../model/mutation' ;
26+ import { ResourcePath } from '../model/path' ;
2527import {
2628 ApiClientObjectMap ,
2729 BatchGetDocumentsRequest as ProtoBatchGetDocumentsRequest ,
@@ -47,11 +49,11 @@ import {
4749import {
4850 fromDocument ,
4951 fromBatchGetDocumentsResponse ,
50- getEncodedDatabaseId ,
5152 JsonProtoSerializer ,
5253 toMutation ,
5354 toName ,
5455 toQueryTarget ,
56+ toResourcePath ,
5557 toRunAggregationQueryRequest
5658} from './serializer' ;
5759
@@ -94,7 +96,8 @@ class DatastoreImpl extends Datastore {
9496 /** Invokes the provided RPC with auth and AppCheck tokens. */
9597 invokeRPC < Req , Resp > (
9698 rpcName : string ,
97- path : string ,
99+ databaseId : DatabaseId ,
100+ resourcePath : ResourcePath ,
98101 request : Req
99102 ) : Promise < Resp > {
100103 this . verifyInitialized ( ) ;
@@ -105,7 +108,7 @@ class DatastoreImpl extends Datastore {
105108 . then ( ( [ authToken , appCheckToken ] ) => {
106109 return this . connection . invokeRPC < Req , Resp > (
107110 rpcName ,
108- path ,
111+ toResourcePath ( databaseId , resourcePath ) ,
109112 request ,
110113 authToken ,
111114 appCheckToken
@@ -127,7 +130,8 @@ class DatastoreImpl extends Datastore {
127130 /** Invokes the provided RPC with streamed results with auth and AppCheck tokens. */
128131 invokeStreamingRPC < Req , Resp > (
129132 rpcName : string ,
130- path : string ,
133+ databaseId : DatabaseId ,
134+ resourcePath : ResourcePath ,
131135 request : Req ,
132136 expectedResponseCount ?: number
133137 ) : Promise < Resp [ ] > {
@@ -139,7 +143,7 @@ class DatastoreImpl extends Datastore {
139143 . then ( ( [ authToken , appCheckToken ] ) => {
140144 return this . connection . invokeStreamingRPC < Req , Resp > (
141145 rpcName ,
142- path ,
146+ toResourcePath ( databaseId , resourcePath ) ,
143147 request ,
144148 authToken ,
145149 appCheckToken ,
@@ -186,26 +190,35 @@ export async function invokeCommitRpc(
186190 mutations : Mutation [ ]
187191) : Promise < void > {
188192 const datastoreImpl = debugCast ( datastore , DatastoreImpl ) ;
189- const path = getEncodedDatabaseId ( datastoreImpl . serializer ) + '/documents' ;
190193 const request = {
191194 writes : mutations . map ( m => toMutation ( datastoreImpl . serializer , m ) )
192195 } ;
193- await datastoreImpl . invokeRPC ( 'Commit' , path , request ) ;
196+ await datastoreImpl . invokeRPC (
197+ 'Commit' ,
198+ datastoreImpl . serializer . databaseId ,
199+ ResourcePath . emptyPath ( ) ,
200+ request
201+ ) ;
194202}
195203
196204export async function invokeBatchGetDocumentsRpc (
197205 datastore : Datastore ,
198206 keys : DocumentKey [ ]
199207) : Promise < Document [ ] > {
200208 const datastoreImpl = debugCast ( datastore , DatastoreImpl ) ;
201- const path = getEncodedDatabaseId ( datastoreImpl . serializer ) + '/documents' ;
202209 const request = {
203210 documents : keys . map ( k => toName ( datastoreImpl . serializer , k ) )
204211 } ;
205212 const response = await datastoreImpl . invokeStreamingRPC <
206213 ProtoBatchGetDocumentsRequest ,
207214 ProtoBatchGetDocumentsResponse
208- > ( 'BatchGetDocuments' , path , request , keys . length ) ;
215+ > (
216+ 'BatchGetDocuments' ,
217+ datastoreImpl . serializer . databaseId ,
218+ ResourcePath . emptyPath ( ) ,
219+ request ,
220+ keys . length
221+ ) ;
209222
210223 const docs = new Map < string , Document > ( ) ;
211224 response . forEach ( proto => {
@@ -226,11 +239,16 @@ export async function invokeRunQueryRpc(
226239 query : Query
227240) : Promise < Document [ ] > {
228241 const datastoreImpl = debugCast ( datastore , DatastoreImpl ) ;
229- const request = toQueryTarget ( datastoreImpl . serializer , queryToTarget ( query ) ) ;
242+ const { queryTarget, parent } = toQueryTarget (
243+ datastoreImpl . serializer ,
244+ queryToTarget ( query )
245+ ) ;
230246 const response = await datastoreImpl . invokeStreamingRPC <
231247 ProtoRunQueryRequest ,
232248 ProtoRunQueryResponse
233- > ( 'RunQuery' , request . parent ! , { structuredQuery : request . structuredQuery } ) ;
249+ > ( 'RunQuery' , datastoreImpl . serializer . databaseId , parent , {
250+ structuredQuery : queryTarget . structuredQuery
251+ } ) ;
234252 return (
235253 response
236254 // Omit RunQueryResponses that only contain readTimes.
@@ -247,20 +265,25 @@ export async function invokeRunAggregationQueryRpc(
247265 aggregates : Aggregate [ ]
248266) : Promise < ApiClientObjectMap < Value > > {
249267 const datastoreImpl = debugCast ( datastore , DatastoreImpl ) ;
250- const { request, aliasMap } = toRunAggregationQueryRequest (
268+ const { request, aliasMap, parent } = toRunAggregationQueryRequest (
251269 datastoreImpl . serializer ,
252270 queryToAggregateTarget ( query ) ,
253271 aggregates
254272 ) ;
255273
256- const parent = request . parent ;
257274 if ( ! datastoreImpl . connection . shouldResourcePathBeIncludedInRequest ) {
258275 delete request . parent ;
259276 }
260277 const response = await datastoreImpl . invokeStreamingRPC <
261278 ProtoRunAggregationQueryRequest ,
262279 ProtoRunAggregationQueryResponse
263- > ( 'RunAggregationQuery' , parent ! , request , /*expectedResponseCount=*/ 1 ) ;
280+ > (
281+ 'RunAggregationQuery' ,
282+ datastoreImpl . serializer . databaseId ,
283+ parent ,
284+ request ,
285+ /*expectedResponseCount=*/ 1
286+ ) ;
264287
265288 // Omit RunAggregationQueryResponse that only contain readTimes.
266289 const filteredResult = response . filter ( proto => ! ! proto . result ) ;
0 commit comments