MCPcopy Create free account
hub / github.com/Effect-TS/effect / make

Function make

packages/sql/sqlite-do/src/SqliteClient.ts:211–342  ·  view source on GitHub ↗
(
  options: SqliteClientConfig
)

Source from the content-addressed store, hash-verified

209 * @since 4.0.0
210 */
211export const make = (
212 options: SqliteClientConfig
213): Effect.Effect<SqliteClient & Client.SqlClient, never, Scope.Scope | Reactivity.Reactivity> =>
214 Effect.gen(function*() {
215 const compiler = Statement.makeCompilerSqlite(options.transformQueryNames)
216 const transformRows = options.transformResultNames
217 ? Statement.defaultTransforms(options.transformResultNames).array
218 : undefined
219 const db = options.storage?.sql ?? options.db
220
221 if (db === undefined) {
222 return yield* Effect.die("SqliteClient.make requires either a Durable Object storage or sql storage")
223 }
224 const sqlStorage = db
225
226 const makeConnection = Effect.gen(function*() {
227 function* runIterator(
228 sql: string,
229 params: ReadonlyArray<unknown> = []
230 ) {
231 const cursor = sqlStorage.exec(sql, ...params)
232 const columns = cursor.columnNames
233 for (const result of cursor.raw()) {
234 const obj: any = {}
235 for (let i = 0; i < columns.length; i++) {
236 const value = result[i]
237 Rec.assignProperty(obj, columns[i], value instanceof ArrayBuffer ? new Uint8Array(value) : value)
238 }
239 yield obj
240 }
241 }
242
243 const runStatement = (
244 sql: string,
245 params: ReadonlyArray<unknown> = []
246 ): Effect.Effect<ReadonlyArray<any>, SqlError, never> =>
247 Effect.try({
248 try: () => Array.from(runIterator(sql, params)),
249 catch: (cause) => new SqlError({ reason: classifyError(cause, "Failed to execute statement", "execute") })
250 })
251
252 const runValues = (
253 sql: string,
254 params: ReadonlyArray<unknown> = []
255 ): Effect.Effect<ReadonlyArray<any>, SqlError, never> =>
256 Effect.try({
257 try: () =>
258 Array.from(sqlStorage.exec(sql, ...params).raw(), (row) => {
259 for (let i = 0; i < row.length; i++) {
260 const value = row[i]
261 if (value instanceof ArrayBuffer) {
262 row[i] = new Uint8Array(value) as any
263 }
264 }
265 return row
266 }),
267 catch: (cause) => new SqlError({ reason: classifyError(cause, "Failed to execute statement", "execute") })
268 })

Callers 1

layerFunction · 0.70

Calls 9

withPermitMethod · 0.80
assignMethod · 0.80
makeMethod · 0.65
takeMethod · 0.65
releaseMethod · 0.65
identityFunction · 0.50
fFunction · 0.50

Tested by

no test coverage detected