Build your application
Database and backend choices
Your account model belongs to your application. A subject can be a row in your existing customer table or an identity managed by another service. Yielded Auth needs ways to look up that identity and durably store credentials, proofs, and sessions; core does not require an ORM or a particular database.
Choose how to provide those services:
| Start with | When it fits |
|---|---|
| Managed tables | You want auth storage supplied alongside your account table. |
| Your schema | You already own SQL tables and migrations. |
| Your services | Your backend needs a custom store or transaction model. |
| In-memory tests | You need seeded password sign-in and stateful sessions without a database. |
Replacing storage Layers keeps your contract, AppAuth, and client unchanged.
Managed tablesPersistence. | Your schemaPersistence. | Your servicesLayer. | |
|---|---|---|---|
| Account table | YouYour table and IDs, mapped by column. | YouYour table and IDs, mapped by column. | YouYour records and IDs, in any store. |
| Auth tables and migrations | Yielded AuthDefined for you. Drizzle Kit generates migrations you review. | YouYour table names, columns, and migrations. | YouYour durable store; SQL is optional. |
| Queries and transactions | Yielded AuthRun for you. | Yielded AuthRun against your tables, checked at startup. | YouYour implementation of each storage service. |
| Sign-in workflows | Yielded AuthRun for you. | Yielded AuthRun for you. | Yielded AuthRun for you, on your storage. |
| Contract and client | UnchangedThe same AuthApi, AppAuth, and client at every level. Only Layers change. | ||
| Runnable app | Managed Drizzle | Custom Drizzle, Effect SQL | Custom services |
Managed tables
Persistence.managed(…)- Account table
- You
- Your table and IDs, mapped by column.
- Auth tables and migrations
- Yielded Auth
- Defined for you. Drizzle Kit generates migrations you review.
- Queries and transactions
- Yielded Auth
- Run for you.
- Sign-in workflows
- Yielded Auth
- Run for you.
Your schema
Persistence.map(…)- Account table
- You
- Your table and IDs, mapped by column.
- Auth tables and migrations
- You
- Your table names, columns, and migrations.
- Queries and transactions
- Yielded Auth
- Run against your tables, checked at startup.
- Sign-in workflows
- Yielded Auth
- Run for you.
Your services
Layer.mergeAll(…)- Account table
- You
- Your records and IDs, in any store.
- Auth tables and migrations
- You
- Your durable store; SQL is optional.
- Queries and transactions
- You
- Your implementation of each storage service.
- Sign-in workflows
- Yielded Auth
- Run for you, on your storage.
Contract and client
UnchangedThe same
AuthApi,AppAuth, and client at every level. Only Layers change.
Managed tables
Section titled “Managed tables”Map your account table and let Yielded Auth define the rest:
import { Schema as AuthSchema } from "@yielded/auth";import { AuthPersistence } from "@yielded/auth-persistence-drizzle/SqliteBun";import { Effect } from "effect";
import { AppAuth, requirement } from "./auth";import { customers } from "./customers";
export const Persistence = AuthPersistence.make(AppAuth);
export const storage = Persistence.managed({ subjects: { table: customers, id: "id", status: "enabled", activeValue: true, securityRevision: "securityRevision", idCodec: AuthSchema.SubjectId, requirements: () => Effect.succeed(requirement), }, prefix: "customer_auth",});
export const authSchema = storage.schema;Your table
customerscustomer_keysubject IDenabledactive flagauth_revisionsecurity revisiondisplay_name- Your other columns
Managed by Yielded Auth
Each references customers.customer_key as subject_id.
customer_auth_identifierscustomer_auth_passwordscustomer_auth_sessionscustomer_auth_pendingcustomer_auth_passkeyCredentials- Additional tables depend on the installed methods
Only the methods you install add tables. Export them from authSchema for Drizzle
Kit, then review and apply the generated migrations, including the non-unique
indexes for subject lookups, session inventory, and pending expiry. These indexes
trade additional storage and write work for faster lookups. Keep prefix stable: it names
physical tables. To give a table your own layout, pass it in tables; the rest
stay managed. The managed app
shows schema exports, migrations, and Layer wiring.
For Cloudflare D1, import AuthPersistence from
@yielded/auth-persistence-drizzle/D1 and provide D1Client.layer({ db: env.DB }).
See D1 composition for migrations and batch
constraints.
With PhoneOtp.make({ lifecycle: true }) and Sessions.stateful(), managed or
mapped storage also supports account signup and phone number updates. Supply
account values through Persistence.Provisioning; see the
phone lifecycle guide.
Your schema
Section titled “Your schema”Declare your tables and connect them with Persistence.map({ subjects, tables }).
TypeScript requires a table for every storage role your installed methods use.
The library supplies auth operations; you own physical names, column codecs, and
migrations. See the custom Drizzle mapping.
The same Persistence.map works without Drizzle. Import AuthPersistence from
@yielded/auth-persistence, describe the tables for Effect SQL, and write the DDL
yourself. The Effect SQL app
runs on SQLite and PostgreSQL. Keep mapped subject IDs canonical and allow the
adapter to validate the database’s columns and unique keys; see
mapping requirements.
For Workers, share WorkerdServices across requests and build auth and persistence
with one SQL client per request.
OAuth sign-in and retained provider access use the same managed or mapped composition.
Applications with custom registration, login linking, or SQL authorization predicates
can use the explicit OAuthPersistence factories.
Coordinated auth and application writes share the same Effect SQL transaction owner,
including for Kysely-owned tables.
Your services
Section titled “Your services”Each storage role is a public service, such as Password.PasswordPersistence.
Implement it in your own Layer; auth calls it through Effect without knowing
whether it uses SQL. The custom services app
uses a single-writer file store and no persistence package. It supports one
process on a local filesystem, not replicated storage.
What a custom backend must guarantee
Section titled “What a custom backend must guarantee”Storage services describe workflow commits, not just CRUD. Recheck account and credential authority and consume proofs with protected changes in the same commit. Release private credentials and lifecycle events only after the owner commits. Replay is available only where a workflow retains its original decision.
Choose a transaction or batch owner that can uphold those contracts. Independent backends do not make account and credential writes atomic. An uncertain commit requires reconciliation, not another credential issuance. See the adapter reference for supported transaction boundaries.
In-memory tests
Section titled “In-memory tests”For an Auth definition with one sign-in-only Password.make() strategy and
Sessions.stateful(), provide Testing.layer(AppAuth, { subjects, requirement })
from @yielded/auth-persistence/Testing. Each acquisition starts with your seed
subjects and discards its state when the scope closes. This storage is only for tests.
Testing.services() supplies real password hashing and WebCrypto entropy/digests.
Provide an Effect Kdf Layer with Argon2id support, such as NodeCrypto.layerKdf.
Its optional seed makes credentials reproducible; omit it for secure randomness.
Provide these services to both Auth and persistence, and supply your claims and
private credential delivery.
Both Layers use the caller’s clock, so TestClock.adjust controls expiry in Effect
tests. The consumer test
shows complete wiring. See the testing reference
for options and supported operations.
Run each level
Section titled “Run each level”The four example apps implement the
same account features with managed Drizzle, custom Drizzle, Effect SQL, and
custom services. Compare their schema.ts and live.ts files to see what changes.
The driver reference lists the maintained
SQL adapters and their constraints.