import { makeExecutionMemo } from "alchemy/Runtime/ExecutionMemo";
import * as Context from "effect/Context";
import * as Effect from "effect/Effect";
import * as Reactivity from "effect/reactivity/Reactivity";
import * as References from "effect/References";
import type * as Scope from "effect/Scope";
import * as SqlClient from "effect/sql/SqlClient";
import type { Connection } from "effect/sql/SqlConnection";
import { ConnectionError, isSqlError, SqlError } from "effect/sql/SqlError";
import * as Statement from "effect/sql/Statement";
import * as Stream from "effect/Stream";
type TransformRows = ((rows: ReadonlyArray) => ReadonlyArray) | undefined;
/**
* What a driver supplies to {@link makeDeferredClient}.
*/
export interface DeferredClientOptions {
/** The driver name, exposed as `SqlClient.driver`. */
readonly driver: string;
/** The driver's statement compiler. Building one is pure, so it exists at Construction phase. */
readonly compiler: Statement.Compiler;
/** The driver's result transform (`transformResultNames`), if configured. */
readonly transformRows?: TransformRows;
/** Attributes for each statement's span. Only what is known before connecting. */
readonly spanAttributes: ReadonlyArray;
/**
* Opens the driver's real client. Runs at most once per execution, with
* that execution's `Scope`, so the pool closes when the execution settles.
*/
readonly connect: Effect.Effect;
}
/**
* A `SqlClient` that exists before its database does.
*/
export interface DeferredClient {
/**
* A real `SqlClient`: statements, fragments and helpers are built and
* compiled by the driver's own compiler. Only running one reaches the
* database.
*/
readonly client: SqlClient.SqlClient;
/**
* This execution's real client, opened on first use. A driver forwards its
* own extras (`PgClient.listen`, `D1Client.batch`) through it.
*/
readonly connected: Effect.Effect;
}
/**
* Build a `SqlClient` at Construction phase for a database that can only be
* reached while handling an event.
*
* A Runtime resolves its clients once, at Construction phase, and uses them
* in every handler. No connection can exist yet: the connection string comes
* from the Runtime's environment, and a socket belongs to the event that
* opened it. So the client is split at the one place it does I/O. Everything
* that builds a query (`` sql`...` ``, `sql.insert`, `sql.and`, an ORM on
* top) is the driver's real, synchronous code. The `Connection` underneath
* forwards each compiled statement to the real client, which `connect` opens
* once per execution.
*
* ```typescript
* const { client, connected } = yield* makeDeferredClient({
* driver: "pg",
* compiler: PgClient.makeCompiler(),
* spanAttributes: [["db.system.name", "postgresql"]],
* connect: Effect.flatMap(url, (url) => PgClient.make({ url })),
* });
* ```
*
* A statement inside `client.withTransaction` runs on the real client's
* transaction connection, and a nested `withTransaction` is a savepoint on
* it. A failure of `connect` that isn't a `SqlError` becomes one with a
* `ConnectionError` reason, because a statement can fail with nothing else.
*
* `connect` needs an execution `Scope`, so running a statement outside one
* is a defect. A `stream` opens its own scope, and so its own client, which
* closes when the stream ends.
*/
export const makeDeferredClient = Effect.fn(function* (
options: DeferredClientOptions,
) {
const memo = yield* makeExecutionMemo(options.connect);
// A statement has no requirements, and every Runtime provides the
// services a connection source reads (`RuntimeContext`) to the fibers that
// run handlers. This is the one place the requirement is dropped.
// oxlint-disable-next-line effecttsgo/unsafe-effect-type-assertion -- the Runtime that runs the handler provides what the connection source reads
const connected = Effect.mapError(memo, (error) =>
isSqlError(error)
? error
: SqlError.make({
reason: ConnectionError.make({
message: "Failed to open the database connection",
operation: "connect",
cause: error,
}),
}),
) as Effect.Effect;
// The deferred statement already ran the statement transformer and owns
// the span, so the real client's statement skips both.
const forward = (run: (real: Client) => Effect.Effect) =>
Effect.flatMap(connected, (real) =>
run(real).pipe(
Effect.provideService(Statement.CurrentTransformer, undefined),
Effect.provideService(References.TracerEnabled, false),
),
);
const transformed = (
rows: Effect.Effect, SqlError>,
transformRows: TransformRows,
) => (transformRows === undefined ? rows : Effect.map(rows, transformRows));
// The statement is already compiled, so it goes to the real client as a
// literal with its parameters. `withoutTransforms` keeps the real client
// from transforming rows a second time.
const connection: Connection = {
execute: (sql, params, transformRows) =>
transformed(
forward((real) => real.unsafe