chore: removes ability to retain deleted versions

This commit is contained in:
James
2023-01-31 21:16:40 -05:00
parent 44d55e39f3
commit 6ae6b9268f
9 changed files with 77 additions and 88 deletions

View File

@@ -219,16 +219,14 @@ const ensureAtLeastOneVersion = async () => {
}) })
); );
if (!versions?.retainDeleted) { const versionsWithoutParentDocs = await VersionsModel.deleteMany({
const versionsWithoutParentDocs = await VersionsModel.deleteMany({ parent: { $nin: existingDocIds },
parent: { $nin: existingDocIds }, });
});
if (versionsWithoutParentDocs.deletedCount > 0) { if (versionsWithoutParentDocs.deletedCount > 0) {
console.log( console.log(
`Removing ${versionsWithoutParentDocs.deletedCount} versions for ${slug} collection - parent documents no longer exist` `Removing ${versionsWithoutParentDocs.deletedCount} versions for ${slug} collection - parent documents no longer exist`
); );
}
} }
} }
}) })

View File

@@ -7,13 +7,14 @@ keywords: version history, revisions, audit log, draft, publish, restore, autosa
--- ---
<Banner> <Banner>
Payload's powerful Versions functionality allows you to keep a running history of changes over time and extensible to fit any content publishing workflow. Payload's powerful Versions functionality allows you to keep a running history
of changes over time and extensible to fit any content publishing workflow.
</Banner> </Banner>
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. 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](/images/docs/versions.png) ![Versions](/images/docs/versions.png)
*Comparing an old version to a newer version of a document* _Comparing an old version to a newer version of a document_
**With Versions, you can:** **With Versions, you can:**
@@ -25,7 +26,9 @@ When enabled, Payload will automatically scaffold a new Collection in your datab
- Build a powerful publishing schedule mechanism to create documents and have them become publicly readable automatically at a future date - Build a powerful publishing schedule mechanism to create documents and have them become publicly readable automatically at a future date
<Banner type="success"> <Banner type="success">
Versions are extremely performant and totally opt-in. They don't change the shape of your data at all. All versions are stored in a separate Collection and can be turned on and off easily at your discretion. Versions are extremely performant and totally opt-in. They don't change the
shape of your data at all. All versions are stored in a separate Collection
and can be turned on and off easily at your discretion.
</Banner> </Banner>
### Options ### Options
@@ -50,22 +53,19 @@ When you have versions, drafts, _and_ `autosave` enabled, the Admin UI will auto
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: 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:
| Option | Description | | Option | Description |
| ---------------------------- | -------------| | ----------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| `maxPerDoc` | Use this setting to control how many versions to keep on a document by document basis. Must be an integer. | | `maxPerDoc` | Use this setting to control how many versions to keep on a document by document basis. Must be an integer. |
| `retainDeleted` | Boolean to determine if, when a document is deleted, you'd like to retain versions of that document, or go through and automatically delete all versions that are associated with the deleted document. | | `drafts ` | Enable [Drafts](/docs/versions/drafts) mode for this collection. To enable, set to `true` or pass an object with `draft` [options](/docs/versions/drafts#options). |
| `drafts ` | Enable [Drafts](/docs/versions/drafts) mode for this collection. To enable, set to `true` or pass an object with `draft` [options](/docs/versions/drafts#options). |
### Global config ### Global config
Global versions work similarly to Collection versions but have a slightly different set of config properties supported. Global versions work similarly to Collection versions but have a slightly different set of config properties supported.
| Option | Description | | Option | Description |
| --------- | -------------| | -------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `max` | Use this setting to control how many versions to keep on a global by global basis. Must be an integer. | | `max` | Use this setting to control how many versions to keep on a global by global basis. Must be an integer. |
| `drafts` | Enable [Drafts](/docs/versions/drafts) mode for this global. To enable, set to `true` or pass an object with `draft` [options](/docs/versions/drafts#options) | | `drafts` | Enable [Drafts](/docs/versions/drafts) mode for this global. To enable, set to `true` or pass an object with `draft` [options](/docs/versions/drafts#options) |
#### Database impact #### Database impact
@@ -99,24 +99,24 @@ Versions expose new operations for both collections and globals. They allow you
**Collection REST endpoints:** **Collection REST endpoints:**
| Method | Path | Description | | Method | Path | Description |
| -------- | ------------------------------------ | -------------------------------------- | | ------ | ------------------------------------ | --------------------------------- |
| `GET` | `/api/{collectionSlug}/versions` | Find and query paginated versions | | `GET` | `/api/{collectionSlug}/versions` | Find and query paginated versions |
| `GET` | `/api/{collectionSlug}/versions/:id` | Find a specific version by ID | | `GET` | `/api/{collectionSlug}/versions/:id` | Find a specific version by ID |
| `POST` | `/api/{collectionSlug}/versions/:id` | Restore a version by ID | | `POST` | `/api/{collectionSlug}/versions/:id` | Restore a version by ID |
**Collection GraphQL queries:** **Collection GraphQL queries:**
| Query Name | Operation | | Query Name | Operation |
| ---------------------- | -------------| | ---------------------------------------- | ----------------- |
| **`version{collection.label.singular}`** | `findVersionByID` | | **`version{collection.label.singular}`** | `findVersionByID` |
| **`versions{collection.label.plural}`** | `findVersions` | | **`versions{collection.label.plural}`** | `findVersions` |
**And mutation:** **And mutation:**
| Query Name | Operation | | Query Name | Operation |
| ---------------------- | -------------| | ----------------------------------------------- | ---------------- |
| **`restoreVersion{collection.label.singular}`** | `restoreVersion` | | **`restoreVersion{collection.label.singular}`** | `restoreVersion` |
**Collection Local API methods:** **Collection Local API methods:**
@@ -126,18 +126,18 @@ Versions expose new operations for both collections and globals. They allow you
// Result will be a paginated set of Versions. // Result will be a paginated set of Versions.
// See /docs/queries/pagination for more. // See /docs/queries/pagination for more.
const result = await payload.findVersions({ const result = await payload.findVersions({
collection: 'posts', // required collection: "posts", // required
depth: 2, depth: 2,
page: 1, page: 1,
limit: 10, limit: 10,
where: {}, // pass a `where` query here where: {}, // pass a `where` query here
sort: '-createdAt', sort: "-createdAt",
locale: 'en', locale: "en",
fallbackLocale: false, fallbackLocale: false,
user: dummyUser, user: dummyUser,
overrideAccess: false, overrideAccess: false,
showHiddenFields: true, showHiddenFields: true,
}) });
``` ```
#### Find by ID #### Find by ID
@@ -145,15 +145,15 @@ const result = await payload.findVersions({
```js ```js
// Result will be a Post document. // Result will be a Post document.
const result = await payload.findVersionByID({ const result = await payload.findVersionByID({
collection: 'posts', // required collection: "posts", // required
id: '507f1f77bcf86cd799439013', // required id: "507f1f77bcf86cd799439013", // required
depth: 2, depth: 2,
locale: 'en', locale: "en",
fallbackLocale: false, fallbackLocale: false,
user: dummyUser, user: dummyUser,
overrideAccess: false, overrideAccess: false,
showHiddenFields: true, showHiddenFields: true,
}) });
``` ```
#### Restore #### Restore
@@ -161,35 +161,35 @@ const result = await payload.findVersionByID({
```js ```js
// Result will be the restored global document. // Result will be the restored global document.
const result = await payload.restoreVersion({ const result = await payload.restoreVersion({
collection: 'posts', // required collection: "posts", // required
id: '507f1f77bcf86cd799439013', // required id: "507f1f77bcf86cd799439013", // required
depth: 2, depth: 2,
user: dummyUser, user: dummyUser,
overrideAccess: false, overrideAccess: false,
showHiddenFields: true, showHiddenFields: true,
}) });
``` ```
**Global REST endpoints:** **Global REST endpoints:**
| Method | Path | Description | | Method | Path | Description |
| -------- | ---------------------------------------- | -------------------------------------- | | ------ | ---------------------------------------- | --------------------------------- |
| `GET` | `/api/globals/{globalSlug}/versions` | Find and query paginated versions | | `GET` | `/api/globals/{globalSlug}/versions` | Find and query paginated versions |
| `GET` | `/api/globals/{globalSlug}/versions/:id` | Find a specific version by ID | | `GET` | `/api/globals/{globalSlug}/versions/:id` | Find a specific version by ID |
| `POST` | `/api/globals/{globalSlug}/versions/:id` | Restore a version by ID | | `POST` | `/api/globals/{globalSlug}/versions/:id` | Restore a version by ID |
**Global GraphQL queries:** **Global GraphQL queries:**
| Query Name | Operation | | Query Name | Operation |
| ---------------------- | -------------| | ---------------------------- | ----------------- |
| **`version{global.label}`** | `findVersionByID` | | **`version{global.label}`** | `findVersionByID` |
| **`versions{global.label}`** | `findVersions` | | **`versions{global.label}`** | `findVersions` |
**Global GraphQL mutation:** **Global GraphQL mutation:**
| Query Name | Operation | | Query Name | Operation |
| ---------------------- | -------------| | ---------------------------------- | ---------------- |
| **`restoreVersion{global.label}`** | `restoreVersion` | | **`restoreVersion{global.label}`** | `restoreVersion` |
**Global Local API methods:** **Global Local API methods:**
@@ -199,18 +199,18 @@ const result = await payload.restoreVersion({
// Result will be a paginated set of Versions. // Result will be a paginated set of Versions.
// See /docs/queries/pagination for more. // See /docs/queries/pagination for more.
const result = await payload.findGlobalVersions({ const result = await payload.findGlobalVersions({
slug: 'header', // required slug: "header", // required
depth: 2, depth: 2,
page: 1, page: 1,
limit: 10, limit: 10,
where: {}, // pass a `where` query here where: {}, // pass a `where` query here
sort: '-createdAt', sort: "-createdAt",
locale: 'en', locale: "en",
fallbackLocale: false, fallbackLocale: false,
user: dummyUser, user: dummyUser,
overrideAccess: false, overrideAccess: false,
showHiddenFields: true, showHiddenFields: true,
}) });
``` ```
#### Find by ID #### Find by ID
@@ -218,15 +218,15 @@ const result = await payload.findGlobalVersions({
```js ```js
// Result will be a Post document. // Result will be a Post document.
const result = await payload.findGlobalVersionByID({ const result = await payload.findGlobalVersionByID({
slug: 'header', // required slug: "header", // required
id: '507f1f77bcf86cd799439013', // required id: "507f1f77bcf86cd799439013", // required
depth: 2, depth: 2,
locale: 'en', locale: "en",
fallbackLocale: false, fallbackLocale: false,
user: dummyUser, user: dummyUser,
overrideAccess: false, overrideAccess: false,
showHiddenFields: true, showHiddenFields: true,
}) });
``` ```
#### Restore #### Restore
@@ -234,13 +234,13 @@ const result = await payload.findGlobalVersionByID({
```js ```js
// Result will be the restored global document. // Result will be the restored global document.
const result = await payload.restoreGlobalVersion({ const result = await payload.restoreGlobalVersion({
slug: 'header', // required slug: "header", // required
id: '507f1f77bcf86cd799439013', // required id: "507f1f77bcf86cd799439013", // required
depth: 2, depth: 2,
user: dummyUser, user: dummyUser,
overrideAccess: false, overrideAccess: false,
showHiddenFields: true, showHiddenFields: true,
}) });
``` ```
### Access Control ### Access Control
@@ -249,6 +249,6 @@ Versions expose a new access control function on both Collections and Globals th
**New version access control:** **New version access control:**
| Function | Allows/Denies Access | | Function | Allows/Denies Access |
| ------------------------ | -------------------- | | ------------------ | ---------------------------------------------------------------------------------------------------------------------- |
| **`readVersions`** | Used to control who can read versions, and who can't. Will automatically restrict the Admin UI version viewing access. | | **`readVersions`** | Used to control who can read versions, and who can't. Will automatically restrict the Admin UI version viewing access. |

View File

@@ -126,7 +126,6 @@ const collectionSchema = joi.object().keys({
versions: joi.alternatives().try( versions: joi.alternatives().try(
joi.object({ joi.object({
maxPerDoc: joi.number(), maxPerDoc: joi.number(),
retainDeleted: joi.boolean(),
drafts: joi.alternatives().try( drafts: joi.alternatives().try(
joi.object({ joi.object({
autosave: joi.alternatives().try( autosave: joi.alternatives().try(

View File

@@ -173,13 +173,11 @@ async function deleteOperation<TSlug extends keyof GeneratedTypes['collections']
// Delete versions // Delete versions
// ///////////////////////////////////// // /////////////////////////////////////
if (!collectionConfig.versions.retainDeleted) { deleteCollectionVersions({
deleteCollectionVersions({ payload,
payload, id,
id, slug: collectionConfig.slug,
slug: collectionConfig.slug, });
});
}
// ///////////////////////////////////// // /////////////////////////////////////
// afterDelete - Collection // afterDelete - Collection

View File

@@ -6,7 +6,6 @@ export const versionCollectionDefaults: IncomingCollectionVersions = {
interval: 2000, // in milliseconds interval: 2000, // in milliseconds
}, },
}, },
retainDeleted: false,
}; };
export const versionGlobalDefaults: IncomingGlobalVersions = { export const versionGlobalDefaults: IncomingGlobalVersions = {

View File

@@ -12,22 +12,20 @@ export type SanitizedDrafts = {
export type IncomingCollectionVersions = { export type IncomingCollectionVersions = {
maxPerDoc?: number maxPerDoc?: number
retainDeleted?: boolean
drafts?: boolean | IncomingDrafts drafts?: boolean | IncomingDrafts
} }
export interface SanitizedCollectionVersions extends Omit<IncomingCollectionVersions, 'drafts'> { export interface SanitizedCollectionVersions extends Omit<IncomingCollectionVersions, 'drafts'> {
maxPerDoc?: number maxPerDoc?: number
retainDeleted: boolean
drafts: SanitizedDrafts | false drafts: SanitizedDrafts | false
} }
export type IncomingGlobalVersions= { export type IncomingGlobalVersions = {
max?: number max?: number
drafts?: boolean | IncomingDrafts drafts?: boolean | IncomingDrafts
} }
export type SanitizedGlobalVersions= { export type SanitizedGlobalVersions = {
max: number max: number
drafts: SanitizedDrafts | false drafts: SanitizedDrafts | false
} }

View File

@@ -13,7 +13,6 @@ const AutosavePosts: CollectionConfig = {
}, },
versions: { versions: {
maxPerDoc: 35, maxPerDoc: 35,
retainDeleted: false,
drafts: { drafts: {
autosave: { autosave: {
interval: 2000, interval: 2000,

View File

@@ -9,7 +9,6 @@ const DraftPosts: CollectionConfig = {
}, },
versions: { versions: {
maxPerDoc: 35, maxPerDoc: 35,
retainDeleted: false,
drafts: true, drafts: true,
}, },
access: { access: {

View File

@@ -10,7 +10,6 @@ const VersionPosts: CollectionConfig = {
versions: { versions: {
drafts: false, drafts: false,
maxPerDoc: 35, maxPerDoc: 35,
retainDeleted: false,
}, },
access: { access: {
read: ({ req: { user } }) => { read: ({ req: { user } }) => {