AuthSchema.ts overview
AuthSchema: an Alchemy v2 resource that reconciles @thomasfosterau/effect-auth/sql’s schemaStatements in a Postgres database. The database is given to the provider, never as a prop, because props persist as plain data and a destroy runs from them. Tables survive teardown unless dropOnDelete is set.
Example
The database goes to the provider
```ts
providers: Layer.mergeAll(AuthSchemaProvider({ database: database(url) }))
const schema = yield* AuthSchema("auth-schema", { dropOnDelete: true });
---
## Exports Grouped by Category
- [AuthSchema](#authschema)
- [AuthSchema (type alias)](#authschema-type-alias)
- [AuthSchemaAttributes (interface)](#authschemaattributes-interface)
- [AuthSchemaDatabase (type alias)](#authschemadatabase-type-alias)
- [AuthSchemaProps (interface)](#authschemaprops-interface)
- [AuthSchemaProvider](#authschemaprovider)
- [AuthSchemaProviderOptions (type alias)](#authschemaprovideroptions-type-alias)
- [DEFAULT_DATABASE](#default_database)
- [PostgresDatabase (interface)](#postgresdatabase-interface)
- [UnknownDatabaseError (class)](#unknowndatabaseerror-class)
- [authTableNames](#authtablenames)
- [postgresDatabase](#postgresdatabase)
---
# utils
## AUTH_SCHEMA_TYPE
The resource's Alchemy type tag, and the key its provider is registered under.
**Signature**
```ts
declare const AUTH_SCHEMA_TYPE: "effect-auth::PostgresAuthSchema"
AuthSchema
The declared AuthSchema resource. Yield it in an Alchemy program.
Signature
declare const AuthSchema: ResourceClass<AuthSchema>
AuthSchema (type alias)
The AuthSchema resource shape, as Alchemy models resources.
Signature
type AuthSchema = Resource<typeof AUTH_SCHEMA_TYPE, AuthSchemaProps, AuthSchemaAttributes>
AuthSchemaAttributes (interface)
Output attributes returned by the AuthSchema resource.
Signature
export interface AuthSchemaAttributes {
/** The auth tables observed in the database after reconciling, sorted. */
readonly tables: readonly string[]
/** Number of DDL statements in the applied schema. */
readonly statements: number
/** Epoch milliseconds at which the schema was last reconciled. */
readonly appliedAt: number
}
AuthSchemaDatabase (type alias)
A database AuthSchemaProvider can reconcile.
Signature
type AuthSchemaDatabase = PostgresDatabase
AuthSchemaProps (interface)
Input props for the AuthSchema resource.
Signature
export interface AuthSchemaProps {
/**
* Which of the provider's databases to reconcile — the key used in
* `AuthSchemaProvider({ databases: { … } })`. Omit it for the
* single-database form (`AuthSchemaProvider({ database })`), which
* registers under {@link DEFAULT_DATABASE}.
*
* This is a *name*, not a database: props are persisted to state and a
* destroy replays them, so they may only hold plain data.
*/
readonly database?: string
/**
* When `true`, deleting this resource drops every auth table (destroying all
* auth data). Defaults to `false` so a stack teardown leaves the data in
* place — the safe choice for a shared database.
*/
readonly dropOnDelete?: boolean
}
AuthSchemaProvider
The provider that drives the AuthSchema lifecycle, as a Layer. Provide it to your stack alongside the other providers, with the Postgres database(s) it should reconcile. A database is reached by URL, since a Hyperdrive binding only exists inside a Worker:
import * as AuthPostgres from "@thomasfosterau/effect-auth-alchemy-postgres"
Alchemy.Stack(
"app",
{
providers: Layer.mergeAll(
Cloudflare.providers(),
AuthPostgres.AuthSchema.AuthSchemaProvider({
database: AuthPostgres.Postgres.database(Redacted.make(process.env.DATABASE_URL))
})
),
state: Cloudflare.state()
},
program
)
Several databases in one stack are registered by name, and each resource picks one:
AuthSchemaProvider({ databases: { primary: primaryDb, tenants: tenantDb } })
yield * AuthSchema("tenant-auth", { database: "tenants" })
The databases are held by the provider closure, so every lifecycle operation — including the delete a real alchemy destroy replays from persisted state — can connect. They cannot travel in props, which persist as plain data.
Signature
declare const AuthSchemaProvider: (
options: AuthSchemaProviderOptions
) => Layer.Layer<Provider.Provider<AuthSchema>, never, never>
AuthSchemaProviderOptions (type alias)
The databases an AuthSchemaProvider drives: PostgresDatabases.
Either one database for the whole stack, or several keyed by the name each resource selects with AuthSchemaProps.database.
Signature
type AuthSchemaProviderOptions =
| {
/** The database every {@link AuthSchema} with no `database` prop uses. */
readonly database: AuthSchemaDatabase
}
| {
/** Databases by name, selected per resource with `database`. */
readonly databases: Readonly<Record<string, AuthSchemaDatabase>>
}
DEFAULT_DATABASE
The name a single-database AuthSchemaProvider registers its database under, and the one AuthSchemaProps.database defaults to.
Signature
declare const DEFAULT_DATABASE: "default"
PostgresDatabase (interface)
A Postgres database an AuthSchemaProvider reconciles: the SqlClient layer each lifecycle operation connects with. Build one with database(url) from this package, or with postgresDatabase over any Postgres SqlClient layer.
Signature
export interface PostgresDatabase {
readonly _tag: "PostgresDatabase"
readonly client: Layer.Layer<SqlClient.SqlClient, SqlError.SqlError>
}
UnknownDatabaseError (class)
An AuthSchema names a database its provider does not hold a database for. Raised instead of skipping the work: a silently-skipped reconcile would leave the schema unapplied, and a silently-skipped delete would break the AuthSchemaProps.dropOnDelete contract.
Signature
declare class UnknownDatabaseError
authTableNames
Every table the effect-auth schema creates (@thomasfosterau/effect-auth/sql’s list).
Signature
declare const authTableNames: ReadonlyArray<string>
postgresDatabase
A Postgres database for AuthSchemaProvider, over any layer that provides a Postgres SqlClient. The layer is built once per lifecycle operation, so it should connect rather than hold a connection.
Signature
declare const postgresDatabase: (client: Layer.Layer<SqlClient.SqlClient, SqlError.SqlError>) => PostgresDatabase