Lesson
5
Setting created and modified dates
While the Content Lake stores date time values for document operations, it may be better to write your own for editorial purposes.
Log in to mark your progress for each Lesson and Task
Your existing content source likely contains dates and times for when documents were created and last modified. They may look something like this in a record:
{ "type": "post", "id": 234, "created": "2013-11-05T13:25:02Z", "modified": "2015-11-05T04:12:23Z"}
The Content Lake also stores information about when documents were created and modified.
- You can set the
_createdAt
attribute the first time you create a document, but it cannot be modified in subsequent mutations (without deleting and recreating the document). - Whenever a document changes, the Content Lake writes to the
_updatedAt
attribute, which reflects when the mutation was completed.
Both _createdAt
and _updatedAt
are considered “automatic” values and not "editorial" values; that is, these attributes can‘t be overridden by a content creator using Sanity Studio.
For example, you may want to use a “last modified” date to display when a document was last significantly updated, but the _updatedAt
field will automatically update every time a change is made.
So, if a “last modified” date is essential for editorial reasons, it is best to create a datetime
field in the document schema definition and populate it during migration.
{ "_type": "post", "_id": "post-234", "title": "Today is the greatest", "created": "2013-11-05T13:25:02Z", "modified": "2015-11-05T04:12:23Z"}
With the above data sent in a create
or createOrReplace
mutation, the _createdAt
and _updatedAt
fields will be populated in the response.
You can import and create documents with any data independently of your Sanity Studio schema, but we include the corresponding schema definition for educational purposes. Note that the _createdAt
and _updatedAt
attributes are not part of the schema definition:
postType.ts
import { defineType, defineField } from 'sanity'
export const post = defineType({ name: 'post', type: 'document', title: 'Post', fields: [ defineField({ name: 'title', type: 'string', title: 'Post title', }), definedField({ name: 'created', type: 'datetime', title: 'Post creation date', description: 'The editorial creation date for this post', }), definedField({ name: 'modified', type: 'datetime', title: 'Manual modified date', description: 'The editorial modified date for this post', }), ]})
Identify if you have editorial date/datetime fields that need to be editable by content teams in the studio.
You have 1 uncompleted task in this lesson
0 of 1