Skip to content

PgliteClient.ts

Connects Effect SQL to PGlite, the embedded PostgreSQL-compatible database from @electric-sql/pglite.

This module can create a managed PGlite instance or wrap an existing one and expose it as both PgliteClient and the generic Effect SQL client. The client runs PostgreSQL-style SQL, adds helpers for JSON values and LISTEN/NOTIFY messages, can dump the PGlite data directory, and can refresh PGlite array types. It also provides layers and maps common PostgreSQL-style failures into Effect SQL errors.

Since v4.0.0



Builds a PgliteClient around an existing PGlite instance, adding SQL client operations, LISTEN/NOTIFY, dump helpers, and serialized access.

Signature

declare const fromClient: (
options: PgliteClientConfig.Base & { readonly liveClient: PGliteInterface }
) => Effect.Effect<PgliteClient, SqlError, Scope.Scope | Reactivity.Reactivity>

Source

Since v4.0.0

Creates a scoped PGlite SQL client. When no live client is supplied it creates and closes a PGlite instance; when liveClient is supplied, the caller retains ownership.

Signature

declare const make: (
options?: PgliteClientConfig
) => Effect.Effect<PgliteClient, SqlError, Scope.Scope | Reactivity.Reactivity>

Source

Since v4.0.0

Creates the PGlite statement compiler, using PostgreSQL $1 placeholders, double-quoted identifiers, returning clauses, and optional JSON value transformation.

Signature

declare const makeCompiler: (transform?: (_: string) => string, transformJson?: boolean) => Statement.Compiler

Source

Since v4.0.0

PGlite-specific custom statement fragments supported by the compiler, currently JSON parameter fragments.

Signature

type PgCustom = PgJson

Source

Since v4.0.0

Creates a layer from a concrete PGlite client configuration, providing both PgliteClient and SqlClient.

Signature

declare const layer: (config?: PgliteClientConfig | undefined) => Layer.Layer<PgliteClient | Client.SqlClient, SqlError>

Source

Since v4.0.0

Creates a layer from a Config-wrapped PGlite client configuration, providing both PgliteClient and SqlClient.

Signature

declare const layerConfig: (
config: Config.Wrap<PgliteClientConfig.ConfigBase>
) => Layer.Layer<PgliteClient | Client.SqlClient, Config.ConfigError | SqlError>

Source

Since v4.0.0

Creates a layer from an effect that acquires a PgliteClient, providing both PgliteClient and SqlClient.

Signature

declare const layerFrom: <E, R>(
acquire: Effect.Effect<PgliteClient, E, R>
) => Layer.Layer<PgliteClient | Client.SqlClient, E, Exclude<R, Scope.Scope | Reactivity.Reactivity>>

Source

Since v4.0.0

PGlite-backed PostgreSQL client service, extending SqlClient with access to the PGlite instance, JSON fragments, LISTEN/NOTIFY, data directory dumps, and array type refresh.

Signature

export interface PgliteClient extends Client.SqlClient {
readonly [TypeId]: TypeId
readonly config: PgliteClientConfig
readonly pglite: PGliteInterface
readonly json: (_: unknown) => Fragment
readonly listen: (channel: string) => Stream.Stream<string, SqlError>
readonly notify: (channel: string, payload: string) => Effect.Effect<void, SqlError>
readonly dumpDataDir: (compression?: "none" | "gzip" | "auto") => Effect.Effect<File | Blob, SqlError>
readonly refreshArrayTypes: Effect.Effect<void, SqlError>
}

Source

Since v4.0.0

Configuration for a PGlite client, either by supplying PGlite creation options or an existing live PGlite client.

Signature

type PgliteClientConfig = PgliteClientConfig.Create | PgliteClientConfig.Live

Source

Since v4.0.0

Service tag for the PGlite client service.

When to use

Use to access or provide a PGlite client through the Effect context.

Signature

declare const PgliteClient: Context.Service<PgliteClient, PgliteClient>

Source

Since v4.0.0

Runtime type identifier used to mark PgliteClient values.

Signature

declare const TypeId: "~@effect/sql-pglite/PgliteClient"

Source

Since v4.0.0

Type-level identifier used to mark PgliteClient values.

Signature

type TypeId = "~@effect/sql-pglite/PgliteClient"

Source

Since v4.0.0

Namespace containing the configuration variants for PgliteClient.

Source

Since v4.0.0

Shared PGlite client options for span attributes, query/result name transformations, and JSON value transformation.

Signature

export interface Base {
readonly spanAttributes?: Record<string, unknown> | undefined
readonly transformResultNames?: ((str: string) => string) | undefined
readonly transformQueryNames?: ((str: string) => string) | undefined
readonly transformJson?: boolean | undefined
}

Source

Since v4.0.0

Configuration used to create a managed PGlite instance from PGlite constructor options.

Signature

export interface Create extends Base, PGliteOptions {}

Source

Since v4.0.0

Configuration that uses an existing PGlite client. The supplied liveClient is caller-owned and is not closed by the Effect client.

Signature

export interface Live extends Base {
readonly liveClient: PGliteInterface
}

Source

Since v4.0.0

Config-friendly subset of PGlite creation options, including data directory, username, database, relaxed durability, and shared transform options.

Signature

export interface ConfigBase extends Base {
readonly dataDir?: string | undefined
readonly username?: string | undefined
readonly database?: string | undefined
readonly relaxedDurability?: boolean | undefined
}

Source

Since v4.0.0