Skip to content

D1Client

Cloudflare D1 client implementation for Effect SQL, backed by a Workers D1Database binding.

This module adapts a Cloudflare D1 database binding into both the D1-specific D1Client service and the generic Effect SqlClient service. It uses the SQLite statement compiler, caches prepared statements, maps D1 failures to SqlError, and provides direct or config-backed layers. Transactions, streaming queries, and updateValues are not supported by this driver.

8 exports Added in v4.0.0 Source

Constructors

make

Added in v4.0.0 Source

Creates a scoped Cloudflare D1 SQL client. Prepared statements are cached, while transactions and streaming queries are not supported by this driver.

Signature

declare function make(options: D1ClientConfig): Effect<D1Client, never, Scope | Reactivity>;

Layers

layer

Added in v4.0.0 Source

Creates a layer from a concrete D1 client configuration, providing both D1Client and SqlClient.

Signature

declare function layer(config: D1ClientConfig): Layer<D1Client | SqlClient, ConfigError>;

layerConfig

Added in v4.0.0 Source

Creates a layer from a Config-wrapped D1 client configuration, providing both D1Client and SqlClient.

Signature

declare function layerConfig(
  config:
    | {
        readonly db: Config;
        readonly prepareCacheSize?: Config<number | undefined>;
        readonly prepareCacheTTL?: Config<Input | undefined>;
        readonly spanAttributes?:
          | {
              [key: string]: Config<unknown>;
            }
          | Config<Record<string, unknown> | undefined>;
        readonly transformQueryNames?: Config<(str: string) => string | undefined>;
        readonly transformResultNames?: Config<(str: string) => string | undefined>;
      }
    | Config<D1ClientConfig>,
): Layer<D1Client | SqlClient, ConfigError>;

Models

D1ClientConfig interface

Added in v4.0.0 Source

Configuration for a Cloudflare D1 client, including the D1Database, prepared statement cache settings, span attributes, and query/result name transforms.

Signature

interface D1ClientConfig {
  readonly db: D1Database;
  readonly prepareCacheSize?: number;
  readonly prepareCacheTTL?: Input;
  readonly spanAttributes?: Record<string, unknown>;
  readonly transformQueryNames?: (str: string) => string;
  readonly transformResultNames?: (str: string) => string;
}

Services

D1Client

Added in v4.0.0 Source

Service tag for the Cloudflare D1 SQL client.

When to use

Use to access or provide a Cloudflare D1 SQL client through the Effect context.

Signature

declare const D1Client: Service<D1Client, D1Client>;

D1Client interface

Added in v4.0.0 Source

Cloudflare D1 SQL client service, extending SqlClient with its D1 configuration and no updateValues support.

Signature

interface D1Client extends SqlClient {
  <A extends object = Row>(strings: TemplateStringsArray, ...args: Array<any>): Statement<A>;
  (value: string): Identifier;
  readonly "~@effect/sql-d1/D1Client": "~@effect/sql-d1/D1Client";
  readonly batch: <Statements extends readonly Array<Statement<any>>>(statements: Statements) => Effect<{ [K in string | number | symbol]: Success<Statements[K]> }, SqlError>;
  readonly config: D1ClientConfig;
  readonly updateValues: never;
}

Type IDs

TypeId

Added in v4.0.0 Source

Unique runtime identifier used to tag D1Client values.

Signature

declare const TypeId: TypeId;

TypeId type

Added in v4.0.0 Source

Type-level literal for the D1Client runtime identifier.

Signature

type TypeId = "~@effect/sql-d1/D1Client";