Skip to content

Commit 723552f

Browse files
Finish FieldValue migration
1 parent 1a0b08d commit 723552f

36 files changed

+1693
-2660
lines changed

packages/firestore/src/api/database.ts

Lines changed: 41 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
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';
2123
import { FirebaseService, _FirebaseApp } from '@firebase/app-types/private';
2224
import { DatabaseId, DatabaseInfo } from '../core/database_info';
@@ -42,12 +44,6 @@ import { IndexedDbPersistence } from '../local/indexeddb_persistence';
4244
import { LruParams } from '../local/lru_garbage_collector';
4345
import { Document, MaybeDocument, NoDocument } from '../model/document';
4446
import { DocumentKey } from '../model/document_key';
45-
import {
46-
ArrayValue,
47-
FieldValue,
48-
RefValue,
49-
ServerTimestampValue
50-
} from '../model/field_value';
5147
import { DeleteMutation, Mutation, Precondition } from '../model/mutation';
5248
import { FieldPath, ResourcePath } from '../model/path';
5349
import { PlatformSupport } from '../platform/platform';
@@ -106,6 +102,8 @@ import {
106102
import { UserDataWriter } from './user_data_writer';
107103
import { FirebaseAuthInternalName } from '@firebase/auth-interop-types';
108104
import { Provider } from '@firebase/component';
105+
import { isServerTimestamp } from '../model/server_timestamps';
106+
import { JsonProtoSerializer } from '../remote/serializer';
109107

110108
// settings() defaults:
111109
const DEFAULT_HOST = 'firestore.googleapis.com';
@@ -550,7 +548,12 @@ export class Firestore implements firestore.FirebaseFirestore, FirebaseService {
550548
return value;
551549
}
552550
};
553-
return new UserDataReader(preConverter);
551+
return new UserDataReader(
552+
new JsonProtoSerializer(databaseId, {
553+
useProto3Json: PlatformSupport.getPlatform().usesProto3Json
554+
}),
555+
preConverter
556+
);
554557
}
555558

