Versions

When enabled, Payload will automatically scaffold a new Collection in your database to store versions of your document(s) over time, and the Admin UI will be extended with additional views that allow you to browse document versions, view diffs in order to see exactly what has changed in your documents (and when they changed), and restore documents back to prior versions easily.

Versions Comparing an old version to a newer version of a document

With Versions, you can:

  • Maintain an audit log / history of every change ever made to a document, including monitoring for what user made which change
  • Restore documents and globals to prior states in case you need to roll back changes
  • Build a true Draft Preview mode for your data
  • Manage who can see Drafts, and who can only see Published documents via Access Control
  • Enable Autosave on collections and globals to never lose your work again
  • Build a powerful publishing schedule mechanism to create documents and have them become publicly readable automatically at a future date

Options

Versions support a few different levels of functionality that each come with their own impacts to document workflow.

Versions enabled, drafts disabled

If you enable versions but keep draft mode disabled, Payload will simply create a new version of a document each time you update a document. This is great for use cases where you need to retain a history of all document updates over time, but always want to treat the newest document version as the version that is "published".

For example, a use case for "versions enabled, drafts disabled" could be on a collection of users, where you might want to keep a version history (or audit log) of all changes ever made to users - but any changes to users should always be treated as "published" and you have no need to maintain a "draft" version of a user.

Versions and drafts enabled

If you have versions and drafts enabled, you are able to control which documents are published, and which are considered draft. That lets you write access control to control who can see published documents, and who can see draft documents. It also lets you save versions (drafts) that are newer than your most recently published document, which is helpful if you want to draft changes and maybe even preview them before you publish the changes. Read more about Drafts here.

Versions, drafts, and autosave enabled

When you have versions, drafts, and autosave enabled, the Admin UI will automatically save changes that you make to a new draft version as you edit a document, which makes sure that you never lose your changes ever again. Autosave will not affect your published post at all—instead, it'll just save your changes and let you publish them whenever you or your editors are ready to do so. Read more about Autosave here.

Collection config

Configuring Versions is done by adding the versions key to your Collection configs. Set it to true to enable default Versions settings, or customize versions options by setting the property equal to an object containing the following available options:

OptionDescription
maxPerDocUse this setting to control how many versions to keep on a document by document basis. Must be an integer. Defaults to 100, use 0 to save all versions.
drafts Enable Drafts mode for this collection. To enable, set to true or pass an object with draft options.

Global config

Global versions work similarly to Collection versions but have a slightly different set of config properties supported.

OptionDescription
maxUse this setting to control how many versions to keep on a global by global basis. Must be an integer.
draftsEnable Drafts mode for this global. To enable, set to true or pass an object with draft options

Database impact

By enabling versions, a new database collection will be made to store versions for your collection or global. The collection will be named based off the slug of the collection or global and will follow this pattern (where slug is replaced with the slug of your collection or global):

1
_slug_versions

Each document in this new versions collection will store a set of meta properties about the version as well as a full copy of the document. For example, a version's data might look like this for a Collection document:

1
{
2
"_id": "61cf752c19cdf1b1af7b61f1", // a unique ID of this version
3
"parent": "61ce1354091d5b3ffc20ea6e", // the ID of the parent document
4
"autosave": false, // used to denote if this version was created via autosave
5
"version": {
6
// your document's data goes here
7
// all fields are set to not required and this property can be partially complete
8
},
9
"createdAt": "2021-12-31T21:25:00.992+00:00",
10
"updatedAt": "2021-12-31T21:25:00.992+00:00"
11
}

Global versions are stored the same as the collection version shown above, except they do not feature the parent property, as each Global receives its own versions collection. That means we know that all versions in that collection correspond to that specific global.

Version operations

Versions expose new operations for both collections and globals. They allow you to find and query versions, find a single version by ID, and publish (or restore) a version by ID. Both Collections and Globals support the same new operations. They are used primarily by the admin UI, but if you are writing custom logic in your app and would like to utilize them, they're available for you to use as well via REST, GraphQL, and Local APIs.

Collection REST endpoints:

MethodPathDescription
GET/api/{collectionSlug}/versionsFind and query paginated versions
GET/api/{collectionSlug}/versions/:idFind a specific version by ID
POST/api/{collectionSlug}/versions/:idRestore a version by ID

Collection GraphQL queries:

Query NameOperation
version{collection.label.singular}findVersionByID
versions{collection.label.plural}findVersions

And mutation:

Query NameOperation
restoreVersion{collection.label.singular}restoreVersion

Collection Local API methods:

Find

1
// Result will be a paginated set of Versions.
2
// See /docs/queries/pagination for more.
3
const result = await payload.findVersions({
4
collection: 'posts', // required
5
depth: 2,
6
page: 1,
7
limit: 10,
8
where: {}, // pass a `where` query here
9
sort: '-createdAt',
10
locale: 'en',
11
fallbackLocale: false,
12
user: dummyUser,
13
overrideAccess: false,
14
showHiddenFields: true,
15
})

Find by ID

1
// Result will be a Post document.
2
const result = await payload.findVersionByID({
3
collection: 'posts', // required
4
id: '507f1f77bcf86cd799439013', // required
5
depth: 2,
6
locale: 'en',
7
fallbackLocale: false,
8
user: dummyUser,
9
overrideAccess: false,
10
showHiddenFields: true,
11
})

Restore

1
// Result will be the restored global document.
2
const result = await payload.restoreVersion({
3
collection: 'posts', // required
4
id: '507f1f77bcf86cd799439013', // required
5
depth: 2,
6
user: dummyUser,
7
overrideAccess: false,
8
showHiddenFields: true,
9
})

Global REST endpoints:

MethodPathDescription
GET/api/globals/{globalSlug}/versionsFind and query paginated versions
GET/api/globals/{globalSlug}/versions/:idFind a specific version by ID
POST/api/globals/{globalSlug}/versions/:idRestore a version by ID

Global GraphQL queries:

Query NameOperation
version{global.label}findVersionByID
versions{global.label}findVersions

Global GraphQL mutation:

Query NameOperation
restoreVersion{global.label}restoreVersion

Global Local API methods:

Find

1
// Result will be a paginated set of Versions.
2
// See /docs/queries/pagination for more.
3
const result = await payload.findGlobalVersions({
4
slug: 'header', // required
5
depth: 2,
6
page: 1,
7
limit: 10,
8
where: {}, // pass a `where` query here
9
sort: '-createdAt',
10
locale: 'en',
11
fallbackLocale: false,
12
user: dummyUser,
13
overrideAccess: false,
14
showHiddenFields: true,
15
})

Find by ID

1
// Result will be a Post document.
2
const result = await payload.findGlobalVersionByID({
3
slug: 'header', // required
4
id: '507f1f77bcf86cd799439013', // required
5
depth: 2,
6
locale: 'en',
7
fallbackLocale: false,
8
user: dummyUser,
9
overrideAccess: false,
10
showHiddenFields: true,
11
})

Restore

1
// Result will be the restored global document.
2
const result = await payload.restoreGlobalVersion({
3
slug: 'header', // required
4
id: '507f1f77bcf86cd799439013', // required
5
depth: 2,
6
user: dummyUser,
7
overrideAccess: false,
8
showHiddenFields: true,
9
})

Access Control

Versions expose a new access control function on both Collections and Globals that allow for you to control who can see versions of documents, and who can't.

New version access control:

FunctionAllows/Denies Access
readVersionsUsed to control who can read versions, and who can't. Will automatically restrict the Admin UI version viewing access.
Next

Drafts