Postgres.ts overview
Postgres for effect-auth on Alchemy: a SqlClient over a Hyperdrive binding or any Postgres URL, the @thomasfosterau/effect-auth/sql stores over it (layer), and the database target AuthSchemaProvider reconciles on deploy. On Workers, @effect/sql-pg needs nodejs_compat.
Example
At runtime, over Hyperdrive
```ts skip-type-checking
import * as Postgres from "@thomasfosterau/effect-auth-alchemy-postgres";
const AppLayer = Auth.layer.pipe(Layer.provide(Postgres.Postgres.layer(env.HYPERDRIVE)));
**Example**
```ts
On deploy, by URL
```ts skip-type-checking
AuthSchemaProvider({ database: Postgres.Postgres.database(Redacted.make(process.env.DATABASE_URL)) });
---
## Exports Grouped by Category
- [ClientOptions (type alias)](#clientoptions-type-alias)
- [Hyperdrive (interface)](#hyperdrive-interface)
- [Source (type alias)](#source-type-alias)
- [clientLayer](#clientlayer)
- [database](#database)
- [layer](#layer)
- [migrate](#migrate)
- [purgeExpired](#purgeexpired)
---
# utils
## AuthStores (type alias)
Every effect-auth store `@thomasfosterau/effect-auth/sql` implements.
**Signature**
```ts
type AuthStores = Layer.Success<typeof Sql.layer>
ClientOptions (type alias)
@effect/sql-pg pool settings. The connection itself comes from the Source.
Signature
type ClientOptions = Omit<PgClient.PgPoolConfig, "url">
Hyperdrive (interface)
A Cloudflare Hyperdrive binding, typed structurally. effect-auth reads only its connection string. @cloudflare/workers-types’ Hyperdrive satisfies this type, so no Cloudflare types package is a dependency.
Signature
export interface Hyperdrive {
readonly connectionString: string
}
Source (type alias)
Where to connect: a Hyperdrive binding, or a Postgres URL.
Signature
type Source = Hyperdrive | Redacted.Redacted
clientLayer
The SqlClient for a Hyperdrive binding or a Postgres URL, as a layer. The options are @effect/sql-pg’s pool settings.
Signature
declare const clientLayer: (
source: Source,
options?: ClientOptions
) => Layer.Layer<SqlClient.SqlClient, SqlError.SqlError>
database
A Postgres database for AuthSchemaProvider to reconcile, reached by URL. The provider holds the target as it holds a D1 binding, so a destroy, which runs from persisted state, can still reach the database.
Signature
declare const database: (url: Redacted.Redacted, options?: ClientOptions) => PostgresDatabase
layer
Every effect-auth store, backed by Postgres through a Hyperdrive binding (or a URL).
Signature
declare const layer: (source: Source, options?: ClientOptions) => Layer.Layer<AuthStores, SqlError.SqlError>
migrate
Apply the effect-auth schema to a Postgres database. The DDL is @thomasfosterau/effect-auth/sql’s and every statement is IF NOT EXISTS. Inside an Alchemy program, declare AuthSchema with a database target instead.
Signature
declare const migrate: (
source: Source,
options?: ClientOptions
) => Effect.Effect<void, Auth.Errors.AuthError | SqlError.SqlError>
purgeExpired
Delete every expired row through @thomasfosterau/effect-auth/sql’s purgeExpired (which lists them). Schedule it from a cron trigger.
Signature
declare const purgeExpired: (
source: Source,
options?: ClientOptions
) => Effect.Effect<Sql.PurgeExpiredResult, Auth.Errors.AuthError | SqlError.SqlError>