Databases
CapyDB Adapter
Run Entro Auth on a CapyDB cell, with row-level security and K/V storage.
CapyDB is managed Postgres with a transaction-mode pooler, scale-to-zero cells and a K/V store beside each cell. The CapyDB adapter is the Drizzle adapter on a @capydb/drizzle client, with three additions:
- Every auth query can run inside a transaction that carries a row-level-security context.
- Reads are retried when a scale-to-zero pause cut the connection.
capydbKvStoragestores sessions and rate-limit counters in CapyDB K/V.
Installation
npm install @entro314labs/entro-auth-capydb @capydb/drizzle drizzle-orm@rc postgresThe database adapter and the K/V storage are two entry points of one package, and neither needs the other. Install what the one you use needs: the three packages above for the adapter, @capydb/kv and @upstash/redis for the storage. A project that only uses CapyDB K/V beside another database installs @entro314labs/entro-auth-capydb with those two alone.
Example Usage
import { createDb } from "@capydb/drizzle";
import { capydbAdapter } from "@entro314labs/entro-auth-capydb";
import { entroAuth } from "@entro314labs/entro-auth";
import * as schema from "./auth-schema";
const db = createDb({ relations: schema.authRelations });
export const auth = entroAuth({
database: capydbAdapter(db, { schema }),
//... the rest of your config
});createDb() reads CAPYDB_DATABASE_URL or DATABASE_URL and applies the pooler-safe client defaults. The adapter accepts the Drizzle adapter's options except provider and transaction, which it sets itself.
Import entroAuth from @entro314labs/entro-auth/minimal to leave Kysely, which this adapter does not use, out of the bundle.
On Cloudflare Workers, do not create the client and the auth instance at module scope as above: a Worker may not use one request's connection in another, and the setup fails as soon as two requests overlap. Build both per request with perRequest, as the Cloudflare guide shows.
Schema generation & migration
Generate the Drizzle schema with the Entro Auth CLI, then create and apply the migration with drizzle-kit against the direct URL (DATABASE_DIRECT_URL). Migrations do not work through the pooled endpoint.
Your auth.ts may import ./auth-schema before the file exists: the CLI recognizes the import of the file it is about to write and generates it. The tables land in the public schema under the names user, session, account and verification; use modelName when your app has tables of its own by those names.
npx @entro314labs/entro-auth-cli@latest generateRow-level security
If the auth tables have row-level security enabled, tell the adapter which context its queries run under. Each operation then runs in its own transaction with that context set through set_config(..., true), so it cannot outlive the transaction on the pooled endpoint. A transaction Entro Auth opens itself is one such block.
For policies that read auth.jwt() or auth.uid() (databases converted with capydb migrate rls --mode supabase-compat), give the claims:
capydbAdapter(db, {
schema,
rls: { claims: { app_role: "system" } },
});For policies that read the app.* settings, give the context:
capydbAdapter(db, {
schema,
rls: { context: { role: "service" } },
});Both accept a function, which is called again for every operation.
The adapter does not create policies. Add policies that let this context read and write the auth tables, for example:
create policy auth_system on "session"
using (auth.jwt() ->> 'app_role' = 'system')
with check (auth.jwt() ->> 'app_role' = 'system');Call Entro Auth outside your own withAuthContext and
withSupabaseJwtClaims blocks. @capydb/drizzle refuses a block opened
inside another one, because with one pooled connection per instance the inner
block would wait forever for the connection the outer one holds.
Scale-to-zero
A connection that was open when the cell paused fails its next statement. The adapter retries reads (findOne, findMany, count) through retryOnPause. It does not retry writes: a write can be applied before the connection drops, and running it twice would apply it twice. A write that hits a pause surfaces the error to the caller.
K/V storage
capydbKvStorage is a secondary storage on CapyDB K/V. It needs @capydb/kv and @upstash/redis, and works beside any database adapter, not only the CapyDB one.
npm install @capydb/kv @upstash/redisimport { capydbKvStorage } from "@entro314labs/entro-auth-capydb/kv";
export const auth = entroAuth({
secondaryStorage: capydbKvStorage(),
//... the rest of your config
});Without arguments it creates a client from CAPYKV_REST_URL and CAPYKV_REST_TOKEN. Pass client to use your own and keyPrefix to change the entro-auth: key prefix.