Skip to content

Commit 7080047

Browse files
committed
Add createTime to Document.
1 parent 2fdd601 commit 7080047

File tree

5 files changed

+79
-15
lines changed

5 files changed

+79
-15
lines changed

packages/firestore/src/model/document.ts

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,13 @@ export interface Document {
101101
*/
102102
readonly readTime: SnapshotVersion;
103103

104+
/**
105+
* The timestamp at which the document was created. This value increases
106+
* monotonically when a document is deleted then recreated. It can also be
107+
* compared to `createTime` of other documents and the `readTime` of a query.
108+
*/
109+
readonly createTime: SnapshotVersion;
110+
104111
/** The underlying data of this document or an empty value if no data exists. */
105112
readonly data: ObjectValue;
106113

@@ -163,6 +170,7 @@ export class MutableDocument implements Document {
163170
private documentType: DocumentType,
164171
public version: SnapshotVersion,
165172
public readTime: SnapshotVersion,
173+
public createTime: SnapshotVersion,
166174
public data: ObjectValue,
167175
private documentState: DocumentState
168176
) {}
@@ -175,8 +183,9 @@ export class MutableDocument implements Document {
175183
return new MutableDocument(
176184
documentKey,
177185
DocumentType.INVALID,
178-
SnapshotVersion.min(),
179-
SnapshotVersion.min(),
186+
/* version */ SnapshotVersion.min(),
187+
/* readTime */ SnapshotVersion.min(),
188+
/* createTime */ SnapshotVersion.min(),
180189
ObjectValue.empty(),
181190
DocumentState.SYNCED
182191
);
@@ -189,13 +198,15 @@ export class MutableDocument implements Document {
189198
static newFoundDocument(
190199
documentKey: DocumentKey,
191200
version: SnapshotVersion,
201+
createTime: SnapshotVersion,
192202
value: ObjectValue
193203
): MutableDocument {
194204
return new MutableDocument(
195205
documentKey,
196206
DocumentType.FOUND_DOCUMENT,
197-
version,
198-
SnapshotVersion.min(),
207+
/* version */ version,
208+
/* readTime */ SnapshotVersion.min(),
209+
/* createTime */ createTime,
199210
value,
200211
DocumentState.SYNCED
201212
);
@@ -209,8 +220,9 @@ export class MutableDocument implements Document {
209220
return new MutableDocument(
210221
documentKey,
211222
DocumentType.NO_DOCUMENT,
212-
version,
213-
SnapshotVersion.min(),
223+
/* version */ version,
224+
/* readTime */ SnapshotVersion.min(),
225+
/* createTime */ SnapshotVersion.min(),
214226
ObjectValue.empty(),
215227
DocumentState.SYNCED
216228
);
@@ -228,8 +240,9 @@ export class MutableDocument implements Document {
228240
return new MutableDocument(
229241
documentKey,
230242
DocumentType.UNKNOWN_DOCUMENT,
231-
version,
232-
SnapshotVersion.min(),
243+
/* version */ version,
244+
/* readTime */ SnapshotVersion.min(),
245+
/* createTime */ SnapshotVersion.min(),
233246
ObjectValue.empty(),
234247
DocumentState.HAS_COMMITTED_MUTATIONS
235248
);
@@ -340,6 +353,7 @@ export class MutableDocument implements Document {
340353
this.documentType,
341354
this.version,
342355
this.readTime,
356+
this.createTime,
343357
this.data.clone(),
344358
this.documentState
345359
);

packages/firestore/src/remote/serializer.ts

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,8 @@ export function toDocument(
395395
return {
396396
name: toName(serializer, document.key),
397397
fields: document.data.value.mapValue.fields,
398-
updateTime: toTimestamp(serializer, document.version.toTimestamp())
398+
updateTime: toTimestamp(serializer, document.version.toTimestamp()),
399+
createTime: toTimestamp(serializer, document.createTime.toTimestamp())
399400
};
400401
}
401402

@@ -406,8 +407,16 @@ export function fromDocument(
406407
): MutableDocument {
407408
const key = fromName(serializer, document.name!);
408409
const version = fromVersion(document.updateTime!);
410+
const createTime = document.createTime
411+
? fromVersion(document.createTime)
412+
: SnapshotVersion.min();
409413
const data = new ObjectValue({ mapValue: { fields: document.fields } });
410-
const result = MutableDocument.newFoundDocument(key, version, data);
414+
const result = MutableDocument.newFoundDocument(
415+
key,
416+
version,
417+
createTime,
418+
data
419+
);
411420
if (hasCommittedMutations) {
412421
result.setHasCommittedMutations();
413422
}
@@ -426,8 +435,11 @@ function fromFound(
426435
assertPresent(doc.found.updateTime, 'doc.found.updateTime');
427436
const key = fromName(serializer, doc.found.name);
428437
const version = fromVersion(doc.found.updateTime);
438+
const createTime = doc.found.createTime
439+
? fromVersion(doc.found.createTime)
440+
: SnapshotVersion.min();
429441
const data = new ObjectValue({ mapValue: { fields: doc.found.fields } });
430-
return MutableDocument.newFoundDocument(key, version, data);
442+
return MutableDocument.newFoundDocument(key, version, createTime, data);
431443
}
432444

433445
function fromMissing(
@@ -493,10 +505,18 @@ export function fromWatchChange(
493505
);
494506
const key = fromName(serializer, entityChange.document.name);
495507
const version = fromVersion(entityChange.document.updateTime);
508+
const createTime = entityChange.document.createTime
509+
? fromVersion(entityChange.document.createTime)
510+
: SnapshotVersion.min();
496511
const data = new ObjectValue({
497512
mapValue: { fields: entityChange.document.fields }
498513
});
499-
const doc = MutableDocument.newFoundDocument(key, version, data);
514+
const doc = MutableDocument.newFoundDocument(
515+
key,
516+
version,
517+
createTime,
518+
data
519+
);
500520
const updatedTargetIds = entityChange.targetIds || [];
501521
const removedTargetIds = entityChange.removedTargetIds || [];
502522
watchChange = new DocumentWatchChange(

packages/firestore/test/unit/local/local_store.test.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1958,6 +1958,33 @@ function genericLocalStoreTests(
19581958
.finish();
19591959
});
19601960

1961+
it('handles document creation time', () => {
1962+
return (
1963+
expectLocalStore()
1964+
.afterAllocatingQuery(query('col'))
1965+
.toReturnTargetId(2)
1966+
.after(docAddedRemoteEvent(doc('col/doc1', 12, { foo: 'bar' }, 5), [2]))
1967+
.toReturnChanged(doc('col/doc1', 12, { foo: 'bar' }, 5))
1968+
.toContain(doc('col/doc1', 12, { foo: 'bar' }, 5))
1969+
.after(setMutation('col/doc1', { foo: 'newBar' }))
1970+
.toReturnChanged(
1971+
doc('col/doc1', 12, { foo: 'newBar' }, 5).setHasLocalMutations()
1972+
)
1973+
.toContain(
1974+
doc('col/doc1', 12, { foo: 'newBar' }, 5).setHasLocalMutations()
1975+
)
1976+
.afterAcknowledgingMutation({ documentVersion: 13 })
1977+
// We haven't seen the remote event yet
1978+
.toReturnChanged(
1979+
doc('col/doc1', 13, { foo: 'newBar' }, 5).setHasCommittedMutations()
1980+
)
1981+
.toContain(
1982+
doc('col/doc1', 13, { foo: 'newBar' }, 5).setHasCommittedMutations()
1983+
)
1984+
.finish()
1985+
);
1986+
});
1987+
19611988
it('uses target mapping to execute queries', () => {
19621989
if (gcIsEager) {
19631990
return;

packages/firestore/test/unit/remote/serializer.helper.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -799,11 +799,12 @@ export function serializerTest(
799799
});
800800

801801
it('toDocument() / fromDocument', () => {
802-
const d = doc('foo/bar', 42, { a: 5, b: 'b' });
802+
const d = doc('foo/bar', 42, { a: 5, b: 'b' }, /* createTime */ 12);
803803
const proto = {
804804
name: toName(s, d.key),
805805
fields: d.data.value.mapValue.fields,
806-
updateTime: toVersion(s, d.version)
806+
updateTime: toVersion(s, d.version),
807+
createTime: toVersion(s, d.createTime)
807808
};
808809
const serialized = toDocument(s, d);
809810
expect(serialized).to.deep.equal(proto);

packages/firestore/test/util/helpers.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,11 +155,13 @@ export function ref(key: string, offset?: number): DocumentReference {
155155
export function doc(
156156
keyStr: string,
157157
ver: TestSnapshotVersion,
158-
jsonOrObjectValue: JsonObject<unknown> | ObjectValue
158+
jsonOrObjectValue: JsonObject<unknown> | ObjectValue,
159+
createTime?: TestSnapshotVersion
159160
): MutableDocument {
160161
return MutableDocument.newFoundDocument(
161162
key(keyStr),
162163
version(ver),
164+
createTime ? version(createTime) : SnapshotVersion.min(),
163165
jsonOrObjectValue instanceof ObjectValue
164166
? jsonOrObjectValue
165167
: wrapObject(jsonOrObjectValue)

0 commit comments

Comments
 (0)