Skip to content

Commit b6a60a3

Browse files
Finish FieldValue migration
1 parent 3d7c892 commit b6a60a3

35 files changed

+999
-1179
lines changed

packages/firestore/src/api/database.ts

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@
1717

1818
import * as firestore from '@firebase/firestore-types';
1919

20+
import * as api from '../protos/firestore_proto_api';
21+
2022
import { FirebaseApp } from '@firebase/app-types';
21-
import { FirebaseService, _FirebaseApp } from '@firebase/app-types/private';
23+
import { _FirebaseApp, FirebaseService } from '@firebase/app-types/private';
2224
import { DatabaseId, DatabaseInfo } from '../core/database_info';
2325
import { ListenOptions } from '../core/event_manager';
2426
import { FirestoreClient, PersistenceSettings } from '../core/firestore_client';
@@ -38,14 +40,11 @@ import { MemoryPersistenceProvider } from '../local/memory_persistence';
3840
import { PersistenceProvider } from '../local/persistence';
3941
import { Document, MaybeDocument, NoDocument } from '../model/document';
4042
import { DocumentKey } from '../model/document_key';
41-
import {
42-
ArrayValue,
43-
FieldValue,
44-
RefValue,
45-
ServerTimestampValue
46-
} from '../model/field_value';
4743
import { DeleteMutation, Mutation, Precondition } from '../model/mutation';
4844
import { FieldPath, ResourcePath } from '../model/path';
45+
import { JsonProtoSerializer } from '../remote/serializer';
46+
import { isServerTimestamp } from '../model/server_timestamps';
47+
import { refValue } from '../model/values';
4948
import { PlatformSupport } from '../platform/platform';
5049
import { makeConstructorPrivate } from '../util/api';
5150
import { assert, fail } from '../util/assert';
@@ -555,7 +554,10 @@ export class Firestore implements firestore.FirebaseFirestore, FirebaseService {
555554
return value;
556555
}
557556
};
558-
return new UserDataReader(preConverter);
557+
const serializer = new JsonProtoSerializer(databaseId, {
558+
useProto3Json: PlatformSupport.getPlatform().useProto3Json
559+
});
560+
return new UserDataReader(serializer, preConverter);
559561
}
560562

561563
private static databaseIdFromApp(app: FirebaseApp): DatabaseId {
@@ -1390,7 +1392,7 @@ export class DocumentSnapshot<T = firestore.DocumentData>
13901392
options.serverTimestamps,
13911393
/* converter= */ undefined
13921394
);
1393-
return userDataWriter.convertValue(this._document.data()) as T;
1395+
return userDataWriter.convertValue(this._document.toProto()) as T;
13941396
}
13951397
}
13961398
}
@@ -1495,7 +1497,7 @@ export class Query<T = firestore.DocumentData> implements firestore.Query<T> {
14951497
];
14961498
validateStringEnum('Query.where', whereFilterOpEnums, 2, opStr);
14971499

