@@ -20,7 +20,7 @@ import { MaybeDocument } from '../model/document';
20
20
import { DocumentKey } from '../model/document_key' ;
21
21
import { Mutation , MutationResult } from '../model/mutation' ;
22
22
import * as api from '../protos/firestore_proto_api' ;
23
- import { hardAssert } from '../util/assert' ;
23
+ import { debugAssert , hardAssert } from '../util/assert' ;
24
24
import { Code , FirestoreError } from '../util/error' ;
25
25
import { Connection } from './connection' ;
26
26
import { JsonProtoSerializer } from './serializer' ;
@@ -43,16 +43,24 @@ interface CommitRequest extends api.CommitRequest {
43
43
}
44
44
45
45
/**
46
- * Datastore is a wrapper around the external Google Cloud Datastore grpc API,
47
- * which provides an interface that is more convenient for the rest of the
48
- * client SDK architecture to consume.
46
+ * Datastore and its related methods are a wrapper around the external Google
47
+ * Cloud Datastore grpc API, which provides an interface that is more convenient
48
+ * for the rest of the client SDK architecture to consume.
49
49
*/
50
- export class Datastore {
50
+ export class Datastore { }
51
+
52
+ /**
53
+ * An implementation of Datastore that exposes additional state for internal
54
+ * consumption.
55
+ */
56
+ class DatastoreImpl extends Datastore {
51
57
constructor (
52
58
public readonly connection : Connection ,
53
59
public readonly credentials : CredentialsProvider ,
54
60
public readonly serializer : JsonProtoSerializer
55
- ) { }
61
+ ) {
62
+ super ( ) ;
63
+ }
56
64
57
65
/** Gets an auth token and invokes the provided RPC. */
58
66
invokeRPC < Req , Resp > ( rpcName : string , request : Req ) : Promise < Resp > {
@@ -92,10 +100,22 @@ export class Datastore {
92
100
}
93
101
}
94
102
103
+ export function createDatastore (
104
+ connection : Connection ,
105
+ credentials : CredentialsProvider ,
106
+ serializer : JsonProtoSerializer
107
+ ) : Datastore {
108
+ return new DatastoreImpl ( connection , credentials , serializer ) ;
109
+ }
110
+
95
111
export function invokeCommitRpc (
96
112
datastore : Datastore ,
97
113
mutations : Mutation [ ]
98
114
) : Promise < MutationResult [ ] > {
115
+ debugAssert (
116
+ datastore instanceof DatastoreImpl ,
117
+ 'invokeCommitRpc() requires DatastoreImpl'
118
+ ) ;
99
119
const params : CommitRequest = {
100
120
database : datastore . serializer . encodedDatabaseId ,
101
121
writes : mutations . map ( m => datastore . serializer . toMutation ( m ) )
@@ -114,6 +134,10 @@ export function invokeBatchGetDocumentsRpc(
114
134
datastore : Datastore ,
115
135
keys : DocumentKey [ ]
116
136
) : Promise < MaybeDocument [ ] > {
137
+ debugAssert (
138
+ datastore instanceof DatastoreImpl ,
139
+ 'invokeBatchGetDocumentsRpc() requires DatastoreImpl'
140
+ ) ;
117
141
const params : BatchGetDocumentsRequest = {
118
142
database : datastore . serializer . encodedDatabaseId ,
119
143
documents : keys . map ( k => datastore . serializer . toName ( k ) )
@@ -145,6 +169,10 @@ export function newPersistentWriteStream(
145
169
queue : AsyncQueue ,
146
170
listener : WriteStreamListener
147
171
) : PersistentWriteStream {
172
+ debugAssert (
173
+ datastore instanceof DatastoreImpl ,
174
+ 'newPersistentWriteStream() requires DatastoreImpl'
175
+ ) ;
148
176
return new PersistentWriteStream (
149
177
queue ,
150
178
datastore . connection ,
@@ -159,6 +187,10 @@ export function newPersistentWatchStream(
159
187
queue : AsyncQueue ,
160
188
listener : WatchStreamListener
161
189
) : PersistentListenStream {
190
+ debugAssert (
191
+ datastore instanceof DatastoreImpl ,
192
+ 'newPersistentWatchStream() requires DatastoreImpl'
193
+ ) ;
162
194
return new PersistentListenStream (
163
195
queue ,
164
196
datastore . connection ,
0 commit comments