r1tsuu

cached local api

by r1tsuu
payload-enchants
Databases & data
v3
^3.0.0-beta.61
MIT

Overview

Summary generated from the README

Wraps Payload's Local API in a cached layer built on Next.js unstable_cache. Cached operations are revalidated automatically with revalidateTag when documents are created, changed or deleted. You configure it through a plugin in payload.config.ts and call a cached Payload wrapper in your pages. It is experimental and targets Payload 3.0 beta.

Features

  • Caches find, findByID, findGlobal and count operations
  • Adds a findOne operation to look up a document by a field such as slug
  • Revalidates cache tags automatically on create, update and delete
  • Respects related documents within the configured depth when revalidating
  • Custom tag builders and per-operation should-cache and should-revalidate hooks
  • Optional simple strategy that revalidates all cached data on any database update

Use cases

  • Speed up Next.js pages by caching repeated Payload queries
  • Fetch pages or posts by slug with a cached findOne call
  • Keep cached content fresh without manual revalidation code

Installation

npm i @payload-enchants/cached-local-api

335

Stars

28

Forks

28

Open Issues

1 years ago

Last Updated

107

Weekly Downloads

v1.2.2

npm Version

138.0 KB

Unpacked Size

0

Dependencies

Repository stars

335+2 since Sep 7

Show data
Repository stars by day (UTC)
DateStars
Dec 6, 2026335
Dec 5, 2026335
Dec 4, 2026335
Dec 3, 2026335
Dec 2, 2026335
Dec 1, 2026335
Nov 30, 2026335
Nov 29, 2026335
Nov 28, 2026335
Nov 27, 2026335
Nov 26, 2026335
Nov 25, 2026335
Nov 24, 2026335
Nov 23, 2026335
Nov 22, 2026335
Nov 21, 2026335
Nov 20, 2026335
Nov 19, 2026336
Nov 18, 2026336
Nov 17, 2026335
Nov 16, 2026335
Nov 15, 2026335
Nov 14, 2026335
Nov 13, 2026335
Nov 12, 2026335
Nov 11, 2026335
Nov 10, 2026335
Nov 9, 2026335
Nov 8, 2026335
Nov 7, 2026335
Nov 6, 2026335
Nov 5, 2026335
Nov 4, 2026335
Nov 3, 2026335
Nov 2, 2026335
Nov 1, 2026335
Oct 31, 2026335
Oct 30, 2026335
Oct 29, 2026335
Oct 28, 2026335
Oct 27, 2026335
Oct 26, 2026335
Oct 25, 2026335
Oct 24, 2026334
Oct 23, 2026334
Oct 22, 2026334
Oct 21, 2026334
Oct 20, 2026334
Oct 19, 2026334
Oct 18, 2026334
Oct 17, 2026334
Oct 16, 2026334
Oct 15, 2026334
Oct 14, 2026334
Oct 13, 2026334
Oct 12, 2026334
Oct 11, 2026333
Oct 10, 2026333
Oct 9, 2026333
Oct 8, 2026333
Oct 7, 2026333
Oct 6, 2026333
Oct 5, 2026333
Oct 4, 2026333
Oct 3, 2026333
Oct 2, 2026333
Oct 1, 2026333
Sep 30, 2026333
Sep 29, 2026333
Sep 28, 2026333
Sep 27, 2026333
Sep 26, 2026333
Sep 25, 2026333
Sep 24, 2026333
Sep 23, 2026333
Sep 22, 2026333
Sep 21, 2026333
Sep 20, 2026333
Sep 19, 2026333
Sep 18, 2026333
Sep 17, 2026333
Sep 16, 2026333
Sep 15, 2026333
Sep 14, 2026333
Sep 13, 2026333
Sep 12, 2026333
Sep 11, 2026333
Sep 10, 2026333
Sep 9, 2026333
Sep 8, 2026333
Sep 7, 2026333

Shared by 7 plugins in r1tsuu/payload-enchants.

npm weekly downloads

107−2.1K since Sep 7

