Provides CollectionOperations and GlobalOperations base classes that wrap Payload's Local API in a repository pattern. Types come from Payload's generated types, and the package requires payload 3.82.0 or newer. Defaults and transformers let you apply shared options or rewrite data, where clauses and options for each operation.
Opinionated repository and operations object wrapper around Payload's Local API.
Installation
npm i payload-repository0
Stars
0
Forks
0
Open Issues
5 days ago
Last Updated
229
Weekly Downloads
v2.0.0
npm Version
54.1 KB
Unpacked Size
0
Dependencies
00 since Sep 6
| Date | Stars |
|---|---|
| Dec 5, 2026 | 0 |
| Dec 4, 2026 | 0 |
| Dec 3, 2026 | 0 |
| Dec 2, 2026 | 0 |
| Dec 1, 2026 | 0 |
| Nov 30, 2026 | 0 |
| Nov 29, 2026 | 0 |
| Nov 28, 2026 | 0 |
| Nov 27, 2026 | 0 |
| Nov 26, 2026 | 0 |
| Nov 25, 2026 | 0 |
| Nov 24, 2026 | 0 |
| Nov 23, 2026 | 0 |
| Nov 22, 2026 | 0 |
| Nov 21, 2026 | 0 |
| Nov 20, 2026 | 0 |
| Nov 19, 2026 | 0 |
| Nov 18, 2026 | 0 |
| Nov 17, 2026 | 0 |
| Nov 16, 2026 | 0 |
| Nov 15, 2026 | 0 |
| Nov 14, 2026 | 0 |
| Nov 13, 2026 | 0 |
| Nov 12, 2026 | 0 |
| Nov 11, 2026 | 0 |
| Nov 10, 2026 | 0 |
| Nov 9, 2026 | 0 |
| Nov 8, 2026 | 0 |
| Nov 7, 2026 | 0 |
| Nov 6, 2026 | 0 |
| Nov 5, 2026 | 0 |
| Nov 4, 2026 | 0 |
| Nov 3, 2026 | 0 |
| Nov 2, 2026 | 0 |
| Nov 1, 2026 | 0 |
| Oct 31, 2026 | 0 |
| Oct 30, 2026 | 0 |
| Oct 29, 2026 | 0 |
| Oct 28, 2026 | 0 |
| Oct 27, 2026 | 0 |
| Oct 26, 2026 | 0 |
| Oct 25, 2026 | 0 |
| Oct 24, 2026 | 0 |
| Oct 23, 2026 | 0 |
| Oct 22, 2026 | 0 |
| Oct 21, 2026 | 0 |
| Oct 20, 2026 | 0 |
| Oct 19, 2026 | 0 |
| Oct 18, 2026 | 0 |
| Oct 17, 2026 | 0 |
| Oct 16, 2026 | 0 |
| Oct 15, 2026 | 0 |
| Oct 14, 2026 | 0 |
| Oct 13, 2026 | 0 |
| Oct 12, 2026 | 0 |
| Oct 11, 2026 | 0 |
| Oct 10, 2026 | 0 |
| Oct 9, 2026 | 0 |
| Oct 8, 2026 | 0 |
| Oct 7, 2026 | 0 |
| Oct 6, 2026 | 0 |
| Oct 5, 2026 | 0 |
| Oct 4, 2026 | 0 |
| Oct 3, 2026 | 0 |
| Oct 2, 2026 | 0 |
| Oct 1, 2026 | 0 |
| Sep 30, 2026 | 0 |
| Sep 29, 2026 | 0 |
| Sep 28, 2026 | 0 |
| Sep 27, 2026 | 0 |
| Sep 26, 2026 | 0 |
| Sep 25, 2026 | 0 |
| Sep 24, 2026 | 0 |
| Sep 23, 2026 | 0 |
| Sep 22, 2026 | 0 |
| Sep 21, 2026 | 0 |
| Sep 20, 2026 | 0 |
| Sep 19, 2026 | 0 |
| Sep 18, 2026 | 0 |
| Sep 17, 2026 | 0 |
| Sep 16, 2026 | 0 |
| Sep 15, 2026 | 0 |
| Sep 14, 2026 | 0 |
| Sep 13, 2026 | 0 |
| Sep 12, 2026 | 0 |
| Sep 11, 2026 | 0 |
| Sep 10, 2026 | 0 |
| Sep 9, 2026 | 0 |
| Sep 8, 2026 | 0 |
| Sep 7, 2026 | 0 |
| Sep 6, 2026 | 0 |
211+187 since Sep 6
| Date | Weekly downloads |
|---|---|
| Dec 5, 2026 | 211 |
| Dec 4, 2026 | 210 |
| Dec 3, 2026 | 189 |
| Dec 2, 2026 | 3 |
| Dec 1, 2026 | 14 |
| Nov 30, 2026 | 14 |
| Nov 29, 2026 | 12 |
| Nov 28, 2026 | 12 |
| Nov 27, 2026 | 12 |
| Nov 26, 2026 | 12 |
| Nov 25, 2026 | 12 |
| Nov 24, 2026 | 1 |
| Nov 23, 2026 | 1 |
| Nov 22, 2026 | 4 |
| Nov 21, 2026 | 4 |
| Nov 20, 2026 | 4 |
| Nov 19, 2026 | 5 |
| Nov 18, 2026 | 14 |
| Nov 17, 2026 | 14 |
| Nov 16, 2026 | 15 |
| Nov 15, 2026 | 12 |
| Nov 14, 2026 | 12 |
| Nov 13, 2026 | 13 |
| Nov 12, 2026 | 23 |
| Nov 11, 2026 | 14 |
| Nov 10, 2026 | 17 |
| Nov 9, 2026 | 16 |
| Nov 8, 2026 | 17 |
| Nov 7, 2026 | 17 |
| Nov 6, 2026 | 15 |
| Nov 5, 2026 | 4 |
| Nov 4, 2026 | 5 |
| Nov 3, 2026 | 4 |
| Nov 2, 2026 | 6 |
| Nov 1, 2026 | 7 |
| Oct 31, 2026 | 8 |
| Oct 30, 2026 | 8 |
| Oct 29, 2026 | 8 |
| Oct 28, 2026 | 7 |
| Oct 27, 2026 | 6 |
| Oct 26, 2026 | 4 |
| Oct 25, 2026 | 3 |
| Oct 24, 2026 | 4 |
| Oct 23, 2026 | 4 |
| Oct 22, 2026 | 6 |
| Oct 21, 2026 | 6 |
| Oct 20, 2026 | 5 |
| Oct 19, 2026 | 5 |
| Oct 18, 2026 | 4 |
| Oct 17, 2026 | 3 |
| Oct 16, 2026 | 3 |
| Oct 15, 2026 | 2 |
| Oct 14, 2026 | 2 |
| Oct 13, 2026 | 2 |
| Oct 12, 2026 | 4 |
| Oct 11, 2026 | 5 |
| Oct 10, 2026 | 4 |
| Oct 9, 2026 | 5 |
| Oct 8, 2026 | 4 |
| Oct 7, 2026 | 4 |
| Oct 6, 2026 | 4 |
| Oct 5, 2026 | 2 |
| Oct 4, 2026 | 1 |
| Oct 3, 2026 | 1 |
| Oct 2, 2026 | 5 |
| Oct 1, 2026 | 5 |
| Sep 30, 2026 | 12 |
| Sep 29, 2026 | 27 |
| Sep 28, 2026 | 35 |
| Sep 27, 2026 | 48 |
| Sep 26, 2026 | 54 |
| Sep 25, 2026 | 51 |
| Sep 24, 2026 | 58 |
| Sep 23, 2026 | 52 |
| Sep 22, 2026 | 47 |
| Sep 21, 2026 | 43 |
| Sep 20, 2026 | 31 |
| Sep 19, 2026 | 25 |
| Sep 18, 2026 | 25 |
| Sep 17, 2026 | 19 |
| Sep 16, 2026 | 20 |
| Sep 15, 2026 | 10 |
| Sep 14, 2026 | 8 |
| Sep 13, 2026 | 7 |
| Sep 12, 2026 | 7 |
| Sep 11, 2026 | 20 |
| Sep 10, 2026 | 19 |
| Sep 9, 2026 | 18 |
| Sep 8, 2026 | 23 |
| Sep 7, 2026 | 24 |
| Sep 6, 2026 | 24 |
Each point is npm's rolling 7-day download count.
Health Score
Composite score based on GitHub activity, stars, npm downloads, publish recency, dependency count, and package size.
Badge for your README
[](https://payloaddirectory.dev/plugins/1169584752-root)<a href="https://payloaddirectory.dev/plugins/1169584752-root"><img src="https://payloaddirectory.dev/badge/1169584752-root.svg" alt="Payload Directory"></a>README
Opinionated repository and operations object wrapper around Payload's Local API.
npm install payload-repository
Requires payload >= 3.82.0.
Select, data and result types come from Payload's generated types, i.e. the augmentation
declare module 'payload' { interface GeneratedTypes extends Config {} } that payload generate:types writes. Without
it there is no type safety. Pass your generated Config type as the first type argument as well.
Extend CollectionOperations to encapsulate domain-specific operations logic for a collection:
import type {BasePayload} from 'payload';
import type {Config} from '@/payload-types';
import {CollectionOperations} from 'payload-repository';
class PostsOperations extends CollectionOperations<Config, 'posts'> {
constructor(payload: BasePayload) {
super(payload, 'posts');
}
create(title: string) {
return this.repository.create({title});
}
findAll() {
return this.repository.findAll({});
}
findById(id: number) {
return this.repository.findById(id);
}
findBySlug(slug: string) {
return this.repository.findOne({slug: {equals: slug}});
}
findPublished() {
return this.repository.findAll({status: {equals: 'published'}});
}
publish(id: number) {
return this.repository.updateById(id, {status: 'published'});
}
deleteById(id: number) {
return this.repository.deleteById(id);
}
}
// Usage
const postsOperations = new PostsOperations(payload);
const post = await postsOperations.create('Hello World');
const published = await postsOperations.findPublished();
await postsOperations.publish(post.id);
find returns Payload's paginated result (10 documents by default). For the common cases there are shortcuts that run
through find or count and therefore use their transformers:
findOne(where) returns the first match or null.findAll(where) returns all matching documents as an array (pagination: false).exists(where) returns whether at least one document matches.A find options transformer sees limit: 1 (findOne) and pagination: false (findOne, findAll) and can override
limit.
findByIds sets limit to the number of ids by default, so all requested documents are returned. An explicit limit
takes precedence.
joins is typed by the join fields of the collection.
create without draft publishes the document (_status: 'published' is set for collections with drafts).
create(data, {draft: true}) saves a draft and accepts partial data.
update and updateById without draft keep the status like Payload does: an open draft stays a draft. Set
_status: 'published' in the data to publish, or pass draft: true to save as draft.
update, updateByIds, delete and deleteByIds return Payload's BulkOperationResult unchanged. As in the Local API,
per-document failures are collected in errors instead of being thrown, and the successful documents are written. Check
errors (and compare docs with the requested ids) when partial success matters:
const {docs, errors} = await this.repository.updateByIds(ids, {status: 'archived'});
if (errors.length > 0) {
// docs were updated, errors were not
}
restoreVersion(versionId) is available for collections and globals. draft is not available, because Payload's Local
API ignores it. For globals, Payload returns the new version record (typed as TypeWithVersion), not the global; call
find() afterwards if you need the restored global.
With disableErrors: true, findById, findVersionById and GlobalRepository.find return null instead of throwing,
and the return type becomes nullable. The type is nullable only for a literal true; with a boolean variable or a
disableErrors set by an options transformer it stays non-null, as with Payload's findByID.
Extend GlobalOperations to encapsulate domain-specific operations logic for a global:
import type {BasePayload} from 'payload';
import type {Config} from '@/payload-types';
import {GlobalOperations} from 'payload-repository';
class SettingsOperations extends GlobalOperations<Config, 'settings'> {
constructor(payload: BasePayload) {
super(payload, 'settings');
}
get() {
return this.repository.find();
}
setSiteTitle(title: string) {
return this.repository.update({siteTitle: title});
}
}
// Usage
const settingsOperations = new SettingsOperations(payload);
const settings = await settingsOperations.get();
await settingsOperations.setSiteTitle('My Site');
Pass defaults as the fourth argument to apply options to every operation. Allowed are req, locale,
fallbackLocale, depth, overrideAccess, user and context. Explicit options win, and transformers see the merged
result. An explicit undefined in the options keeps the default.
A typical use is passing req inside a hook, so all operations run in the same transaction:
import type {CollectionAfterChangeHook, PayloadRequest} from 'payload';
class AuthorsOperations extends CollectionOperations<Config, 'authors'> {
constructor(req: PayloadRequest) {
super(req.payload, 'authors', undefined, {req});
}
rename(id: number, name: string) {
return this.repository.updateById(id, {name});
}
}
const afterChange: CollectionAfterChangeHook = async ({doc, req}) => {
await new AuthorsOperations(req).rename(doc.author, doc.authorName);
};
Payload mutates a passed req (locale, fallbackLocale, context, user, query.depth). A req shared via
defaults therefore carries these values into later calls, just as with direct Local API use. If calls need different
locales, set locale explicitly on each call.
Transformers are optional middleware functions that intercept and modify arguments before they reach the Payload API.
They can transform data, where clauses, and options on a per-operation basis.
Pass transformers as the third argument to CollectionOperations or GlobalOperations:
import type {BasePayload, Where} from 'payload';
import type {Config} from '@/payload-types';
import {CollectionOperations} from 'payload-repository';
import type {CollectionTransformers} from 'payload-repository';
const withTenant = (where: Where, tenantId: number): Where => ({and: [where, {tenant: {equals: tenantId}}]});
const tenantTransformers = (tenantId: number): CollectionTransformers<Config, 'posts'> => ({
create: {
data: data => ({...data, tenant: tenantId}),
},
find: {
where: where => withTenant(where, tenantId),
},
count: {
where: where => withTenant(where, tenantId),
},
update: {
data: data => ({...data, tenant: tenantId}),
where: where => withTenant(where, tenantId),
},
delete: {
where: where => withTenant(where, tenantId),
},
});
class PostsOperations extends CollectionOperations<Config, 'posts'> {
constructor(payload: BasePayload, tenantId: number) {
super(payload, 'posts', tenantTransformers(tenantId));
}
create(title: string) {
return this.repository.create({title});
}
findAll() {
return this.repository.findAll({});
}
}
// Usage
const tenantPosts = new PostsOperations(payload, 42);
// tenant is automatically injected into the data
await tenantPosts.create('Hello World');
// tenant where clause is automatically applied
const posts = await tenantPosts.findAll();
Combine the caller's where with and instead of spreading it: {...where, tenant: …} silently overwrites a tenant
condition the caller set, while and keeps every condition of the caller, including or.
Transformers support async functions and are available for all repository operations:
{} when
neither options nor defaults are given.where slots exist for find, count, findDistinct, findVersions, countVersions, update and delete.
findOne, findAll and exists use the find and count transformers; findByIds, updateByIds and
deleteByIds use the find, update and delete where transformers.findVersions and countVersions, field conditions need the version. prefix; Payload's
appendVersionToQueryKey converts an existing where.where, id, data, collection, slug or field; the
repository sets them after the options. Bulk update and delete (including updateByIds and deleteByIds) drop an
id from the options, so Payload cannot switch to the single-document operation and skip where.[!WARNING]
findById,updateById,deleteById,duplicate,findVersionByIdandrestoreVersionhave nowherein Payload, so where transformers do not apply to them. Where transformers are a query convenience, not a security boundary. To enforce isolation, use access control:overrideAccess: falsewith auser(e.g. via defaults), or Payload's official multi-tenant plugin.
All public types (CollectionTransformers, FindOptions, GlobalSelectResult, RepositoryDefaults, …) are exported
from payload-repository.
When building layered repositories where each layer contributes its own transformers, use mergeTransformers to combine
them. Overlapping transformers on the same operation and argument are chained sequentially:
import type {BasePayload, TypedLocale} from 'payload';
import type {Config} from '@/payload-types';
import {CollectionOperations, mergeTransformers} from 'payload-repository';
import type {CollectionTransformers} from 'payload-repository';
const tenantTransformers = (tenantId: number): CollectionTransformers<Config, 'posts'> => ({
find: {
where: where => ({and: [where, {tenant: {equals: tenantId}}]}),
},
});
const localeTransformers = (locale: TypedLocale): CollectionTransformers<Config, 'posts'> => ({
find: {
options: options => ({...options, locale}),
},
});
class PostsOperations extends CollectionOperations<Config, 'posts'> {
constructor(payload: BasePayload, tenantId: number, locale: TypedLocale) {
super(payload, 'posts', mergeTransformers(
tenantTransformers(tenantId),
localeTransformers(locale),
));
}
findAll() {
return this.repository.findAll({});
}
}
mergeTransformers works with both CollectionTransformers and GlobalTransformers. When multiple transformer objects
define the same leaf (e.g. both define find.where), they are chained in order — the second transformer receives the
output of the first.
When several operations classes need the same query, or a query needs repository internals, extend the repository
instead of repeating it in each operations class. Override repository in the operations class with your subclass:
import type {BasePayload, CollectionSlug} from 'payload';
import type {Config} from '@/payload-types';
import {CollectionOperations, CollectionRepository} from 'payload-repository';
import type {AnyCollectionConfig} from 'payload-repository';
class SluggedRepository<TConfig extends AnyCollectionConfig, TSlug extends CollectionSlug> extends CollectionRepository<
TConfig,
TSlug
> {
findBySlug(slug: string) {
return this.findOne({slug: {equals: slug}});
}
}
class PostsOperations extends CollectionOperations<Config, 'posts'> {
protected override readonly repository: SluggedRepository<Config, 'posts'>;
constructor(payload: BasePayload) {
super(payload, 'posts');
this.repository = new SluggedRepository(payload, 'posts');
}
findBySlug(slug: string) {
return this.repository.findBySlug(slug);
}
}
Inside a repository subclass, this.payload is a Promise<BasePayload>. If you need several repositories or your own
context in one class, write your own base class instead of extending CollectionOperations.
payload-repository; the payload-repository/internal/types subpath was removed.CollectionCountVersionsResult and GlobalCountVersionsResult.OperationsTransformerMap is no longer an index signature on
CollectionTransformers and GlobalTransformers.findByIds, updateByIds and deleteByIds apply the find, update and delete where transformers.findByIds sets limit to the number of ids by default._status on create.{} instead of undefined.disableErrors: true the return type is nullable.GeneratedTypes augmentation instead of the Config type argument.where, id, data, collection, slug or field.GlobalRepository.restoreVersion resolves to TypeWithVersion<…> (the new version record).payload in repository subclasses is a Promise<BasePayload> (since 1.1.2).payload >= 3.82.0.MIT
payload operations
Typed custom RPC operations for Payload with shared Zod contracts, server endpoints and type-safe clients
77208/wk3v3
query
Typesafe query filters and field selection for Payload's REST and internal APIs
4526/wk143v1
mcp
Adds Model Context Protocol capabilities to a Payload application
98275.9k/wk46.1kv3 v4
payload oapi
Generate an OpenAPI spec from your Payload config with optional documentation UIs
7631.1k/wk125v3
payload plugin openapi
Generate an OpenAPI 3.0, 3.1 or 3.2 spec from your Payload config and serve it with Scalar or Swagger UI
701.4k/wk18v4
payload schema export
Serve a JSON description of your Payload collections, fields, relations, locales and REST routes
6010/wk0v3