@@ -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 ) ;
@@ -243,6 +256,18 @@ export class MutableDocument implements Document {
243256 version : SnapshotVersion ,
244257 value : ObjectValue
245258 ) : MutableDocument {
259+ // If a document is switching state from being an invalid or deleted
260+ // document to a valid (FOUND_DOCUMENT) document, either due to receiving an
261+ // update from Watch or due to applying a local set mutation on top
262+ // of a deleted document, our best guess about its createTime would be the
263+ // version at which the document transitioned to a FOUND_DOCUMENT.
264+ if (
265+ this . createTime . isEqual ( SnapshotVersion . min ( ) ) &&
266+ ( this . documentType === DocumentType . NO_DOCUMENT ||
267+ this . documentType === DocumentType . INVALID )
268+ ) {
269+ this . createTime = version ;
270+ }
246271 this . version = version ;
247272 this . documentType = DocumentType . FOUND_DOCUMENT ;
248273 this . data = value ;
@@ -340,6 +365,7 @@ export class MutableDocument implements Document {
340365 this . documentType ,
341366 this . version ,
342367 this . readTime ,
368+ this . createTime ,
343369 this . data . clone ( ) ,
344370 this . documentState
345371 ) ;
@@ -350,6 +376,7 @@ export class MutableDocument implements Document {
350376 `Document(${ this . key } , ${ this . version } , ${ JSON . stringify (
351377 this . data . value
352378 ) } , ` +
379+ `{createTime: ${ this . createTime } }), ` +
353380 `{documentType: ${ this . documentType } }), ` +
354381 `{documentState: ${ this . documentState } })`
355382 ) ;
0 commit comments