From f7ac9ff52a8292605aadc8b7b24b64da5cd787bc Mon Sep 17 00:00:00 2001 From: Kendell Joseph Date: Fri, 6 Sep 2024 14:54:12 -0400 Subject: [PATCH] chore: adds docs for collection operations --- docs/configuration/collections.mdx | 3 +- docs/database/overview.mdx | 103 ++++++++++++++++++++++++++++- 2 files changed, 104 insertions(+), 2 deletions(-) diff --git a/docs/configuration/collections.mdx b/docs/configuration/collections.mdx index 9035feb644..8ebbdbaee3 100644 --- a/docs/configuration/collections.mdx +++ b/docs/configuration/collections.mdx @@ -30,7 +30,8 @@ It's often best practice to write your Collections in separate files and then im | **`typescript`** | An object with property `interface` as the text used in schema generation. Auto-generated from slug if not defined. | | **`defaultSort`** | Pass a top-level field to sort by default in the collection List view. Prefix the name of the field with a minus symbol ("-") to sort in descending order. | | **`custom`** | Extension point for adding custom data (e.g. for plugins) | -| **`dbName`** | Custom table or collection name depending on the database adapter. Auto-generated from slug if not defined. +| **`dbName`** | Custom table or collection name depending on the database adapter. Auto-generated from slug if not defined. | +| **`db`** | Set custom database operations for this Collection. [More](/docs/database/overview#collection-operations) | _\* An asterisk denotes that a property is required._ diff --git a/docs/database/overview.mdx b/docs/database/overview.mdx index 7f20e8df65..107b09b770 100644 --- a/docs/database/overview.mdx +++ b/docs/database/overview.mdx @@ -70,4 +70,105 @@ export default buildConfig({ } }), }) -``` \ No newline at end of file +``` + +## Collection Operations + +To configure Collection database operations in your Payload application, your Collection config has methods that can override default database operations for that Collection. + +The override methods receive arguments useful for augmenting operations such as Field data, the collection slug, and the req. + +Here is an example: +```ts +import type { CollectionConfig } from 'payload/types' + +export const Collection: CollectionConfig => { + return { + slug: 'collection-db-operations', + db: { + // Create a document in a custom db + create: async ({ collection, data, req }) => { + const doc = await fetch(`https://example.com/api/${collection}/create`, { + method: "POST", + body: JSON.stringify(data), + headers: { + 'x-app-user': `payload_${req.payload.user}`, + 'Content-Type': 'application/json' + } + }).then(response => response.json()) + + return doc + }, + + // Delete a document in a custom db + deleteOne: async ({ collection, data, req }) => { + const docs = await fetch(`https://example.com/api/${collection}/delete/${data.id}`, { + method: 'DELETE', + headers: { + 'x-app-user': `payload_${req.payload.user}` + } + }).then(response => response.json()) + + return docs + }, + + // Delete many documents in a custom db + deleteMany: async ({ collection, data, req }) => { + const docs = await fetch(`https://example.com/api/${collection}/delete`, { + method: 'DELETE' + headers: { + 'x-app-user': `payload_${req.payload.user}` + } + body: JSON.stringify(data), + }).then(response => response.json()) + + return docs + }, + + // Find documents in a custom db + find: async ({ collection, data, req, where, limit }) => { + const docs = await fetch(`https://example.com/api/${collection}/find`, { + headers: { + 'x-app-user': `payload_${req.payload.user}` + } + body: JSON.stringify({data, where, limit}), + }).then(response => response.json()) + + return { docs } + }, + + // Find one document in a custom db + findOne: async ({ collection, data, req }) => { + const doc = await fetch(`https://example.com/api/${collection}/find/${data.id}`, { + headers: { + 'x-app-user': `payload_${req.payload.user}` + } + }).then(response => response.json()) + + return doc + }, + + // Update one document in an custom db + updateOne: async ({ collection, data, req }) => { + const doc = await fetch(`https://example.com/api/${collection}/update/${data.id}`, { + method: 'PUT', + body: JSON.stringify(data), + headers: { + 'x-app-user': `payload_${req.payload.user}`, + 'Content-Type': 'application/json' + } + }).then(response => response.json()) + + return { ...doc, updated: true } + }, + }, + fields: [ + { + name: 'name', + type: 'text', + }, + ], + } +} + +```