@@ -101,6 +101,13 @@ export interface Document {
101
101
*/
102
102
readonly readTime : SnapshotVersion ;
103
103
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
+
104
111
/** The underlying data of this document or an empty value if no data exists. */
105
112
readonly data : ObjectValue ;
106
113
@@ -163,6 +170,7 @@ export class MutableDocument implements Document {
163
170
private documentType : DocumentType ,
164
171
public version : SnapshotVersion ,
165
172
public readTime : SnapshotVersion ,
173
+ public createTime : SnapshotVersion ,
166
174
public data : ObjectValue ,
167
175
private documentState : DocumentState
168
176
) { }
@@ -175,8 +183,9 @@ export class MutableDocument implements Document {
175
183
return new MutableDocument (
176
184
documentKey ,
177
185
DocumentType . INVALID ,
178
- SnapshotVersion . min ( ) ,
179
- SnapshotVersion . min ( ) ,
186
+ /* version */ SnapshotVersion . min ( ) ,
187
+ /* readTime */ SnapshotVersion . min ( ) ,
188
+ /* createTime */ SnapshotVersion . min ( ) ,
180
189
ObjectValue . empty ( ) ,
181
190
DocumentState . SYNCED
182
191
) ;
@@ -189,13 +198,15 @@ export class MutableDocument implements Document {
189
198
static newFoundDocument (
190
199
documentKey : DocumentKey ,
191
200
version : SnapshotVersion ,
201
+ createTime : SnapshotVersion ,
192
202
value : ObjectValue
193
203
) : MutableDocument {
194
204
return new MutableDocument (
195
205
documentKey ,
196
206
DocumentType . FOUND_DOCUMENT ,
197
- version ,
198
- SnapshotVersion . min ( ) ,
207
+ /* version */ version ,
208
+ /* readTime */ SnapshotVersion . min ( ) ,
209
+ /* createTime */ createTime ,
199
210
value ,
200
211
DocumentState . SYNCED
201
212
) ;
@@ -209,8 +220,9 @@ export class MutableDocument implements Document {
209
220
return new MutableDocument (
210
221
documentKey ,
211
222
DocumentType . NO_DOCUMENT ,
212
- version ,
213
- SnapshotVersion . min ( ) ,
223
+ /* version */ version ,
224
+ /* readTime */ SnapshotVersion . min ( ) ,
225
+ /* createTime */ SnapshotVersion . min ( ) ,
214
226
ObjectValue . empty ( ) ,
215
227
DocumentState . SYNCED
216
228
) ;
@@ -228,8 +240,9 @@ export class MutableDocument implements Document {
228
240
return new MutableDocument (
229
241
documentKey ,
230
242
DocumentType . UNKNOWN_DOCUMENT ,
231
- version ,
232
- SnapshotVersion . min ( ) ,
243
+ /* version */ version ,
244
+ /* readTime */ SnapshotVersion . min ( ) ,
245
+ /* createTime */ SnapshotVersion . min ( ) ,
233
246
ObjectValue . empty ( ) ,
234
247
DocumentState . HAS_COMMITTED_MUTATIONS
235
248
) ;
@@ -243,6 +256,18 @@ export class MutableDocument implements Document {
243
256
version : SnapshotVersion ,
244
257
value : ObjectValue
245
258
) : 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
+ }
246
271
this . version = version ;
247
272
this . documentType = DocumentType . FOUND_DOCUMENT ;
248
273
this . data = value ;
@@ -340,6 +365,7 @@ export class MutableDocument implements Document {
340
365
this . documentType ,
341
366
this . version ,
342
367
this . readTime ,
368
+ this . createTime ,
343
369
this . data . clone ( ) ,
344
370
this . documentState
345
371
) ;
@@ -350,6 +376,7 @@ export class MutableDocument implements Document {
350
376
`Document(${ this . key } , ${ this . version } , ${ JSON . stringify (
351
377
this . data . value
352
378
) } , ` +
379
+ `{createTime: ${ this . createTime } }), ` +
353
380
`{documentType: ${ this . documentType } }), ` +
354
381
`{documentState: ${ this . documentState } })`
355
382
) ;
0 commit comments