| 45 | } |
| 46 | |
| 47 | const make = (options: Config) => |
| 48 | Effect.gen(function* () { |
| 49 | const native = (yield* Sqlite.Native) as Database |
| 50 | |
| 51 | const compiler = Statement.makeCompilerSqlite(options.transformQueryNames) |
| 52 | const transformRows = options.transformResultNames |
| 53 | ? Statement.defaultTransforms(options.transformResultNames).array |
| 54 | : undefined |
| 55 | |
| 56 | const run = (query: string, params: ReadonlyArray<unknown> = []) => |
| 57 | Effect.withFiber<Array<Record<string, unknown>>, SqlError>((fiber) => { |
| 58 | const statement = native.query(query) |
| 59 | // @ts-ignore bun-types missing safeIntegers method, fixed in https://github.com/oven-sh/bun/pull/26627 |
| 60 | statement.safeIntegers(Context.get(fiber.context, Client.SafeIntegers)) |
| 61 | try { |
| 62 | return Effect.succeed((statement.all(...(params as any)) ?? []) as Array<Record<string, unknown>>) |
| 63 | } catch (cause) { |
| 64 | return Effect.fail( |
| 65 | new SqlError({ |
| 66 | reason: classifySqliteError(cause, { message: "Failed to execute statement", operation: "execute" }), |
| 67 | }), |
| 68 | ) |
| 69 | } |
| 70 | }) |
| 71 | |
| 72 | const runValues = (query: string, params: ReadonlyArray<unknown> = []) => |
| 73 | Effect.withFiber<Array<unknown[]>, SqlError>((fiber) => { |
| 74 | const statement = native.query(query) |
| 75 | // @ts-ignore bun-types missing safeIntegers method, fixed in https://github.com/oven-sh/bun/pull/26627 |
| 76 | statement.safeIntegers(Context.get(fiber.context, Client.SafeIntegers)) |
| 77 | try { |
| 78 | return Effect.succeed((statement.values(...(params as any)) ?? []) as Array<unknown[]>) |
| 79 | } catch (cause) { |
| 80 | return Effect.fail( |
| 81 | new SqlError({ |
| 82 | reason: classifySqliteError(cause, { message: "Failed to execute statement", operation: "execute" }), |
| 83 | }), |
| 84 | ) |
| 85 | } |
| 86 | }) |
| 87 | |
| 88 | const connection = identity<SqliteConnection>({ |
| 89 | execute(query, params, transformRows) { |
| 90 | return transformRows ? Effect.map(run(query, params), transformRows) : run(query, params) |
| 91 | }, |
| 92 | executeRaw(query, params) { |
| 93 | return run(query, params) |
| 94 | }, |
| 95 | executeValues(query, params) { |
| 96 | return runValues(query, params) |
| 97 | }, |
| 98 | executeUnprepared(query, params, transformRows) { |
| 99 | return this.execute(query, params, transformRows) |
| 100 | }, |
| 101 | executeStream() { |
| 102 | return Stream.die("executeStream not implemented") |
| 103 | }, |
| 104 | export: Effect.try({ |