Skip to content

Commit

Permalink
Fall back to updateTime if no readTime exists (#843)
Browse files Browse the repository at this point in the history
Fixes #599
  • Loading branch information
iwikal committed Jun 24, 2021
1 parent 8858937 commit 4028acc
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
13 changes: 13 additions & 0 deletions spec/providers/firestore.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,7 @@ describe('Firestore Functions', () => {

describe('Other DocumentSnapshot methods', () => {
let snapshot: FirebaseFirestore.DocumentSnapshot;
let newSnapshot: FirebaseFirestore.DocumentSnapshot;

before(() => {
snapshot = firestore.snapshotConstructor(
Expand All @@ -579,6 +580,16 @@ describe('Firestore Functions', () => {
},
})
);
newSnapshot = firestore.snapshotConstructor(
makeEvent({
value: {
fields: { key: { integerValue: '2' } },
createTime: '2017-06-17T14:45:17.876479Z',
updateTime: '2017-06-17T14:45:17.876479Z',
name: 'projects/pid/databases/(default)/documents/collection/124',
},
})
);
});

it('should support #exists', () => {
Expand Down Expand Up @@ -606,6 +617,8 @@ describe('Firestore Functions', () => {
it('should support #readTime', () => {
expect(snapshot.readTime.seconds).to.be.a('number');
expect(snapshot.readTime.nanoseconds).to.be.a('number');
expect(newSnapshot.readTime.seconds).to.be.a('number');
expect(newSnapshot.readTime.nanoseconds).to.be.a('number');
});
});

Expand Down
5 changes: 4 additions & 1 deletion src/providers/firestore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,10 @@ export function snapshotConstructor(event: Event): DocumentSnapshot {
event.context.resource.name,
'value'
);
const readTime = dateToTimestampProto(_.get(event, 'data.value.readTime'));
const timeString =
_.get(event, 'data.value.readTime') ??
_.get(event, 'data.value.updateTime');
const readTime = dateToTimestampProto(timeString);
return firestoreInstance.snapshot_(valueProto, readTime, 'json');
}

Expand Down

0 comments on commit 4028acc

Please sign in to comment.