Show data
npm weekly downloads by day (UTC)
DateWeekly downloads
Dec 6, 2026107
Dec 5, 2026107
Dec 4, 202650
Dec 3, 202651
Dec 2, 202622
Dec 1, 202623
Nov 30, 202623
Nov 29, 202651
Nov 28, 202651
Nov 27, 202646
Nov 26, 202643
Nov 25, 202644
Nov 24, 202644
Nov 23, 202643
Nov 22, 202627
Nov 21, 202635
Nov 20, 202636
Nov 19, 202639
Nov 18, 202636
Nov 17, 202635
Nov 16, 202637
Nov 15, 202631
Nov 14, 202620
Nov 13, 202618
Nov 12, 202613
Nov 11, 202616
Nov 10, 202646
Nov 9, 202646
Nov 8, 202650
Nov 7, 202656
Nov 6, 202656
Nov 5, 202660
Nov 4, 202685
Nov 3, 202655
Nov 2, 202655
Nov 1, 202646
Oct 31, 202651
Oct 30, 202662
Oct 29, 202658
Oct 28, 202629
Oct 27, 202631
Oct 26, 202649
Oct 25, 202649
Oct 24, 202661
Oct 23, 202650
Oct 22, 202669
Oct 21, 202691
Oct 20, 202690
Oct 19, 202694
Oct 18, 2026108
Oct 17, 2026101
Oct 16, 2026114
Oct 15, 2026116
Oct 14, 2026144
Oct 13, 2026149
Oct 12, 2026130
Oct 11, 2026118
Oct 10, 2026102
Oct 9, 202691
Oct 8, 202670
Oct 7, 202620
Oct 6, 202616
Oct 5, 202611
Oct 4, 20268
Oct 3, 202619
Oct 2, 202624
Oct 1, 202645
Sep 30, 202651
Sep 29, 202652
Sep 28, 202654
Sep 27, 2026181
Sep 26, 2026941
Sep 25, 20261,760
Sep 24, 20262,750
Sep 23, 20263,057
Sep 22, 20263,206
Sep 21, 20263,288
Sep 20, 20268,867
Sep 19, 202610,825
Sep 18, 202610,443
Sep 17, 20269,980
Sep 16, 20269,945
Sep 15, 202611,116
Sep 14, 202611,564
Sep 13, 20266,014
Sep 12, 20263,495
Sep 11, 20263,552
Sep 10, 20263,159
Sep 9, 20263,035
Sep 8, 20261,976
Sep 7, 20262,189

Each point is npm's rolling 7-day download count.

Health Score

48/100Fair

Composite score based on GitHub activity, stars, npm downloads, publish recency, dependency count, and package size.

Badge for your README

