16
16
*/
17
17
18
18
import { CredentialsProvider } from '../api/credentials' ;
19
- import { maybeDocumentMap } from '../model/collections' ;
20
19
import { MaybeDocument } from '../model/document' ;
21
20
import { DocumentKey } from '../model/document_key' ;
22
21
import { Mutation , MutationResult } from '../model/mutation' ;
23
22
import * as api from '../protos/firestore_proto_api' ;
24
- import { hardAssert } from '../util/assert' ;
25
- import { AsyncQueue } from '../util/async_queue' ;
23
+ import { debugCast , hardAssert } from '../util/assert' ;
26
24
import { Code , FirestoreError } from '../util/error' ;
27
25
import { Connection } from './connection' ;
26
+ import { JsonProtoSerializer } from './serializer' ;
28
27
import {
29
- WatchStreamListener ,
30
- WriteStreamListener ,
31
28
PersistentListenStream ,
32
- PersistentWriteStream
29
+ PersistentWriteStream ,
30
+ WatchStreamListener ,
31
+ WriteStreamListener
33
32
} from './persistent_stream' ;
33
+ import { AsyncQueue } from '../util/async_queue' ;
34
34
35
- import { JsonProtoSerializer } from './serializer' ;
36
-
37
- // The generated proto interfaces for these class are missing the database
38
- // field. So we add it here.
39
- // TODO(b/36015800): Remove this once the api generator is fixed.
40
- interface BatchGetDocumentsRequest extends api . BatchGetDocumentsRequest {
41
- database ?: string ;
42
- }
43
- interface CommitRequest extends api . CommitRequest {
44
- database ?: string ;
45
- }
35
+ /**
36
+ * Datastore and its related methods are a wrapper around the external Google
37
+ * Cloud Datastore grpc API, which provides an interface that is more convenient
38
+ * for the rest of the client SDK architecture to consume.
39
+ */
40
+ export class Datastore { }
46
41
47
42
/**
48
- * Datastore is a wrapper around the external Google Cloud Datastore grpc API,
49
- * which provides an interface that is more convenient for the rest of the
50
- * client SDK architecture to consume.
43
+ * An implementation of Datastore that exposes additional state for internal
44
+ * consumption.
51
45
*/
52
- export class Datastore {
46
+ class DatastoreImpl extends Datastore {
53
47
constructor (
54
- private queue : AsyncQueue ,
55
- private connection : Connection ,
56
- private credentials : CredentialsProvider ,
57
- private serializer : JsonProtoSerializer
58
- ) { }
59
-
60
- newPersistentWriteStream (
61
- listener : WriteStreamListener
62
- ) : PersistentWriteStream {
63
- return new PersistentWriteStream (
64
- this . queue ,
65
- this . connection ,
66
- this . credentials ,
67
- this . serializer ,
68
- listener
69
- ) ;
70
- }
71
-
72
- newPersistentWatchStream (
73
- listener : WatchStreamListener
74
- ) : PersistentListenStream {
75
- return new PersistentListenStream (
76
- this . queue ,
77
- this . connection ,
78
- this . credentials ,
79
- this . serializer ,
80
- listener
81
- ) ;
82
- }
83
-
84
- commit ( mutations : Mutation [ ] ) : Promise < MutationResult [ ] > {
85
- const params : CommitRequest = {
86
- database : this . serializer . encodedDatabaseId ,
87
- writes : mutations . map ( m => this . serializer . toMutation ( m ) )
88
- } ;
89
- return this . invokeRPC < CommitRequest , api . CommitResponse > (
90
- 'Commit' ,
91
- params
92
- ) . then ( response => {
93
- return this . serializer . fromWriteResults (
94
- response . writeResults ,
95
- response . commitTime
96
- ) ;
97
- } ) ;
98
- }
99
-
100
- lookup ( keys : DocumentKey [ ] ) : Promise < MaybeDocument [ ] > {
101
- const params : BatchGetDocumentsRequest = {
102
- database : this . serializer . encodedDatabaseId ,
103
- documents : keys . map ( k => this . serializer . toName ( k ) )
104
- } ;
105
- return this . invokeStreamingRPC <
106
- BatchGetDocumentsRequest ,
107
- api . BatchGetDocumentsResponse
108
- > ( 'BatchGetDocuments' , params ) . then ( response => {
109
- let docs = maybeDocumentMap ( ) ;
110
- response . forEach ( proto => {
111
- const doc = this . serializer . fromMaybeDocument ( proto ) ;
112
- docs = docs . insert ( doc . key , doc ) ;
113
- } ) ;
114
- const result : MaybeDocument [ ] = [ ] ;
115
- keys . forEach ( key => {
116
- const doc = docs . get ( key ) ;
117
- hardAssert ( ! ! doc , 'Missing entity in write response for ' + key ) ;
118
- result . push ( doc ) ;
119
- } ) ;
120
- return result ;
121
- } ) ;
48
+ public readonly connection : Connection ,
49
+ public readonly credentials : CredentialsProvider ,
50
+ public readonly serializer : JsonProtoSerializer
51
+ ) {
52
+ super ( ) ;
122
53
}
123
54
124
55
/** Gets an auth token and invokes the provided RPC. */
125
- private invokeRPC < Req , Resp > ( rpcName : string , request : Req ) : Promise < Resp > {
56
+ invokeRPC < Req , Resp > ( rpcName : string , request : Req ) : Promise < Resp > {
126
57
return this . credentials
127
58
. getToken ( )
128
59
. then ( token => {
@@ -137,7 +68,7 @@ export class Datastore {
137
68
}
138
69
139
70
/** Gets an auth token and invokes the provided RPC with streamed results. */
140
- private invokeStreamingRPC < Req , Resp > (
71
+ invokeStreamingRPC < Req , Resp > (
141
72
rpcName : string ,
142
73
request : Req
143
74
) : Promise < Resp [ ] > {
@@ -158,3 +89,88 @@ export class Datastore {
158
89
} ) ;
159
90
}
160
91
}
92
+
93
+ export function newDatastore (
94
+ connection : Connection ,
95
+ credentials : CredentialsProvider ,
96
+ serializer : JsonProtoSerializer
97
+ ) : Datastore {
98
+ return new DatastoreImpl ( connection , credentials , serializer ) ;
99
+ }
100
+
101
+ export async function invokeCommitRpc (
102
+ datastore : Datastore ,
103
+ mutations : Mutation [ ]
104
+ ) : Promise < MutationResult [ ] > {
105
+ const datastoreImpl = debugCast ( datastore , DatastoreImpl ) ;
106
+ const params = {
107
+ database : datastoreImpl . serializer . encodedDatabaseId ,
108
+ writes : mutations . map ( m => datastoreImpl . serializer . toMutation ( m ) )
109
+ } ;
110
+ const response = await datastoreImpl . invokeRPC <
111
+ api . CommitRequest ,
112
+ api . CommitResponse
113
+ > ( 'Commit' , params ) ;
114
+ return datastoreImpl . serializer . fromWriteResults (
115
+ response . writeResults ,
116
+ response . commitTime
117
+ ) ;
118
+ }
119
+
120
+ export async function invokeBatchGetDocumentsRpc (
121
+ datastore : Datastore ,
122
+ keys : DocumentKey [ ]
123
+ ) : Promise < MaybeDocument [ ] > {
124
+ const datastoreImpl = debugCast ( datastore , DatastoreImpl ) ;
125
+ const params = {
126
+ database : datastoreImpl . serializer . encodedDatabaseId ,
127
+ documents : keys . map ( k => datastoreImpl . serializer . toName ( k ) )
128
+ } ;
129
+ const response = await datastoreImpl . invokeStreamingRPC <
130
+ api . BatchGetDocumentsRequest ,
131
+ api . BatchGetDocumentsResponse
132
+ > ( 'BatchGetDocuments' , params ) ;
133
+
134
+ const docs = new Map < string , MaybeDocument > ( ) ;
135
+ response . forEach ( proto => {
136
+ const doc = datastoreImpl . serializer . fromMaybeDocument ( proto ) ;
137
+ docs . set ( doc . key . toString ( ) , doc ) ;
138
+ } ) ;
139
+ const result : MaybeDocument [ ] = [ ] ;
140
+ keys . forEach ( key => {
141
+ const doc = docs . get ( key . toString ( ) ) ;
142
+ hardAssert ( ! ! doc , 'Missing entity in write response for ' + key ) ;
143
+ result . push ( doc ) ;
144
+ } ) ;
145
+ return result ;
146
+ }
147
+
148
+ export function newPersistentWriteStream (
149
+ datastore : Datastore ,
150
+ queue : AsyncQueue ,
151
+ listener : WriteStreamListener
152
+ ) : PersistentWriteStream {
153
+ const datastoreImpl = debugCast ( datastore , DatastoreImpl ) ;
154
+ return new PersistentWriteStream (
155
+ queue ,
156
+ datastoreImpl . connection ,
157
+ datastoreImpl . credentials ,
158
+ datastoreImpl . serializer ,
159
+ listener
160
+ ) ;
161
+ }
162
+
163
+ export function newPersistentWatchStream (
164
+ datastore : Datastore ,
165
+ queue : AsyncQueue ,
166
+ listener : WatchStreamListener
167
+ ) : PersistentListenStream {
168
+ const datastoreImpl = debugCast ( datastore , DatastoreImpl ) ;
169
+ return new PersistentListenStream (
170
+ queue ,
171
+ datastoreImpl . connection ,
172
+ datastoreImpl . credentials ,
173
+ datastoreImpl . serializer ,
174
+ listener
175
+ ) ;
176
+ }
0 commit comments