Temporality and indexes
Overview
Fauna documents are immutable, meaning that once created they are
fixed and unchangeable. When a document is updated, a new version is
created with a new timestamp and updated data. Old versions of documents
are retained in accordance with the history_days
field of the
collection they belong to. Individual documents and collections may
also have a ttl
(time to live) field. When a ttl
period expires, all
affected documents are deleted along with their document histories as if
they never existed, and they cannot be retrieved with temporal queries.
Every Fauna document includes a ts
field to hold its timestamp.
You can search an index for documents whose timestamp is at or before a
specified time with the At
function, and you can create an index
to make a collection searchable by the ts
field.
Example
For demonstration purposes, let’s create a collection named temporal
:
The C# version of this example is not currently available.
The Go version of this example is not currently available.
The Java version of this example is not currently available.
{
ref: Collection("temporal"),
ts: 1649181192120000,
history_days: 30,
name: 'temporal'
}
The Python version of this example is not currently available.
The Shell version of this example is not currently available.
Next, add a document:
The C# version of this example is not currently available.
The Go version of this example is not currently available.
The Java version of this example is not currently available.
{
ref: Ref(Collection("temporal"), "1"),
ts: 1648840223890000,
data: { test: 'temporality' }
}
The Python version of this example is not currently available.
The Shell version of this example is not currently available.
Next, create an index which includes the ts
field in its
values:
The C# version of this example is not currently available.
The Go version of this example is not currently available.
The Java version of this example is not currently available.
{
ref: Index("temporal"),
ts: 1649201845600000,
active: true,
serialized: true,
name: 'temporal',
source: Collection("temporal"),
values: [
{ field: [ 'data', 'test' ] },
{ field: [ 'ts' ] },
{ field: [ 'ref' ] }
],
partitions: 8
}
The Python version of this example is not currently available.
The Shell version of this example is not currently available.
You can use the Paginate
function to read out the contents of the
temporal
index:
The C# version of this example is not currently available.
The Go version of this example is not currently available.
The Java version of this example is not currently available.
{
data: [
{
test: 'temporality',
ts: 1649201894950000,
ref: Ref(Collection("temporal"), "1")
}
]
}
The Python version of this example is not currently available.
The Shell version of this example is not currently available.
In this case, we’ve used a custom result object to label each field in
the index’s values
definition.
Now let’s insert an event into the document’s history with a specific
timestamp. The following example uses the Epoch
function to
construct a timestamp 1000 seconds after the Unix epoch date
(1970-01-01T00:00:00Z) and the Insert
function to add a create
event to the document’s history.
The C# version of this example is not currently available.
The Go version of this example is not currently available.
The Java version of this example is not currently available.
{
ts: 1000000000,
action: 'create',
document: Ref(Collection("temporal"), "1"),
data: { test: 'time travel' }
}
The Python version of this example is not currently available.
The Shell version of this example is not currently available.
If you read out the contents of the index again you can see that the
original version of the document still exists, with its original
timestamp. The Match
function, which reads documents from the
temporal
index, retrieves the most recent version of the document; when
we added a create
event to the document we gave it an earlier
timestamp, so its the original version we see now:
The C# version of this example is not currently available.
The Go version of this example is not currently available.
The Java version of this example is not currently available.
{
data: [
{
test: 'temporality',
ts: 1649201894950000,
ref: Ref(Collection("temporal"), "1")
}
]
}
The Python version of this example is not currently available.
The Shell version of this example is not currently available.
You can use the At
function to retrieve the document with the
earlier timestamp. The following example looks for documents created at
or before one day after the Unix epoch date plus 1,000 seconds:
The C# version of this example is not currently available.
The Go version of this example is not currently available.
The Java version of this example is not currently available.
{
data: [ [ 'time travel', 1000000000, Ref(Collection("temporal"), "1") ] ]
}
The Python version of this example is not currently available.
The Shell version of this example is not currently available.
The document’s event history is a record of all events in the life of
the document. The following example updates our document, adding an
update
event to the document’s history:
The C# version of this example is not currently available.
The Go version of this example is not currently available.
The Java version of this example is not currently available.
{
ref: Ref(Collection("temporal"), "1"),
ts: 1649181444450000,
data: { test: 'current time' }
}
The Python version of this example is not currently available.
The Shell version of this example is not currently available.
The document now has three events in its history. The following example
uses the Events
function to display them in order by timestamp:
The C# version of this example is not currently available.
The Go version of this example is not currently available.
The Java version of this example is not currently available.
{
data: [
{
ts: 1000000000,
action: 'create',
document: Ref(Collection("temporal"), "1"),
data: { test: 'time travel' }
},
{
ts: 1649181443630000,
action: 'update',
document: Ref(Collection("temporal"), "1"),
data: { test: 'temporality' }
},
{
ts: 1649181444450000,
action: 'update',
document: Ref(Collection("temporal"), "1"),
data: { test: 'current time' }
}
]
}
The Python version of this example is not currently available.
The Shell version of this example is not currently available.
Is this article helpful?
Tell Fauna how the article can be improved:
Visit Fauna's forums
or email docs@fauna.com
Thank you for your feedback!