Payload Directory health badge
Markdown
[![Payload Directory](https://payloaddirectory.dev/badge/684372049-cached-local-api.svg)](https://payloaddirectory.dev/plugins/684372049-cached-local-api)
HTML
<a href="https://payloaddirectory.dev/plugins/684372049-cached-local-api"><img src="https://payloaddirectory.dev/badge/684372049-cached-local-api.svg" alt="Payload Directory"></a>
Updated Jan 4, 2025Created Aug 29, 2023Published Jul 16, 2024

README

Cached Local API for Payload 3.0 using Next.js unstable_cache (experimental)

Description:

This package allows to cache the following operations:

  1. find
  2. findByID
  3. findOne - new operation, find one by specific field, for example slug
  4. findGlobal
  5. count

Features

  1. Revalidation of the needed data is handled automatically using revalidateTag on Create / Change / Delete.
  2. Respects revalidation of the related documents in the current depth value. In the context of a cached operation, this package replaces payload population handling.
  3. Adds cached findOne operation, to be used, for example, with the slug field.

Install

pnpm add @payload-enchants/cached-local-api

Add to the separated file (see below for the config type)

import { buildCachedPayload } from '@payload-enchants/cached-local-api';
import { revalidateTag, unstable_cache } from 'next/cache';

export const { cachedPayloadPlugin, getCachedPayload } = buildCachedPayload({
  // collections list to cache
  collections: [
    {
      findOneFields: ['slug'],
      slug: 'posts',
    },
  ],
  // Log when revalidation runs or operation cache HIT / SKIP
  loggerDebug: true,
  globals: [{ slug: 'header' }],
  revalidateTag,
  options: {},
  unstable_cache,
});

Add into your payload.config.ts:

import { cachedPayloadPlugin } from './cached-local-api';

export default buildConfig({
  // ...your config
  plugins: [cachedPayloadPlugin],
});

Example of the usage on Next Page

import configPromise from '@payload-config';
import { getPayloadHMR } from '@payloadcms/next/utilities';

import { getCachedPayload } from '../../../cached-local-api';

const Page = async () => {
  const payload = await getPayloadHMR({
    config: configPromise,
  });

  const cachedPayload = getCachedPayload(payload);

  const posts = await cachedPayload.find({ collection: 'posts' });

  const postBySlug = await cachedPayload.findOne({ collection: 'posts', value: 'home', depth: 2 });

  // In this case it's not required as `slug` field is the first item in `findOneFields` array.
  const postBySlugExplict = await cachedPayload.findOne({
    collection: 'posts',
    field: 'slug',
    value: 'home',
  });

  // by id
  if (postBySlug) {
    const postByID = await cachedPayload.findByID({
      collection: 'posts',
      id: postBySlug?.id ?? '',
    });
  }

  // count
  const { totalDocs } = await cachedPayload.count({ collection: 'posts' });

  return (
    <div>
      {posts.docs.map((each) => (
        <div key={each.id}>{each.title}</div>
      ))}
    </div>
  );
};

export default Page;

Args

export type Args = {
  /** List of collections slugs that should use cache */
  collections?: Array<{
    /** array with the fields to use in findOne (name or config, see below) */
    findOneFields?: (FindOneFieldConfig | string)[];

    slug: keyof GeneratedTypes['collections'];
  }>;

  /** List of globals slugs that should use cache */
  globals?: Array<{
    slug: keyof GeneratedTypes['globals'];
  }>;

  /** Additional options */
  options?: Options;

  /** Next.js revalidateTag */
  revalidateTag: (tag: string) => void;

  /** Next.js unstable_cache */
  unstable_cache: UnstableCache;

  /**
   * (experiment): Instead of revalidating each collection document separately
   * 'simpleCache' revalidates all cached data on Payload database update
   *  */
  useSimpleCacheStrategy?: boolean;
};

export type FindOneFieldConfig = {
  buildWhere?: (args: {
    args: FindOneArgs<any>;
    fieldName: string;
    shouldCache: boolean;
    value: unknown;
  }) => Where;
  getFieldFromDoc?: (doc: Record<string, any>) => unknown;
  name: string;
};

Options

export type Options = {
  // custom tag build
  buildTagFind?: (args: { slug: string }) => string;
  buildTagFindByID?: (args: { id: number | string; slug: string }) => string;
  buildTagFindGlobal?: (args: { slug: string }) => string;

  // If not provided - returns `true`
  shouldCacheCountOperation?: (args: CountArgs) => Promise<boolean> | boolean;
  shouldCacheFindByIDOperation?: (args: FindByIDArgs) => Promise<boolean> | boolean;
  shouldCacheFindGlobalOperation?: (args: FindGlobalArgs) => Promise<boolean> | boolean;
  shouldCacheFindOperation?: (args: FindArgs) => Promise<boolean> | boolean;

  // If not provided - returns `true`
  shouldRevalidateGlobalOnChange?: (
    args: Parameters<GlobalAfterChangeHook>[0],
  ) => Promise<boolean> | boolean;
  shouldRevalidateOnChange?: (
    args: Parameters<CollectionAfterChangeHook>[0],
  ) => Promise<boolean> | boolean;
  shouldRevalidateOnDelete?: (
    args: Parameters<CollectionAfterDeleteHook>[0],
  ) => Promise<boolean> | boolean;

  // Completely disable caching in any operation
  disableCache?: boolean;
};

Similar plugins