mirror of
https://github.com/fsolla/teqo.git
synced 2026-11-14 17:43:04 +00:00
34 lines
1.1 KiB
TypeScript
34 lines
1.1 KiB
TypeScript
import type { Config } from '@/payload-types'
|
|
|
|
import configPromise from '@payload-config'
|
|
import { revalidateTag, unstable_cache } from 'next/cache'
|
|
import { getPayload } from 'payload'
|
|
|
|
type Collection = keyof Config['collections']
|
|
|
|
export const getDocuments = <Slug extends Collection>(collection: Slug, depth?: number) =>
|
|
getPayload({ config: configPromise }).then((payload) => payload.find({ collection, depth }))
|
|
|
|
export const getDocumentById = <Slug extends Collection>(
|
|
collection: Slug,
|
|
id: string,
|
|
depth?: number,
|
|
) =>
|
|
getPayload({ config: configPromise }).then((payload) =>
|
|
payload.findByID({ collection, id, depth }),
|
|
)
|
|
|
|
const getTag = <Slug extends Collection>(collection: Slug, id: string) =>
|
|
`document_${collection}:${id}`
|
|
|
|
export const getCachedDocumentById = <Slug extends Collection>(
|
|
collection: Slug,
|
|
id: string,
|
|
depth?: number,
|
|
) =>
|
|
unstable_cache(() => getDocumentById(collection, id, depth), [getTag(collection, id)], {
|
|
tags: [getTag(collection, id)],
|
|
})
|
|
|
|
export const revalidateDocumentById = <Slug extends Collection>(collection: Slug, id: string) =>
|
|
revalidateTag(getTag(collection, id))
|