npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@db4/payload

v0.1.2

Published

Payload CMS database adapter for db4

Readme

@db4/payload

npm version license TypeScript

(GitHub, npm)

Description

Payload CMS database adapter for db4. This package provides seamless integration between Payload CMS and db4 document database, enabling you to use db4 as the backend storage for your Payload-powered applications. The adapter supports all Payload features including collections, globals, versions, drafts, and authentication.

Installation

npm install @db4/payload payload

Or with pnpm:

pnpm add @db4/payload payload

Usage

import { db4Adapter } from '@db4/payload';
import { buildConfig } from 'payload/config';

export default buildConfig({
  db: db4Adapter({
    url: process.env.DB4_URL || 'https://my-app.db4.io',
    apiKey: process.env.DB4_API_KEY,
  }),
  collections: [
    {
      slug: 'posts',
      fields: [
        { name: 'title', type: 'text', required: true },
        { name: 'content', type: 'richText' },
        { name: 'author', type: 'relationship', relationTo: 'users' },
        { name: 'publishedAt', type: 'date' },
      ],
    },
    {
      slug: 'users',
      auth: true,
      fields: [
        { name: 'name', type: 'text' },
        { name: 'avatar', type: 'upload', relationTo: 'media' },
      ],
    },
  ],
});

Query Translation

import { QueryTranslator, createQueryTranslator } from '@db4/payload';

// The adapter automatically translates Payload queries to db4 format
// But you can also use the translator directly

const translator = createQueryTranslator();

// Translate a Payload where clause to db4 filter
const db4Filter = translator.translate({
  and: [
    { title: { contains: 'TypeScript' } },
    { status: { equals: 'published' } },
    {
      or: [
        { category: { equals: 'tutorial' } },
        { featured: { equals: true } },
      ],
    },
  ],
});

// Result:
// {
//   $and: [
//     { title: { $contains: 'TypeScript' } },
//     { status: { $eq: 'published' } },
//     { $or: [
//       { category: { $eq: 'tutorial' } },
//       { featured: { $eq: true } }
//     ]}
//   ]
// }

Schema Mapping

import { SchemaMapper, createSchemaMapper, generateMigration } from '@db4/payload';

// Map Payload collection to db4 schema
const mapper = createSchemaMapper();

const db4Schema = mapper.mapCollection({
  slug: 'posts',
  fields: [
    { name: 'title', type: 'text', required: true },
    { name: 'views', type: 'number', defaultValue: 0 },
    { name: 'tags', type: 'array', fields: [{ name: 'tag', type: 'text' }] },
  ],
  versions: true,
});

// Generate migration for schema changes
const migration = generateMigration(oldSchema, newSchema);
console.log('Migration operations:', migration.operations);

Versioning and Drafts

import { db4Adapter } from '@db4/payload';

export default buildConfig({
  db: db4Adapter({
    url: process.env.DB4_URL,
    versioning: {
      maxVersions: 100, // Keep last 100 versions
      drafts: true, // Enable draft support
    },
  }),
  collections: [
    {
      slug: 'pages',
      versions: {
        drafts: true,
        maxPerDoc: 25,
      },
      fields: [
        { name: 'title', type: 'text' },
        { name: 'content', type: 'richText' },
      ],
    },
  ],
});

Uploads and Media

import { db4Adapter } from '@db4/payload';

export default buildConfig({
  db: db4Adapter({
    url: process.env.DB4_URL,
    uploads: {
      storage: 'r2', // Use Cloudflare R2 for file storage
      bucket: 'my-media-bucket',
    },
  }),
  collections: [
    {
      slug: 'media',
      upload: {
        staticDir: 'media',
        imageSizes: [
          { name: 'thumbnail', width: 150, height: 150 },
          { name: 'card', width: 640, height: 480 },
        ],
      },
      fields: [
        { name: 'alt', type: 'text' },
        { name: 'caption', type: 'textarea' },
      ],
    },
  ],
});

Transaction Support

import { db4Adapter } from '@db4/payload';
import payload from 'payload';

// Transactions are automatically handled by the adapter
await payload.create({
  collection: 'orders',
  data: {
    items: [{ product: 'prod_123', quantity: 2 }],
    total: 99.99,
  },
});

// For complex operations, use explicit transactions
const result = await payload.db.transaction(async (session) => {
  const order = await payload.create({
    collection: 'orders',
    data: orderData,
    req: { transactionID: session.id },
  });

  await payload.update({
    collection: 'inventory',
    where: { product: { equals: 'prod_123' } },
    data: { $inc: { quantity: -2 } },
    req: { transactionID: session.id },
  });

  return order;
});

API

Payload CMS adapter for db4:

function db4Adapter(config: DB4AdapterConfig): PayloadDatabaseAdapter;

db4Adapter(config)

interface DB4AdapterConfig {
  url: string;
  apiKey?: string;
  versioning?: {
    maxVersions?: number;
    drafts?: boolean;
  };
  uploads?: {
    storage: 'r2' | 'do' | 'local';
    bucket?: string;
  };
  connection?: {
    timeout?: number;
    retries?: number;
  };
}

function db4Adapter(config: DB4AdapterConfig): PayloadDatabaseAdapter;

Query Translator

class QueryTranslator {
  translate(where: PayloadWhere): DB4QueryFilter;
  translateSort(sort: PayloadSort): DB4SortSpec;
  translatePagination(pagination: PayloadPagination): DB4PaginationOptions;
}

Schema Mapper

class SchemaMapper {
  mapCollection(collection: CollectionConfig): DB4CollectionSchema;
  mapField(field: PayloadField): DB4FieldSchema;
  mapIndex(index: PayloadIndex): DB4Index;
}

function generateMigration(from: Schema, to: Schema): SchemaMigration;

Types

| Type | Description | |------|-------------| | DB4AdapterConfig | Adapter configuration options | | DB4CollectionSchema | Collection schema definition | | DB4FieldSchema | Field schema definition | | TranslatedQuery | Translated query structure | | VersionDocument | Version document structure |

Related Packages

See Also

License

MIT