556559
private static databaseIdFromApp(app: FirebaseApp): DatabaseId {
@@ -1352,14 +1355,20 @@ export interface SnapshotOptions extends firestore.SnapshotOptions {}
13521355

13531356
export class DocumentSnapshot<T = firestore.DocumentData>
13541357
implements firestore.DocumentSnapshot<T> {
1358+
private readonly _serializer: JsonProtoSerializer;
1359+
13551360
constructor(
13561361
private _firestore: Firestore,
13571362
private _key: DocumentKey,
13581363
public _document: Document | null,
13591364
private _fromCache: boolean,
13601365
private _hasPendingWrites: boolean,
13611366
private readonly _converter?: firestore.FirestoreDataConverter<T>
1362-
) {}
1367+
) {
1368+
this._serializer = new JsonProtoSerializer(this._firestore._databaseId, {
1369+
useProto3Json: PlatformSupport.getPlatform().usesProto3Json
1370+
});
1371+
}
13631372

13641373
data(options?: firestore.SnapshotOptions): T | undefined {
13651374
validateBetweenNumberOfArgs('DocumentSnapshot.data', arguments, 0, 1);
@@ -1381,11 +1390,13 @@ export class DocumentSnapshot<T = firestore.DocumentData>
13811390
} else {
13821391
const userDataWriter = new UserDataWriter(
13831392
this._firestore,
1393+
this._serializer,
13841394
this._firestore._areTimestampsInSnapshotsEnabled(),
13851395
options.serverTimestamps,
13861396
/* converter= */ undefined
13871397
);
1388-
return userDataWriter.convertValue(this._document.data()) as T;
1398+
const proto = this._document.data().toProto();
1399+
return userDataWriter.convertValue(proto) as T;
13891400
}
13901401
}
13911402
}
@@ -1403,6 +1414,7 @@ export class DocumentSnapshot<T = firestore.DocumentData>
14031414
if (value !== null) {
14041415
const userDataWriter = new UserDataWriter(
14051416
this._firestore,
1417+
this._serializer,
14061418
this._firestore._areTimestampsInSnapshotsEnabled(),
14071419
options.serverTimestamps,
14081420
this._converter
@@ -1490,7 +1502,7 @@ export class Query<T = firestore.DocumentData> implements firestore.Query<T> {
14901502
];
14911503
validateStringEnum('Query.where', whereFilterOpEnums, 2, opStr);
14921504

1493-
let fieldValue: FieldValue;
1505+
let fieldValue: api.Value;
14941506
const fieldPath = fieldPathFromArgument('Query.where', field);
14951507
const operator = Operator.fromString(opStr);
14961508
if (fieldPath.isKeyField()) {
@@ -1505,11 +1517,11 @@ export class Query<T = firestore.DocumentData> implements firestore.Query<T> {
15051517
);
15061518
} else if (operator === Operator.IN) {
15071519
this.validateDisjunctiveFilterElements(value, operator);
1508-
const referenceList: FieldValue[] = [];
1509-
for (const arrayValue of value as FieldValue[]) {
1520+
const referenceList: api.Value[] = [];
1521+
for (const arrayValue of value as api.Value[]) {
15101522
referenceList.push(this.parseDocumentIdValue(arrayValue));
15111523
}
1512-
fieldValue = new ArrayValue(referenceList);
1524+
fieldValue = { arrayValue: { values: referenceList } };
15131525
} else {
15141526
fieldValue = this.parseDocumentIdValue(value);
15151527
}
@@ -1715,7 +1727,7 @@ export class Query<T = firestore.DocumentData> implements firestore.Query<T> {
17151727
`${methodName}().`
17161728
);
17171729
}
1718-
return this.boundFromDocument(methodName, snap._document!, before);
1730+
return this.boundFromDocument(snap._document!, before);
17191731
} else {
17201732
const allFields = [docOrField].concat(fields);
17211733
return this.boundFromFields(methodName, allFields, before);
@@ -1733,12 +1745,8 @@ export class Query<T = firestore.DocumentData> implements firestore.Query<T> {
17331745
* of the query or if any of the fields in the order by are an uncommitted
17341746
* server timestamp.
17351747
*/
1736-
private boundFromDocument(
1737-
methodName: string,
1738-
doc: Document,
1739-
before: boolean
1740-
): Bound {
1741-
const components: FieldValue[] = [];
1748+
private boundFromDocument(doc: Document, before: boolean): Bound {
1749+
const components: api.Value[] = [];
17421750

17431751
// Because people expect to continue/end a query at the exact document
17441752
// provided, we need to use the implicit sort order rather than the explicit
@@ -1749,10 +1757,12 @@ export class Query<T = firestore.DocumentData> implements firestore.Query<T> {
17491757
// results.
17501758
for (const orderBy of this._query.orderBy) {
17511759
if (orderBy.field.isKeyField()) {
1752-
components.push(new RefValue(this.firestore._databaseId, doc.key));
1760+
components.push({
1761+
referenceValue: doc.key.toName(this.firestore._databaseId)
1762+
});
17531763
} else {
17541764
const value = doc.field(orderBy.field);
1755-
if (value instanceof ServerTimestampValue) {
1765+
if (isServerTimestamp(value)) {
17561766
throw new FirestoreError(
17571767
Code.INVALID_ARGUMENT,
17581768
'Invalid query. You are trying to start or end a query using a ' +
@@ -1796,7 +1806,7 @@ export class Query<T = firestore.DocumentData> implements firestore.Query<T> {
17961806
);
17971807
}
17981808

1799-
const components: FieldValue[] = [];
1809+
const components: api.Value[] = [];
18001810
for (let i = 0; i < values.length; i++) {
18011811
const rawValue = values[i];
18021812
const orderByComponent = orderBy[i];
@@ -1830,7 +1840,9 @@ export class Query<T = firestore.DocumentData> implements firestore.Query<T> {
18301840
);
18311841
}
18321842
const key = new DocumentKey(path);
1833-
components.push(new RefValue(this.firestore._databaseId, key));
1843+
components.push({
1844+
referenceValue: key.toName(this.firestore._databaseId)
1845+
});
18341846
} else {
18351847
const wrapped = this.firestore._dataReader.parseQueryValue(
18361848
methodName,
@@ -2029,7 +2041,7 @@ export class Query<T = firestore.DocumentData> implements firestore.Query<T> {
20292041
* appropriate errors if the value is anything other than a DocumentReference
20302042
* or String, or if the string is malformed.
20312043
*/
2032-
private parseDocumentIdValue(documentIdValue: unknown): RefValue {
2044+
private parseDocumentIdValue(documentIdValue: unknown): api.Value {
20332045
if (typeof documentIdValue === 'string') {
20342046
if (documentIdValue === '') {
20352047
throw new FirestoreError(
@@ -2060,10 +2072,12 @@ export class Query<T = firestore.DocumentData> implements firestore.Query<T> {
20602072
`but '${path}' is not because it has an odd number of segments (${path.length}).`
20612073
);
20622074
}
2063-
return new RefValue(this.firestore._databaseId, new DocumentKey(path));
2075+
return {
2076+
referenceValue: new DocumentKey(path).toName(this.firestore._databaseId)
2077+
};
20642078
} else if (documentIdValue instanceof DocumentReference) {
20652079
const ref = documentIdValue as DocumentReference<T>;
2066-
return new RefValue(this.firestore._databaseId, ref._key);
2080+
return { referenceValue: ref._key.toName(this.firestore._databaseId) };
20672081
} else {
20682082
throw new FirestoreError(
20692083
Code.INVALID_ARGUMENT,

0 commit comments

Comments
 (0)