( options: D1ClientConfig )
| 97 | * context. |
| 98 | * |
| 99 | * @category services |
| 100 | * @since 4.0.0 |
| 101 | */ |
| 102 | export const D1Client = Context.Service<D1Client>("@effect/sql-d1/D1Client") |
| 103 | |
| 104 | /** |
| 105 | * Configuration for a Cloudflare D1 client, including the `D1Database`, prepared statement cache settings, span attributes, and query/result name transforms. |
| 106 | * |
| 107 | * @category models |
| 108 | * @since 4.0.0 |
| 109 | */ |
| 110 | export interface D1ClientConfig { |
| 111 | readonly db: D1Database |
| 112 | readonly prepareCacheSize?: number | undefined |
| 113 | readonly prepareCacheTTL?: Duration.Input | undefined |
| 114 | readonly spanAttributes?: Record<string, unknown> | undefined |
| 115 | |
| 116 | readonly transformResultNames?: ((str: string) => string) | undefined |
| 117 | readonly transformQueryNames?: ((str: string) => string) | undefined |
| 118 | } |
| 119 | |
| 120 | type TransformRows = <A extends object>(rows: ReadonlyArray<A>) => ReadonlyArray<A> |
| 121 | |
| 122 | type BatchResults<Statements extends ReadonlyArray<Statement.Statement<any>>> = { |
| 123 | readonly [K in keyof Statements]: Effect.Success<Statements[K]> |
| 124 | } |
| 125 | |
| 126 | interface StatementWithTransformRows extends Statement.Statement<any> { |
| 127 | readonly transformRows: TransformRows | undefined |
| 128 | } |
| 129 | |
| 130 | const makeBatch = (options: { |
| 131 | readonly db: D1Database |
| 132 | readonly prepareCache: Cache.Cache<string, D1PreparedStatement, SqlError> |
| 133 | readonly spanAttributes: ReadonlyArray<readonly [string, unknown]> |
| 134 | readonly getClient: () => D1Client |
| 135 | }): D1Client["batch"] => |
| 136 | <const Statements extends ReadonlyArray<Statement.Statement<any>>>( |
| 137 | statements: Statements |
| 138 | ) => { |
| 139 | if (statements.length === 0) { |
| 140 | return Effect.succeed([] as unknown as BatchResults<Statements>) |
| 141 | } |
| 142 | return Effect.useSpan( |
| 143 | "sql.execute", |
| 144 | { kind: "client" }, |
| 145 | (span) => |
| 146 | Effect.withFiber(Effect.fnUntraced(function*(fiber) { |
| 147 | const transformer = fiber.getRef(Statement.CurrentTransformer) |
| 148 | const prepared: Array<D1PreparedStatement> = [] |
| 149 | const transforms: Array<TransformRows | undefined> = [] |
| 150 | const queryTexts: Array<string> = [] |
| 151 | |
| 152 | for (const original of statements) { |
| 153 | const statement = transformer === undefined |
| 154 | ? original |
| 155 | : yield* transformer(original, options.getClient(), fiber, span) |
| 156 | const [sql, params] = statement.compile() |
no test coverage detected
searching dependent graphs…