Skip to Content
Delmare Digital

payloadAdapter

The Better Auth database adapter that stores auth data in Payload collections. Options, ID and database detection, and custom model names.

payloadAdapter(config) creates a Better Auth database adapter that reads and writes Payload collections through the Local API. It is built on Better Auth's createAdapterFactory, so schema-aware transformations (field renames, ID coercion, plugin models) come from Better Auth itself.

src/payload.config.ts
import { payloadAdapter } from '@delmaredigital/payload-better-auth'

betterAuth({
  database: payloadAdapter({
    payloadClient: payload,
    adapterConfig: {
      enableDebugLogs: false,
      atomicWrites: 'auto',
    },
  }),
})

Options

Prop

Type

Detection

  • Database. A Payload database adapter whose name contains mongo or mongoose is MongoDB, sqlite is SQLite, and anything else is treated as Postgres.
  • ID type. MongoDB is always 'text'. Otherwise an explicit idType wins; then a Better Auth advanced.database.generateId other than 'serial' means 'text'; otherwise 'number'.

With number IDs, set advanced.database.generateId: 'serial' in Better Auth. Without it, Better Auth's factory doesn't coerce relationship values to numbers, and Payload rejects writes such as user: '1'. The adapter warns when this is missing.

detectDbType(payload) and resolveIdType(dbType, options, explicitIdType?) are exported if you need the same answers elsewhere.

Custom collection names

The adapter uses the standard collection names by default. Set Better Auth's modelName only to change them:

betterAuth({
  database: payloadAdapter({ payloadClient: payload }),
  user: { modelName: 'member' },          // 'users' → 'members'
  session: { modelName: 'auth_session' }, // 'sessions' → 'auth_sessions'
})

The adapter runs Better Auth's factory with usePlural: true, matching the default slugs betterAuthCollections() generates.

Atomic operations

Better Auth 1.7 requires consumeOne and incrementOne on every adapter. How this adapter implements them, and the atomicWrites policy, are covered in Atomic operations.

Edit this page on GitHub

On this page