BarakChamo

payload kv

by BarakChamo
Databases & data
v3
v4
^3.94.0 || ^4.0.0-beta.12
MIT

Overview

Summary generated from the README

Adds KV-backed field types and whole collections that keep fast-changing or short-lived data in Upstash Redis, Vercel KV or Cloudflare Workers KV. Values come back in normal find calls across the Local API, REST and GraphQL with access control, where and sort support. Registered through kvPlugin and works on Postgres, SQLite and MongoDB with Payload 3.94+ or 4 beta.

Features

  • Counter, value, list, set and set-membership fields backed by KV
  • Atomic increment, toggle and push operations without database writes
  • Where and sort on indexed KV fields mixed with database conditions
  • Whole collections stored in KV with TTLs, indexes and relationships
  • Optional write-behind sync to a hidden database column for durability
  • Drivers for Upstash, Vercel KV, Cloudflare Workers KV and in-memory

Use cases

  • Track post views, likes and comment feeds without writing to the SQL database
  • Hold expiring reservations, carts or sessions as a KV collection with TTLs
  • Build leaderboards sorted by Redis-indexed counters

From the author

Redis / KV-backed fields and collections for Payload CMS (v3 and v4): counters, feeds, TTL data — queryable, access-controlled and populated alongside your SQL data

Installation

npm i payload-kv

0

Stars

0

Forks

0

Open Issues

Today

Last Updated

1.4k

Weekly Downloads

v0.1.8

npm Version

991.2 KB

Unpacked Size

0

Dependencies

GitHub stars

00 since Nov 29

Show data
GitHub stars by day (UTC)
DateStars
Dec 6, 20260
Dec 5, 20260
Dec 4, 20260
Dec 3, 20260
Dec 2, 20260
Dec 1, 20260
Nov 30, 20260
Nov 29, 20260
npm weekly downloads

Trends appear after 7 days of data. Collecting since Dec 4 — 3 daily snapshots so far.

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

Health Score

