Skip to Content
better-auth-nuvion

Prisma and Drizzle

Create the plugin's tables when your database is managed by Prisma or Drizzle, step by step, and keep them in step on upgrades.

Better Auth can create tables itself on Kysely-based databases (npx @better-auth/cli migrate). When Prisma or Drizzle owns your schema, it does not touch the database. Instead it generates the schema for you to add to your own, and your own migration tool applies it.

The plugin adds a parentOrganizationId column to organization and six tables: nuvionBinding, nuvionFeeRule, nuvionDestination, nuvionPayment, nuvionLeg and nuvionEvent (schema).

Prisma

Use the Prisma adapter

lib/auth.ts
import { betterAuth } from "better-auth";
import { prismaAdapter } from "better-auth/adapters/prisma";
import { organization } from "better-auth/plugins";
import { apiKey } from "@better-auth/api-key";
import { nuvion } from "@umohpyro/better-auth-nuvion";
import { prisma } from "./prisma";

export const auth = betterAuth({
  database: prismaAdapter(prisma, { provider: "postgresql" }), // or "mysql" or "sqlite"
  plugins: [
    organization(),
    apiKey(),
    nuvion({
      apiKey: process.env.NUVION_API_KEY!,
      webhookSecret: process.env.NUVION_WEBHOOK_SECRET!,
    }),
  ],
});

Generate the schema

npx @better-auth/cli generate

It writes the models for Better Auth and for every plugin in your list, including the Nuvion ones, into your Prisma schema. Read the diff before you keep it: a first install adds the six models and one optional field on organization.

Migrate and regenerate the client

npx prisma migrate dev --name nuvion
npx prisma generate

Drizzle

Use the Drizzle adapter

lib/auth.ts
import { betterAuth } from "better-auth";
import { drizzleAdapter } from "better-auth/adapters/drizzle";
import { organization } from "better-auth/plugins";
import { apiKey } from "@better-auth/api-key";
import { nuvion } from "@umohpyro/better-auth-nuvion";
import { db } from "./db";
import * as schema from "./auth-schema";

export const auth = betterAuth({
  database: drizzleAdapter(db, { provider: "pg", schema }), // or "mysql" or "sqlite"
  plugins: [
    organization(),
    apiKey(),
    nuvion({
      apiKey: process.env.NUVION_API_KEY!,
      webhookSecret: process.env.NUVION_WEBHOOK_SECRET!,
    }),
  ],
});

Generate the schema

npx @better-auth/cli generate --output ./lib/auth-schema.ts

Add the generated file to the schema your Drizzle config reads, and review the diff.

Create and apply the migration

npx drizzle-kit generate
npx drizzle-kit migrate

Upgrading the plugin

Some releases add a column or a table (Upgrading). After you update the package, run generate again, read the diff, and run your migration tool. A release with no schema change gives an empty diff.

Things to know

  • Amounts and timestamps are bigint. Prisma returns BigInt for them and Drizzle can return numbers or strings depending on the driver. The plugin coerces what it reads, so you do not need to convert them, but keep the column type as generated.
  • Event de-duplication needs the unique constraint on nuvionEvent.eventId. Keep it when you edit the generated schema.
  • Use the version of Better Auth your app runs. @better-auth/cli brings its own copy. If a diff looks wrong, check that the CLI and your app agree, or run the migration through your app's own better-auth (Upgrading).
  • Checked on Postgres. The Prisma 7 and Drizzle adapters were tested against Postgres. MySQL and SQLite with these adapters are next to verify (Status).

On this page