Skip to content

Commit 5a5fe8f

Browse files
Finish FieldValue migration
1 parent 3d7c892 commit 5a5fe8f

34 files changed

+1374
-995
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';
@@ -38,12 +40,6 @@ 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';
4945
import { PlatformSupport } from '../platform/platform';
@@ -101,6 +97,8 @@ import {
10197
import { UserDataWriter } from './user_data_writer';
10298
import { FirebaseAuthInternalName } from '@firebase/auth-interop-types';
10399
import { Provider } from '@firebase/component';
100+
import { isServerTimestamp } from '../model/server_timestamps';
101+
import { JsonProtoSerializer } from '../remote/serializer';
104102

105103
// settings() defaults:
106104
const DEFAULT_HOST = 'firestore.googleapis.com';
@@ -555,7 +553,12 @@ export class Firestore implements firestore.FirebaseFirestore, FirebaseService {
555553
return value;
556554
}
557555
};
558-
return new UserDataReader(preConverter);
556+
return new UserDataReader(
557+
new JsonProtoSerializer(databaseId, {
558+
useProto3Json: PlatformSupport.getPlatform().useProto3Json
559+
}),
560+
preConverter
561+
);
559562
}
560563

561564
private static databaseIdFromApp(app: FirebaseApp): DatabaseId {
@@ -1357,14 +1360,20 @@ export interface SnapshotOptions extends firestore.SnapshotOptions {}
13571360

13581361
export class DocumentSnapshot<T = firestore.DocumentData>
13591362
implements firestore.DocumentSnapshot<T> {
1363+
private readonly _serializer: JsonProtoSerializer;
1364+
13601365
constructor(
13611366
private _firestore: Firestore,
13621367
private _key: DocumentKey,
13631368
public _document: Document | null,
13641369
private _fromCache: boolean,
13651370
private _hasPendingWrites: boolean,
13661371
private readonly _converter?: firestore.FirestoreDataConverter<T>
1367-
) {}
1372+
) {
1373+
this._serializer = new JsonProtoSerializer(this._firestore._databaseId, {
1374+
useProto3Json: PlatformSupport.getPlatform().useProto3Json
1375+
});
1376+
}
13681377

13691378
data(options?: firestore.SnapshotOptions): T | undefined {
13701379
validateBetweenNumberOfArgs('DocumentSnapshot.data', arguments, 0, 1);
@@ -1386,11 +1395,13 @@ export class DocumentSnapshot<T = firestore.DocumentData>
13861395
} else {
13871396
const userDataWriter = new UserDataWriter(
13881397
this._firestore,
1398+
this._serializer,
13891399
this._firestore._areTimestampsInSnapshotsEnabled(),
13901400
options.serverTimestamps,
13911401
/* converter= */ undefined
13921402
);
1393-
return userDataWriter.convertValue(this._document.data()) as T;
1403+
const proto = this._document.data().toProto();
1404+
return userDataWriter.convertValue(proto) as T;
13941405
}
13951406
}
13961407
}
@@ -1408,6 +1419,7 @@ export class DocumentSnapshot<T = firestore.DocumentData>
14081419
if (value !== null) {
14091420
const userDataWriter = new UserDataWriter(
14101421
this._firestore,
1422+
this._serializer,
14111423
this._firestore._areTimestampsInSnapshotsEnabled(),
14121424
options.serverTimestamps,
14131425
this._converter
@@ -1495,7 +1507,7 @@ export class Query<T = firestore.DocumentData> implements firestore.Query<T> {
14951507
];
14961508
validateStringEnum('Query.where', whereFilterOpEnums, 2, opStr);
14971509

1498-
let fieldValue: FieldValue;
1510+
let fieldValue: api.Value;
14991511
const fieldPath = fieldPathFromArgument('Query.where', field);
15001512
const operator = Operator.fromString(opStr);
15011513
if (fieldPath.isKeyField()) {
@@ -1510,11 +1522,11 @@ export class Query<T = firestore.DocumentData> implements firestore.Query<T> {
15101522
);
15111523
} else if (operator === Operator.IN) {
15121524
this.validateDisjunctiveFilterElements(value, operator);
1513-
const referenceList: FieldValue[] = [];
1514-
for (const arrayValue of value as FieldValue[]) {
1525+
const referenceList: api.Value[] = [];
1526+
for (const arrayValue of value as api.Value[]) {
15151527
referenceList.push(this.parseDocumentIdValue(arrayValue));
15161528
}
1517-
fieldValue = new ArrayValue(referenceList);
1529+
fieldValue = { arrayValue: { values: referenceList } };
15181530
} else {
15191531
fieldValue = this.parseDocumentIdValue(value);
15201532
}
@@ -1720,7 +1732,7 @@ export class Query<T = firestore.DocumentData> implements firestore.Query<T> {
17201732
`${methodName}().`
17211733
);
17221734
}
1723-
return this.boundFromDocument(methodName, snap._document!, before);
1735+
return this.boundFromDocument(snap._document!, before);
17241736
} else {
17251737
const allFields = [docOrField].concat(fields);
17261738
return this.boundFromFields(methodName, allFields, before);
@@ -1738,12 +1750,8 @@ export class Query<T = firestore.DocumentData> implements firestore.Query<T> {
17381750
* of the query or if any of the fields in the order by are an uncommitted
17391751
* server timestamp.
17401752
*/
1741-
private boundFromDocument(
1742-
methodName: string,
1743-
doc: Document,
1744-
before: boolean
1745-
): Bound {
1746-
const components: FieldValue[] = [];
1753+
private boundFromDocument(doc: Document, before: boolean): Bound {
1754+
const components: api.Value[] = [];
17471755

17481756
// Because people expect to continue/end a query at the exact document
17491757
// provided, we need to use the implicit sort order rather than the explicit
@@ -1754,10 +1762,12 @@ export class Query<T = firestore.DocumentData> implements firestore.Query<T> {
17541762
// results.
17551763
for (const orderBy of this._query.orderBy) {
17561764
if (orderBy.field.isKeyField()) {
1757-
components.push(new RefValue(this.firestore._databaseId, doc.key));
1765+
components.push({
1766+
referenceValue: doc.key.toName(this.firestore._databaseId)
1767+
});
17581768
} else {
17591769
const value = doc.field(orderBy.field);
1760-
if (value instanceof ServerTimestampValue) {
1770+
if (isServerTimestamp(value)) {
17611771
throw new FirestoreError(
17621772
Code.INVALID_ARGUMENT,
17631773
'Invalid query. You are trying to start or end a query using a ' +
@@ -1801,7 +1811,7 @@ export class Query<T = firestore.DocumentData> implements firestore.Query<T> {
18011811
);
18021812
}
18031813

1804-
const components: FieldValue[] = [];
1814+
const components: api.Value[] = [];
18051815
for (let i = 0; i < values.length; i++) {
18061816
const rawValue = values[i];
18071817
const orderByComponent = orderBy[i];
@@ -1835,7 +1845,9 @@ export class Query<T = firestore.DocumentData> implements firestore.Query<T> {
18351845
);
18361846
}
18371847
const key = new DocumentKey(path);
1838-
components.push(new RefValue(this.firestore._databaseId, key));
1848+
components.push({
1849+
referenceValue: key.toName(this.firestore._databaseId)
1850+
});
18391851
} else {
18401852
const wrapped = this.firestore._dataReader.parseQueryValue(
18411853
methodName,
@@ -2034,7 +2046,7 @@ export class Query<T = firestore.DocumentData> implements firestore.Query<T> {
20342046
* appropriate errors if the value is anything other than a DocumentReference
20352047
* or String, or if the string is malformed.
20362048
*/
2037-
private parseDocumentIdValue(documentIdValue: unknown): RefValue {
2049+
private parseDocumentIdValue(documentIdValue: unknown): api.Value {
20382050
if (typeof documentIdValue === 'string') {
20392051
if (documentIdValue === '') {
20402052
throw new FirestoreError(
@@ -2065,10 +2077,12 @@ export class Query<T = firestore.DocumentData> implements firestore.Query<T> {
20652077
`but '${path}' is not because it has an odd number of segments (${path.length}).`
20662078
);
20672079
}
2068-
return new RefValue(this.firestore._databaseId, new DocumentKey(path));
2080+
return {
2081+
referenceValue: new DocumentKey(path).toName(this.firestore._databaseId)
2082+
};
20692083
} else if (documentIdValue instanceof DocumentReference) {
20702084
const ref = documentIdValue as DocumentReference<T>;
2071-
return new RefValue(this.firestore._databaseId, ref._key);
2085+
return { referenceValue: ref._key.toName(this.firestore._databaseId) };
20722086
} else {
20732087
throw new FirestoreError(
20742088
Code.INVALID_ARGUMENT,

0 commit comments

Comments
 (0)