Using temporality
Overview
Fauna documents are immutable, which means that once a document is created it never changes. If you update a document Fauna creates a new version of the document reflecting the changes and the original is left intact. With Fauna’s temporality features, it is possible to see the change history of a document, retrieve older versions, and perform queries based on document timestamps.
The history_days
attribute
The history_days
field of the
collection determines how
long Fauna retains old versions of updated documents. history_days
defaults to 30, but it is possible to specify a different value when you
create a collection:
{
ref: Collection("fruit"),
ts: 1659482501550000,
history_days: 10,
name: 'fruit'
}
You can also update the history_days
attribute of an existing
collection:
{
ref: Collection("fruit"),
ts: 1659482502120000,
history_days: 5,
name: 'fruit'
}
After the period of time specified by history_days
expires, Fauna
removes any version of a document which has aged out. The most recent
version of a document is not affected.
Documents might also include a ttl
(time-to-live) field. Documents
that have aged out of the database due to a ttl
expiration cannot be
retrieved with temporal queries. See Create
for more information
about the ttl
attribute.
Document removal is handled by a background task, so once a
document "expires" due to the history_days setting on a
collection or the ttl field on a document, it could be some time
(hours or days) before the removal occurs. There is no guarantee
that removal actually occurs.
|
Document history
You can see a document’s change history with the Events
function.
To illustrate this feature, let’s create a new document in the fruit
collection:
{
ref: Ref(Collection("fruit"), "1"),
ts: 1659482501840000,
data: { type: 'apple', colors: [ 'red', 'green' ], quantity: 15 }
}
Now let’s update the document:
{
ref: Ref(Collection("fruit"), "1"),
ts: 1659482502400000,
data: { type: 'apple', colors: [ 'red', 'green' ], quantity: 50 }
}
Now you can use Events
to see the change history. The following
example uses Events
with the Paginate
function to list all the
document’s change events.
{
data: [
{
ts: 1659482501840000,
action: 'create',
document: Ref(Collection("fruit"), "1"),
data: { type: 'apple', colors: [ 'red', 'green' ], quantity: 15 }
},
{
ts: 1659482502400000,
action: 'update',
document: Ref(Collection("fruit"), "1"),
data: { quantity: 50 }
}
]
}
You can retrieve a specific version of a document by including its
timestamp with the Get
function:
{
ref: Ref(Collection("fruit"), "1"),
ts: 1659482760050000,
data: { type: 'apple', colors: [ 'red', 'green' ], quantity: 15 }
}
Replace the string <$TIMESTAMP>
with the value for ts
from the
document creation result.
Timestamp queries
You can use the At
function to retrieve data from a specified
point in time. To see this feature in action, let’s add a few more
documents to our collection.
[
{
ref: Ref(Collection("fruit"), "2"),
ts: 1659482827030000,
data: { type: 'mango', colors: [ 'green', 'yellow' ], quantity: 20 }
},
{
ref: Ref(Collection("fruit"), "3"),
ts: 1659482827030000,
data: { type: 'melon', colors: [ 'green' ], quantity: 100 }
},
{
ref: Ref(Collection("fruit"), "4"),
ts: 1659482827030000,
data: { type: 'pear', colors: [ 'yellow', 'brown' ], quantity: 60 }
}
]
These new documents all have the same timestamp, because they were all added as part of the same transaction.
Now that you have several documents in our collection, create an index to retrieve them all at once:
{
data: [
{
ref: Ref(Collection("fruit"), "1"),
ts: 1659482826190000,
data: { type: 'apple', colors: [ 'red', 'green' ], quantity: 50 }
},
{
ref: Ref(Collection("fruit"), "2"),
ts: 1659482827030000,
data: { type: 'mango', colors: [ 'green', 'yellow' ], quantity: 20 }
},
{
ref: Ref(Collection("fruit"), "3"),
ts: 1659482827030000,
data: { type: 'melon', colors: [ 'green' ], quantity: 100 }
},
{
ref: Ref(Collection("fruit"), "4"),
ts: 1659482827030000,
data: { type: 'pear', colors: [ 'yellow', 'brown' ], quantity: 60 }
}
]
}
To query a database at a particular point in time, you can use the
At
function. At
adds a condition to a query which matches only
documents which were created at or before a specified timestamp.
{
data: [
{
ref: Ref(Collection("fruit"), "1"),
ts: 1659482825620000,
data: { type: 'apple', colors: [ 'red', 'green' ], quantity: 15 }
}
]
}
Replace the string <$TIMESTAMP>
with the value for ts
from the
document creation result.
Any documents which have been removed from the database due to
either history_days or ttl expiration cannot be retrieved with
temporal queries.
|
Conclusion
Temporality offers developers a rich set of features for working with versioned documents and querying collections based on document timestamps. See the following resources for more information about temporality:
-
Temporality (blog post)
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!