Skip to content

Commit be8940f

Browse files
Make Document.field() nullable (#1927)
1 parent 43f0036 commit be8940f

File tree

6 files changed

+19
-22
lines changed

6 files changed

+19
-22
lines changed

packages/firestore/src/api/database.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1294,7 +1294,7 @@ export class DocumentSnapshot implements firestore.DocumentSnapshot {
12941294
const value = this._document.data.field(
12951295
fieldPathFromArgument('DocumentSnapshot.get', fieldPath)
12961296
);
1297-
if (value !== undefined) {
1297+
if (value !== null) {
12981298
return this.convertValue(
12991299
value,
13001300
FieldValueOptions.fromSnapshotOptions(
@@ -1648,7 +1648,7 @@ export class Query implements firestore.Query {
16481648
'" is an uncommitted server timestamp. (Since the value of ' +
16491649
'this field is unknown, you cannot start/end a query with it.)'
16501650
);
1651-
} else if (value !== undefined) {
1651+
} else if (value !== null) {
16521652
components.push(value);
16531653
} else {
16541654
const field = orderBy.field.canonicalString();

packages/firestore/src/core/query.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -401,10 +401,7 @@ export class Query {
401401
private matchesOrderBy(doc: Document): boolean {
402402
for (const orderBy of this.explicitOrderBy) {
403403
// order by key always matches
404-
if (
405-
!orderBy.field.isKeyField() &&
406-
doc.field(orderBy.field) === undefined
407-
) {
404+
if (!orderBy.field.isKeyField() && doc.field(orderBy.field) === null) {
408405
return false;
409406
}
410407
}
@@ -572,7 +569,7 @@ export class FieldFilter extends Filter {
572569

573570
// Only compare types with matching backend order (such as double and int).
574571
return (
575-
other !== undefined &&
572+
other !== null &&
576573
this.value.typeOrder === other.typeOrder &&
577574
this.matchesComparison(other.compareTo(this.value))
578575
);
@@ -676,7 +673,7 @@ export class InFilter extends FieldFilter {
676673
matches(doc: Document): boolean {
677674
const arrayValue = this.value;
678675
const other = doc.field(this.field);
679-
return other !== undefined && arrayValue.contains(other);
676+
return other !== null && arrayValue.contains(other);
680677
}
681678
}
682679

packages/firestore/src/model/document.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ export class Document extends MaybeDocument {
7575
this.hasCommittedMutations = !!options.hasCommittedMutations;
7676
}
7777

78-
field(path: FieldPath): FieldValue | undefined {
78+
field(path: FieldPath): FieldValue | null {
7979
return this.data.field(path);
8080
}
8181

@@ -114,7 +114,7 @@ export class Document extends MaybeDocument {
114114
static compareByField(field: FieldPath, d1: Document, d2: Document): number {
115115
const v1 = d1.field(field);
116116
const v2 = d2.field(field);
117-
if (v1 !== undefined && v2 !== undefined) {
117+
if (v1 !== null && v2 !== null) {
118118
return v1.compareTo(v2);
119119
} else {
120120
return fail("Trying to compare documents on fields that don't exist");

packages/firestore/src/model/field_value.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -589,17 +589,17 @@ export class ObjectValue extends FieldValue {
589589
}
590590

591591
contains(path: FieldPath): boolean {
592-
return this.field(path) !== undefined;
592+
return this.field(path) !== null;
593593
}
594594

595-
field(path: FieldPath): FieldValue | undefined {
595+
field(path: FieldPath): FieldValue | null {
596596
assert(!path.isEmpty(), "Can't get field of empty path");
597-
let field: FieldValue | undefined = this;
597+
let field: FieldValue | null = this;
598598
path.forEach((pathSegment: string) => {
599599
if (field instanceof ObjectValue) {
600-
field = field.internalValue.get(pathSegment) || undefined;
600+
field = field.internalValue.get(pathSegment);
601601
} else {
602-
field = undefined;
602+
field = null;
603603
}
604604
});
605605
return field;

packages/firestore/src/model/mutation.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ export class FieldMask {
8585
return data;
8686
} else {
8787
const newValue = data.field(fieldMaskPath);
88-
if (newValue !== undefined) {
88+
if (newValue !== null) {
8989
filteredObject = filteredObject.set(fieldMaskPath, newValue);
9090
}
9191
}
@@ -511,7 +511,7 @@ export class PatchMutation extends Mutation {
511511
this.fieldMask.fields.forEach(fieldPath => {
512512
if (!fieldPath.isEmpty()) {
513513
const newValue = this.data.field(fieldPath);
514-
if (newValue !== undefined) {
514+
if (newValue !== null) {
515515
data = data.set(fieldPath, newValue);
516516
} else {
517517
data = data.delete(fieldPath);
@@ -671,7 +671,7 @@ export class TransformMutation extends Mutation {
671671
const transform = fieldTransform.transform;
672672
let previousValue: FieldValue | null = null;
673673
if (baseDoc instanceof Document) {
674-
previousValue = baseDoc.field(fieldTransform.field) || null;
674+
previousValue = baseDoc.field(fieldTransform.field);
675675
}
676676
transformResults.push(
677677
transform.applyToRemoteDocument(
@@ -703,7 +703,7 @@ export class TransformMutation extends Mutation {
703703

704704
let previousValue: FieldValue | null = null;
705705
if (baseDoc instanceof Document) {
706-
previousValue = baseDoc.field(fieldTransform.field) || null;
706+
previousValue = baseDoc.field(fieldTransform.field);
707707
}
708708

709709
transformResults.push(

packages/firestore/test/unit/model/field_value.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -193,9 +193,9 @@ describe('FieldValue', () => {
193193
fieldValue.StringValue
194194
);
195195

196-
expect(objValue.field(field('foo.a.b'))).to.equal(undefined);
197-
expect(objValue.field(field('bar'))).to.equal(undefined);
198-
expect(objValue.field(field('bar.a'))).to.equal(undefined);
196+
expect(objValue.field(field('foo.a.b'))).to.be.null;
197+
expect(objValue.field(field('bar'))).to.be.null;
198+
expect(objValue.field(field('bar.a'))).to.be.null;
199199

200200
expect(objValue.field(field('foo'))!.value()).to.deep.equal({
201201
a: 1,

0 commit comments

Comments
 (0)