18
18
import { CredentialsProvider } from '../api/credentials' ;
19
19
import { User } from '../auth/user' ;
20
20
import { Aggregate } from '../core/aggregate' ;
21
+ import { DatabaseId } from '../core/database_info' ;
21
22
import { queryToAggregateTarget , Query , queryToTarget } from '../core/query' ;
22
23
import { Document } from '../model/document' ;
23
24
import { DocumentKey } from '../model/document_key' ;
24
25
import { Mutation } from '../model/mutation' ;
26
+ import { ResourcePath } from '../model/path' ;
25
27
import {
26
28
ApiClientObjectMap ,
27
29
BatchGetDocumentsRequest as ProtoBatchGetDocumentsRequest ,
@@ -47,11 +49,11 @@ import {
47
49
import {
48
50
fromDocument ,
49
51
fromBatchGetDocumentsResponse ,
50
- getEncodedDatabaseId ,
51
52
JsonProtoSerializer ,
52
53
toMutation ,
53
54
toName ,
54
55
toQueryTarget ,
56
+ toResourcePath ,
55
57
toRunAggregationQueryRequest
56
58
} from './serializer' ;
57
59
@@ -94,7 +96,8 @@ class DatastoreImpl extends Datastore {
94
96
/** Invokes the provided RPC with auth and AppCheck tokens. */
95
97
invokeRPC < Req , Resp > (
96
98
rpcName : string ,
97
- path : string ,
99
+ databaseId : DatabaseId ,
100
+ resourcePath : ResourcePath ,
98
101
request : Req
99
102
) : Promise < Resp > {
100
103
this . verifyInitialized ( ) ;
@@ -105,7 +108,7 @@ class DatastoreImpl extends Datastore {
105
108
. then ( ( [ authToken , appCheckToken ] ) => {
106
109
return this . connection . invokeRPC < Req , Resp > (
107
110
rpcName ,
108
- path ,
111
+ toResourcePath ( databaseId , resourcePath ) ,
109
112
request ,
110
113
authToken ,
111
114
appCheckToken
@@ -127,7 +130,8 @@ class DatastoreImpl extends Datastore {
127
130
/** Invokes the provided RPC with streamed results with auth and AppCheck tokens. */
128
131
invokeStreamingRPC < Req , Resp > (
129
132
rpcName : string ,
130
- path : string ,
133
+ databaseId : DatabaseId ,
134
+ resourcePath : ResourcePath ,
131
135
request : Req ,
132
136
expectedResponseCount ?: number
133
137
) : Promise < Resp [ ] > {
@@ -139,7 +143,7 @@ class DatastoreImpl extends Datastore {
139
143
. then ( ( [ authToken , appCheckToken ] ) => {
140
144
return this . connection . invokeStreamingRPC < Req , Resp > (
141
145
rpcName ,
142
- path ,
146
+ toResourcePath ( databaseId , resourcePath ) ,
143
147
request ,
144
148
authToken ,
145
149
appCheckToken ,
@@ -186,26 +190,35 @@ export async function invokeCommitRpc(
186
190
mutations : Mutation [ ]
187
191
) : Promise < void > {
188
192
const datastoreImpl = debugCast ( datastore , DatastoreImpl ) ;
189
- const path = getEncodedDatabaseId ( datastoreImpl . serializer ) + '/documents' ;
190
193
const request = {
191
194
writes : mutations . map ( m => toMutation ( datastoreImpl . serializer , m ) )
192
195
} ;
193
- await datastoreImpl . invokeRPC ( 'Commit' , path , request ) ;
196
+ await datastoreImpl . invokeRPC (
197
+ 'Commit' ,
198
+ datastoreImpl . serializer . databaseId ,
199
+ ResourcePath . emptyPath ( ) ,
200
+ request
201
+ ) ;
194
202
}
195
203
196
204
export async function invokeBatchGetDocumentsRpc (
197
205
datastore : Datastore ,
198
206
keys : DocumentKey [ ]
199
207
) : Promise < Document [ ] > {
200
208
const datastoreImpl = debugCast ( datastore , DatastoreImpl ) ;
201
- const path = getEncodedDatabaseId ( datastoreImpl . serializer ) + '/documents' ;
202
209
const request = {
203
210
documents : keys . map ( k => toName ( datastoreImpl . serializer , k ) )
204
211
} ;
205
212
const response = await datastoreImpl . invokeStreamingRPC <
206
213
ProtoBatchGetDocumentsRequest ,
207
214
ProtoBatchGetDocumentsResponse
208
- > ( 'BatchGetDocuments' , path , request , keys . length ) ;
215
+ > (
216
+ 'BatchGetDocuments' ,
217
+ datastoreImpl . serializer . databaseId ,
218
+ ResourcePath . emptyPath ( ) ,
219
+ request ,
220
+ keys . length
221
+ ) ;
209
222
210
223
const docs = new Map < string , Document > ( ) ;
211
224
response . forEach ( proto => {
@@ -226,11 +239,16 @@ export async function invokeRunQueryRpc(
226
239
query : Query
227
240
) : Promise < Document [ ] > {
228
241
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
+ ) ;
230
246
const response = await datastoreImpl . invokeStreamingRPC <
231
247
ProtoRunQueryRequest ,
232
248
ProtoRunQueryResponse
233
- > ( 'RunQuery' , request . parent ! , { structuredQuery : request . structuredQuery } ) ;
249
+ > ( 'RunQuery' , datastoreImpl . serializer . databaseId , parent , {
250
+ structuredQuery : queryTarget . structuredQuery
251
+ } ) ;
234
252
return (
235
253
response
236
254
// Omit RunQueryResponses that only contain readTimes.
@@ -247,20 +265,25 @@ export async function invokeRunAggregationQueryRpc(
247
265
aggregates : Aggregate [ ]
248
266
) : Promise < ApiClientObjectMap < Value > > {
249
267
const datastoreImpl = debugCast ( datastore , DatastoreImpl ) ;
250
- const { request, aliasMap } = toRunAggregationQueryRequest (
268
+ const { request, aliasMap, parent } = toRunAggregationQueryRequest (
251
269
datastoreImpl . serializer ,
252
270
queryToAggregateTarget ( query ) ,
253
271
aggregates
254
272
) ;
255
273
256
- const parent = request . parent ;
257
274
if ( ! datastoreImpl . connection . shouldResourcePathBeIncludedInRequest ) {
258
275
delete request . parent ;
259
276
}
260
277
const response = await datastoreImpl . invokeStreamingRPC <
261
278
ProtoRunAggregationQueryRequest ,
262
279
ProtoRunAggregationQueryResponse
263
- > ( 'RunAggregationQuery' , parent ! , request , /*expectedResponseCount=*/ 1 ) ;
280
+ > (
281
+ 'RunAggregationQuery' ,
282
+ datastoreImpl . serializer . databaseId ,
283
+ parent ,
284
+ request ,
285
+ /*expectedResponseCount=*/ 1
286
+ ) ;
264
287
265
288
// Omit RunAggregationQueryResponse that only contain readTimes.
266
289
const filteredResult = response . filter ( proto => ! ! proto . result ) ;
0 commit comments