Skip to content

Commit 0295cd5

Browse files
Hide members
1 parent c7eef78 commit 0295cd5

File tree

4 files changed

+48
-12
lines changed

4 files changed

+48
-12
lines changed

packages/firestore/src/core/firestore_client.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import { Document, MaybeDocument, NoDocument } from '../model/document';
2323
import { DocumentKey } from '../model/document_key';
2424
import { Mutation } from '../model/mutation';
2525
import { Platform } from '../platform/platform';
26-
import { Datastore } from '../remote/datastore';
26+
import { createDatastore } from '../remote/datastore';
2727
import { RemoteStore } from '../remote/remote_store';
2828
import { AsyncQueue } from '../util/async_queue';
2929
import { Code, FirestoreError } from '../util/error';
@@ -238,7 +238,11 @@ export class FirestoreClient {
238238
const serializer = this.platform.newSerializer(
239239
this.databaseInfo.databaseId
240240
);
241-
const datastore = new Datastore(connection, this.credentials, serializer);
241+
const datastore = createDatastore(
242+
connection,
243+
this.credentials,
244+
serializer
245+
);
242246

243247
await componentProvider.initialize({
244248
asyncQueue: this.asyncQueue,

packages/firestore/src/remote/datastore.ts

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import { MaybeDocument } from '../model/document';
2020
import { DocumentKey } from '../model/document_key';
2121
import { Mutation, MutationResult } from '../model/mutation';
2222
import * as api from '../protos/firestore_proto_api';
23-
import { hardAssert } from '../util/assert';
23+
import { debugAssert, hardAssert } from '../util/assert';
2424
import { Code, FirestoreError } from '../util/error';
2525
import { Connection } from './connection';
2626
import { JsonProtoSerializer } from './serializer';
@@ -43,16 +43,24 @@ interface CommitRequest extends api.CommitRequest {
4343
}
4444

4545
/**
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.
4949
*/
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 {
5157
constructor(
5258
public readonly connection: Connection,
5359
public readonly credentials: CredentialsProvider,
5460
public readonly serializer: JsonProtoSerializer
55-
) {}
61+
) {
62+
super();
63+
}
5664

5765
/** Gets an auth token and invokes the provided RPC. */
5866
invokeRPC<Req, Resp>(rpcName: string, request: Req): Promise<Resp> {
@@ -92,10 +100,22 @@ export class Datastore {
92100
}
93101
}
94102

103+
export function createDatastore(
104+
connection: Connection,
105+
credentials: CredentialsProvider,
106+
serializer: JsonProtoSerializer
107+
): Datastore {
108+
return new DatastoreImpl(connection, credentials, serializer);
109+
}
110+
95111
export function invokeCommitRpc(
96112
datastore: Datastore,
97113
mutations: Mutation[]
98114
): Promise<MutationResult[]> {
115+
debugAssert(
116+
datastore instanceof DatastoreImpl,
117+
'invokeCommitRpc() requires DatastoreImpl'
118+
);
99119
const params: CommitRequest = {
100120
database: datastore.serializer.encodedDatabaseId,
101121
writes: mutations.map(m => datastore.serializer.toMutation(m))
@@ -114,6 +134,10 @@ export function invokeBatchGetDocumentsRpc(
114134
datastore: Datastore,
115135
keys: DocumentKey[]
116136
): Promise<MaybeDocument[]> {
137+
debugAssert(
138+
datastore instanceof DatastoreImpl,
139+
'invokeBatchGetDocumentsRpc() requires DatastoreImpl'
140+
);
117141
const params: BatchGetDocumentsRequest = {
118142
database: datastore.serializer.encodedDatabaseId,
119143
documents: keys.map(k => datastore.serializer.toName(k))
@@ -145,6 +169,10 @@ export function newPersistentWriteStream(
145169
queue: AsyncQueue,
146170
listener: WriteStreamListener
147171
): PersistentWriteStream {
172+
debugAssert(
173+
datastore instanceof DatastoreImpl,
174+
'newPersistentWriteStream() requires DatastoreImpl'
175+
);
148176
return new PersistentWriteStream(
149177
queue,
150178
datastore.connection,
@@ -159,6 +187,10 @@ export function newPersistentWatchStream(
159187
queue: AsyncQueue,
160188
listener: WatchStreamListener
161189
): PersistentListenStream {
190+
debugAssert(
191+
datastore instanceof DatastoreImpl,
192+
'newPersistentWatchStream() requires DatastoreImpl'
193+
);
162194
return new PersistentListenStream(
163195
queue,
164196
datastore.connection,

packages/firestore/test/integration/util/internal_helpers.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
import * as firestore from '@firebase/firestore-types';
1919

2020
import { DatabaseId, DatabaseInfo } from '../../../src/core/database_info';
21-
import { Datastore } from '../../../src/remote/datastore';
21+
import { createDatastore, Datastore } from '../../../src/remote/datastore';
2222

2323
import {
2424
CredentialChangeListener,
@@ -61,7 +61,7 @@ export function withTestDatastore(
6161
const serializer = PlatformSupport.getPlatform().newSerializer(
6262
databaseInfo.databaseId
6363
);
64-
const datastore = new Datastore(conn, credentialsProvider, serializer);
64+
const datastore = createDatastore(conn, credentialsProvider, serializer);
6565
return fn(datastore);
6666
});
6767
}

packages/firestore/test/unit/specs/spec_test_runner.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ import { Mutation } from '../../../src/model/mutation';
5454
import { PlatformSupport } from '../../../src/platform/platform';
5555
import * as api from '../../../src/protos/firestore_proto_api';
5656
import { Connection, Stream } from '../../../src/remote/connection';
57-
import { Datastore } from '../../../src/remote/datastore';
57+
import { createDatastore, Datastore } from '../../../src/remote/datastore';
5858
import { ExistenceFilter } from '../../../src/remote/existence_filter';
5959
import { WriteRequest } from '../../../src/remote/persistent_stream';
6060
import { RemoteStore } from '../../../src/remote/remote_store';
@@ -470,7 +470,7 @@ abstract class TestRunner {
470470

471471
async start(): Promise<void> {
472472
this.connection = new MockConnection(this.queue);
473-
this.datastore = new Datastore(
473+
this.datastore = createDatastore(
474474
this.connection,
475475
new EmptyCredentialsProvider(),
476476
this.serializer

0 commit comments

Comments
 (0)