payload
payload copied to clipboard
feat: add cloudflare kv adapter
What?
Add a Cloudflare KV adapter to extend recent KV support (https://github.com/payloadcms/payload/pull/9913) Discussion: https://github.com/payloadcms/payload/discussions/14431
👋 I'm very open to comments and feedback
Why?
So that Cloudflare users can use Cloudflare KV as a backing store for Payload’s KV API.
How?
- Implement a
@payloadcms/kv-cloudflarepackage that satisfies theKVAdapterinterface - Prefix keys to avoid collisions within a shared KV namespace
- Use Cloudflare KV’s pagination API to implement
keys()andclear() - Wire the package into the monorepo build and test setup
Testing
Automated tests
- Extended the existing KV adapter integration suite in
test/kv/int.spec.tsto run againstcloudflareKVAdapteralongside the in-memory and Redis adapters, exercising:-
set,get,has,keys,deleteandclear - serialisation/deserialisation of values via Cloudflare KV
-
- Spin up a Miniflare instance in
beforeAllwith aPAYLOAD_KVnamespace and pass its binding into the adapter to mirror how Workers/Pages bindings are provided in production. - Added a dedicated Cloudflare KV Pagination test when:
- there are fewer than 1000, exactly 1000, and more than 1000 keys (1000 is the limit)
-
list_completeistrueandcursoris omitted by Cloudflare - multiple key prefixes share the same KV namespace (ensuring the adapter only returns / clears its own keys)
Manual integration test
- Created this repo: https://github.com/connorads/payload-cloudflare-kv-test
- Published the plugin to npm: https://www.npmjs.com/package/@connorads/kv-cloudflare
- Installed it into a sample Payload app and configured it to use a Cloudflare KV binding
- Verified basic CRUD behaviour via the simple TODO app