db.vars.commitTs
placeholder you can have the value inserted at commit time into documents or the
return value.
This can be used to to model a queue, or to implement an updatedAt field. You
could define an index on a field that captures the commit time, then use that to
iterate over new or updated documents without worrying about missing changes due
to out-of-order commits. For tips on using CommitTs for efficient iteration, see
the notes in the
Batch Worker Component README.
db.vars.commitTs is a placeholder value while the mutation executes. When the
mutation commits, the value gets resolved to the commit timestamp in
nanoseconds.
Commit timestamp vs. _creationTime
Note that the commit timestamp is not the same as the _creationTime system
field.
Mutations execute concurrently before committing, so
_creationTime does not
necessarily reflect the order in which the mutations committed. This mean you
can be reading the last committed document in a table, ordered by
_creationTime, and then later a new document could be inserted with an earlier
_creationTime. This can lead to missing documents when iterating over a table.
With an index on db.vars.commitTs, you do not need to worry about this, as the
timestamp is strictly increasing. Since inserted documents will always have a
greater commit timestamp, the tombstones will always end where the new documents
begin.
Using the commit timestamp
During function execution, new documents inserted withdb.vars.commitTs will
have the CommitTsPlaceholder. Resolved commit timestamps are Int64s (bigint in
JS). You can use the v.commitTs() validator in argument and return validators
and schema definitions. It accepts both resolved Int64 values and the commit
timestamp placeholder. Fields with the commit timestamp are otherwise like any
other field: they can be used in indexes, nested objects, arrays, and unions.