MCPcopy Create free account
hub / github.com/effect-app/libs / make

Function make

repos/effect/packages/sql/d1/src/D1Client.ts:99–231  ·  view source on GitHub ↗
(
  options: D1ClientConfig
)

Source from the content-addressed store, hash-verified

97 * context.
98 *
99 * @category services
100 * @since 4.0.0
101 */
102export 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 */
110export 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
120type TransformRows = <A extends object>(rows: ReadonlyArray<A>) => ReadonlyArray<A>
121
122type BatchResults<Statements extends ReadonlyArray<Statement.Statement<any>>> = {
123 readonly [K in keyof Statements]: Effect.Success<Statements[K]>
124}
125
126interface StatementWithTransformRows extends Statement.Statement<any> {
127 readonly transformRows: TransformRows | undefined
128}
129
130const 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()

Callers 1

layerFunction · 0.70

Calls 4

identityFunction · 0.85
classifyErrorFunction · 0.70
makeMethod · 0.65
succeedMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…