77/100Excellent

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/1479317750-root.svg)](https://payloaddirectory.dev/plugins/1479317750-root)
HTML
<a href="https://payloaddirectory.dev/plugins/1479317750-root"><img src="https://payloaddirectory.dev/badge/1479317750-root.svg" alt="Payload Directory"></a>
Updated Dec 5, 2026Created Dec 1, 2026Published Dec 5, 2026

README

payload-kv

Redis-backed counters, feeds and expiring data for Payload CMS.

Keep fast-changing and short-lived data in Upstash, Vercel KV or Cloudflare KV,
and keep using it like any other Payload data.

CI npm Payload 3 and 4 MIT license

Quick start • Docs • Examples • Drivers • Changelog • npm

// a post lives in Postgres; its views, likes and comments live in Redis
const post = await payload.findByID({ collection: 'posts', id })
// → { title, views: 1203, likes: 87, likedByMe: true, comments: [...the latest 10] }

await getKV(payload).incr({ collection: 'posts', id, field: 'views' }) // no database write

const popular = await payload.find({
	collection: 'posts',
	sort: '-views',
	where: { likes: { greater_than: 10 } },
})

Why payload-kv

Views, likes, comment feeds, presence, carts and reservations change constantly or expire. Writing every change to your database means row locks, version churn, updatedAt noise and slow writes. payload-kv keeps that data in Redis, and your Payload app doesn't notice:

  • It's still Payload data. KV fields come back in the same find, through the Local API, REST and GraphQL, with access control, depth, select, where and sort.
  • Writes are atomic and cheap. Counters, sets and lists change with one Redis command each, and never touch your database.
  • Whole collections can live in KV, with TTLs, indexes and relationships, and they still get the admin UI, hooks and access control.
  • Reads are batched. A page of 100 documents with every KV field costs one round trip.
  • It runs anywhere. Upstash, Vercel KV and Cloudflare Workers KV over fetch, with no SDK, on Node, the edge and serverless.
  • One package for Payload 3 and 4, on Postgres, SQLite and MongoDB.

Quick start

1. Install

npm install payload-kv

2. Add KV fields and the plugin

import { kvCounter, kvList, kvPlugin, kvSet, kvSetHas } from 'payload-kv'

export default buildConfig({
	collections: [
		{
			slug: 'posts',
			fields: [
				{ name: 'title', type: 'text' },
				kvCounter({ name: 'views', index: true }),
				kvSet({ name: 'likes', index: true, writeAccess: ({ req }) => Boolean(req.user) }),
				kvSetHas({ name: 'likedByMe', set: 'likes' }),
				kvList({ name: 'comments', fields: [{ name: 'body', type: 'textarea' }] }),
			],
		},
	],
	plugins: [kvPlugin()],
})

3. Connect a KV store

Create a Redis database on Upstash (or add Upstash for Redis from the Vercel Marketplace) and set its REST credentials:

UPSTASH_REDIS_REST_URL=https://...upstash.io
UPSTASH_REDIS_REST_TOKEN=...

kvPlugin() picks them up, as well as Vercel's KV_REST_API_URL / KV_REST_API_TOKEN. In development and tests it runs in memory without them. For Cloudflare Workers KV or a custom setup, pass a driver.

4. Use it

import { getKV } from 'payload-kv'

const kv = getKV(payload)
await kv.incr({ collection: 'posts', id, field: 'views' })
await kv.toggle({ collection: 'posts', id, field: 'likes', req }) // like / unlike as the current user
await kv.push({ collection: 'posts', id, field: 'comments', item: { body: 'Nice!' }, req })

Or from the browser, with access control applied: POST /api/posts/:id/kv/views/incr.

KV fields

KV fields are Payload fields whose values live in KV. They have normal types, so generated types, GraphQL and the admin UI work as usual.

FieldReads asChanges with
kvCountera numberincr, set, clear
kvValueany field type (text, date, json…), optionally with a TTLdocument saves, set, clear
kvListthe newest items of a capped feedpush, remove, paged with list
kvSeta member count, or the membersadd, remove, toggle
kvSetHaswhether the current user is in a set ("liked by me")—
kvRef, kvJoinrelationships to and from KV collectionsdocument saves / —

Fields take the usual Payload options (access, hooks, admin, and required/validate for values), and TTLs take durations like '15m'.

→ KV fields

Atomic API, REST and GraphQL

getKV(payload) changes KV fields without a document save, with one atomic Redis command per write. The same operations are available as REST endpoints (/api/{collection}/:id/kv/:field/...) and as a GraphQL kv mutation. Access follows your collection rules: users must be able to read the document, writes need update access (or the field's writeAccess), and users can only add or remove themselves in sets.

→ Atomic API, REST and GraphQL

Querying and sorting

Add index: true, and KV fields work in where and sort, mixed freely with database conditions:

payload.find({
	collection: 'posts',
	where: { category: { equals: 'news' }, likes: { greater_than: 10 } },
	sort: '-views',
})

Leaderboards page straight from the Redis index, and large result sets are answered database-first, so queries never send huge IN lists.

→ Querying and sorting

Collections stored in KV

Collections that don't belong in your database, such as reservations, sessions, presence or carts, can live entirely in KV:

{
  slug: 'reservations',
  custom: { kv: { ttl: '15m', indexes: ['status'] } },
  fields: [
    { name: 'status', type: 'select', options: ['held', 'confirmed'] },
    { name: 'post', type: 'relationship', relationTo: 'posts' },
  ],
}

It's a normal Payload collection: the admin UI, hooks, access control, validation, unique, and every API. Documents expire on their own, queries are planned against Redis indexes, and relationships work in both directions.

→ Collections stored in KV

Durability

Turn on sync: true for a field, and the plugin keeps a copy in a hidden database column, written in batches by a Payload job or a cron endpoint. If Redis loses a key, reads fall back to the copy and restore it.

→ Write-behind sync

Drivers

DriverImportBest for
Upstash Redispayload-kv/upstashEverything; the recommended default
Vercel KVpayload-kv/vercelThe same, through Vercel's Marketplace
Cloudflare Workers KVpayload-kv/cloudflareTTL values, cached data, lookups by id (no atomic counters)
Memorypayload-kv/memoryTests and local development

→ Drivers

Configuration

kvPlugin() works with no options. Everything is tunable: key prefix, endpoints, query limits, sync schedule and more.

→ Configuration · Performance

Examples

  • Last login and device limits: when each user last signed in, and at most 3 connected devices per account in a rolling hour, as streaming services do.
  • Rate limiting: sliding-window limits on sign-in, writes and custom endpoints, with RateLimit-* headers and temporary blocks that admins can see and lift.

Each example has its own README and end-to-end tests.

Compatibility

Payload3 (≥ 3.94) and 4 (beta)
DatabasesPostgres, SQLite, MongoDB
KV storesUpstash Redis, Vercel KV, Cloudflare Workers KV, in-memory
Node22, 24, 26+ (as Payload requires)

CI tests every Payload version, database and KV store on every change. payload-kv is pre-1.0, so minor versions may still change the API; the changelog lists every change.

Contributing

Issues and pull requests are welcome. See CONTRIBUTING.md for setup, and TESTING.md for the test matrix.

License

MIT

Similar plugins