1498-
let fieldValue: FieldValue;
1500+
let fieldValue: api.Value;
14991501
const fieldPath = fieldPathFromArgument('Query.where', field);
15001502
const operator = Operator.fromString(opStr);
15011503
if (fieldPath.isKeyField()) {
@@ -1510,11 +1512,11 @@ export class Query<T = firestore.DocumentData> implements firestore.Query<T> {
15101512
);
15111513
} else if (operator === Operator.IN) {
15121514
this.validateDisjunctiveFilterElements(value, operator);
1513-
const referenceList: FieldValue[] = [];
1514-
for (const arrayValue of value as FieldValue[]) {
1515+
const referenceList: api.Value[] = [];
1516+
for (const arrayValue of value as api.Value[]) {
15151517
referenceList.push(this.parseDocumentIdValue(arrayValue));
15161518
}
1517-
fieldValue = new ArrayValue(referenceList);
1519+
fieldValue = { arrayValue: { values: referenceList } };
15181520
} else {
15191521
fieldValue = this.parseDocumentIdValue(value);
15201522
}
@@ -1720,7 +1722,7 @@ export class Query<T = firestore.DocumentData> implements firestore.Query<T> {
17201722
`${methodName}().`
17211723
);
17221724
}
1723-
return this.boundFromDocument(methodName, snap._document!, before);
1725+
return this.boundFromDocument(snap._document!, before);
17241726
} else {
17251727
const allFields = [docOrField].concat(fields);
17261728
return this.boundFromFields(methodName, allFields, before);
@@ -1738,12 +1740,8 @@ export class Query<T = firestore.DocumentData> implements firestore.Query<T> {
17381740
* of the query or if any of the fields in the order by are an uncommitted
17391741
* server timestamp.
17401742
*/
1741-
private boundFromDocument(
1742-
methodName: string,
1743-
doc: Document,
1744-
before: boolean
1745-
): Bound {
1746-
const components: FieldValue[] = [];
1743+
private boundFromDocument(doc: Document, before: boolean): Bound {
1744+
const components: api.Value[] = [];
17471745

17481746
// Because people expect to continue/end a query at the exact document
17491747
// provided, we need to use the implicit sort order rather than the explicit
@@ -1754,10 +1752,10 @@ export class Query<T = firestore.DocumentData> implements firestore.Query<T> {
17541752
// results.
17551753
for (const orderBy of this._query.orderBy) {
17561754
if (orderBy.field.isKeyField()) {
1757-
components.push(new RefValue(this.firestore._databaseId, doc.key));
1755+
components.push(refValue(this.firestore._databaseId, doc.key));
17581756
} else {
17591757
const value = doc.field(orderBy.field);
1760-
if (value instanceof ServerTimestampValue) {
1758+
if (isServerTimestamp(value)) {
17611759
throw new FirestoreError(
17621760
Code.INVALID_ARGUMENT,
17631761
'Invalid query. You are trying to start or end a query using a ' +
@@ -1801,7 +1799,7 @@ export class Query<T = firestore.DocumentData> implements firestore.Query<T> {
18011799
);
18021800
}
18031801

1804-
const components: FieldValue[] = [];
1802+
const components: api.Value[] = [];
18051803
for (let i = 0; i < values.length; i++) {
18061804
const rawValue = values[i];
18071805
const orderByComponent = orderBy[i];
@@ -1835,7 +1833,7 @@ export class Query<T = firestore.DocumentData> implements firestore.Query<T> {
18351833
);
18361834
}
18371835
const key = new DocumentKey(path);
1838-
components.push(new RefValue(this.firestore._databaseId, key));
1836+
components.push(refValue(this.firestore._databaseId, key));
18391837
} else {
18401838
const wrapped = this.firestore._dataReader.parseQueryValue(
18411839
methodName,
@@ -2034,7 +2032,7 @@ export class Query<T = firestore.DocumentData> implements firestore.Query<T> {
20342032
* appropriate errors if the value is anything other than a DocumentReference
20352033
* or String, or if the string is malformed.
20362034
*/
2037-
private parseDocumentIdValue(documentIdValue: unknown): RefValue {
2035+
private parseDocumentIdValue(documentIdValue: unknown): api.Value {
20382036
if (typeof documentIdValue === 'string') {
20392037
if (documentIdValue === '') {
20402038
throw new FirestoreError(
@@ -2065,10 +2063,10 @@ export class Query<T = firestore.DocumentData> implements firestore.Query<T> {
20652063
`but '${path}' is not because it has an odd number of segments (${path.length}).`
20662064
);
20672065
}
2068-
return new RefValue(this.firestore._databaseId, new DocumentKey(path));
2066+
return refValue(this.firestore._databaseId, new DocumentKey(path));
20692067
} else if (documentIdValue instanceof DocumentReference) {
20702068
const ref = documentIdValue as DocumentReference<T>;
2071-
return new RefValue(this.firestore._databaseId, ref._key);
2069+
return refValue(this.firestore._databaseId, ref._key);
20722070
} else {
20732071
throw new FirestoreError(
20742072
Code.INVALID_ARGUMENT,

0 commit comments

Comments
 (0)