UNPKG

@withstudiocms/effect

Version:

Effect-TS Utilities for Astro

129 lines (128 loc) 6.97 kB
import type { Database } from '@astrojs/db/runtime'; import type { ResultSet } from '@libsql/client'; import type { ExtractTablesWithRelations } from 'drizzle-orm'; import type { LibSQLDatabase } from 'drizzle-orm/libsql'; import type { SQLiteTransaction } from 'drizzle-orm/sqlite-core'; import { Context, Effect } from './effect.js'; declare const LibSQLClientError_base: new <A extends Record<string, any> = {}>(args: import("effect/Types").Equals<A, {}> extends true ? void : { readonly [P in keyof A as P extends "_tag" ? never : P]: A[P]; }) => import("effect/Cause").YieldableError & { readonly _tag: "LibSQLClientError"; } & Readonly<A>; /** * Represents an error specific to the LibSQL client. * * This error extends from `Data.TaggedError` with the tag `'LibSQLClientError'` * and includes a `cause` property to provide additional context about the underlying issue. * * @example * ```typescript * throw new LibSQLClientError({ cause: originalError }); * ``` * * @property cause - The underlying cause of the error, can be any value. */ export declare class LibSQLClientError extends LibSQLClientError_base<{ cause: unknown; }> { } /** * Represents a SQLite transaction client with asynchronous operations. * * @typeParam 'async' - Specifies that the transaction is asynchronous. * @typeParam ResultSet - The result set type returned by queries. * @typeParam Record<string, never> - The schema type for the transaction (empty object in this case). * @typeParam ExtractTablesWithRelations<Record<string, never>> - Extracted table relations from the schema. */ export type TransactionClient<Schema extends Record<string, unknown> = Record<string, never>> = SQLiteTransaction<'async', ResultSet, Schema, ExtractTablesWithRelations<Schema>>; /** * Represents a function that executes a provided asynchronous operation using a database client. * * @template Schema - The database schema type, defaults to an empty record. * @template T - The result type of the asynchronous operation. * @param fn - An asynchronous function that receives either a `LibSQLDatabase` or `TransactionClient` * and returns a promise of type `T`. * @returns An `Effect` that resolves to the result of type `T` or fails with a `LibSQLClientError`. */ export type ExecuteFn<Schema extends Record<string, unknown> = Record<string, never>> = <T>(fn: (client: LibSQLDatabase<Schema> | TransactionClient<Schema>) => Promise<T>) => Effect.Effect<T, LibSQLClientError>; /** * Represents a function that executes a given asynchronous operation within a transaction context. * * @template U The type of the value returned by the provided function. * @param fn - A function that receives a `TransactionClient` and returns a `Promise` of type `U`. * @returns An `Effect` that resolves to the result of type `U` or fails with a `LibSQLClientError`. */ export type TransactionContextShape<Schema extends Record<string, unknown> = Record<string, never>> = <U>(fn: (client: TransactionClient<Schema>) => Promise<U>) => Effect.Effect<U, LibSQLClientError>; declare const TransactionContext_base: Context.TagClass<TransactionContext, "TransactionContext", TransactionContextShape<Record<string, never>>>; /** * Represents a context tag for managing transaction context within the Studiocms SDK effect system. * * This class extends a generic `Context.Tag` to provide a strongly-typed context for database transactions. * * @template TransactionContext - The type of the transaction context. * @template TransactionContextShape - The shape of the transaction context. * * @example * ```typescript * const transactionContext = { /* ... *\/ }; * const effectWithTransaction = TransactionContext.provide(transactionContext)(someEffect); * ``` */ export declare class TransactionContext extends TransactionContext_base { static readonly provide: (transaction: TransactionContextShape) => (<A, E, R>(self: Effect.Effect<A, E, R>) => Effect.Effect<A, E, Exclude<R, TransactionContext>>); } declare const DrizzleClient_base: Context.TagClass<DrizzleClient, "DrizzleClient", { drizzle: LibSQLDatabase | Database; schema?: Record<string, unknown>; }>; /** * Represents a DrizzleClient context tag for dependency injection. * * @template DrizzleClient - The type of the DrizzleClient. * @template T - The context value, containing: * - `drizzle`: An instance of either `LibSQLDatabase` or `Database`. * - `schema`: A record representing the database schema. * * This class is used to provide and consume a Drizzle database client and its schema * within a context-aware application, leveraging the Context.Tag utility for type safety. */ export declare class DrizzleClient extends DrizzleClient_base { } declare const DrizzleDBClientService_base: Effect.Service.Class<DrizzleDBClientService, "DrizzleDBClientService", { readonly effect: Effect.Effect<{ readonly makeQuery: <A, E, R, Input = never>(queryFn: (execute: ExecuteFn<Record<string, unknown>>, input: Input) => Effect.Effect<A, E, R>) => (...args: [Input] extends [never] ? [] : [input: Input]) => Effect.Effect<A, E, R>; readonly execute: <T>(fn: (client: LibSQLDatabase<Record<string, never>> | Database) => Promise<T>) => Effect.Effect<T, LibSQLClientError, never>; }, never, DrizzleClient>; }>; /** * DrizzleDBClient is a service class that provides an interface for executing queries * and commands against a Drizzle ORM database instance within an Effect system. * * @remarks * This class extends `Effect.Service` and is registered under the service key `'DrizzleDBClient'`. * It exposes two main utilities: * - `execute`: A function to safely execute asynchronous operations on the Drizzle client, * handling errors using `useWithErrorPromise`. * - `makeQuery`: A higher-order function to create effectful queries that can optionally * participate in a transaction context if available. * * @example * ```typescript * const { makeQuery, execute } = yield* DrizzleDBClient; * const result = yield* makeQuery((exec, input) => exec( ... )); * ``` */ export declare class DrizzleDBClientService extends DrizzleDBClientService_base { } /** * Creates a live Drizzle database client effect with the provided configuration. * * @template Schema - The type of the schema object, extending a record of string keys to unknown values. * @param config - The configuration object for the Drizzle client. * @param config.drizzle - An instance of either `LibSQLDatabase` or `Database` to be used by the client. * @param [config.schema] - Optional schema definition for the database. * @returns An Effect that provides a Drizzle database client service, configured with the given parameters. */ export declare const drizzleDBClientLive: <Schema extends Record<string, unknown>>(config: { drizzle: LibSQLDatabase | Database; schema?: Schema; }) => Effect.Effect<DrizzleDBClientService, never, never>; export {};