| 47 | } |
| 48 | |
| 49 | export const make = ( |
| 50 | options: SqliteClientConfig, |
| 51 | ): Effect.Effect<SqliteClient, never, Scope.Scope | Reactivity.Reactivity> => |
| 52 | Effect.gen(function* () { |
| 53 | const compiler = Statement.makeCompilerSqlite(options.transformQueryNames) |
| 54 | const transformRows = options.transformResultNames |
| 55 | ? Statement.defaultTransforms(options.transformResultNames).array |
| 56 | : undefined |
| 57 | |
| 58 | const makeConnection = Effect.gen(function* () { |
| 59 | const db = new DatabaseSync(options.filename, { |
| 60 | readOnly: options.readonly, |
| 61 | timeout: options.timeout, |
| 62 | allowExtension: options.allowExtension, |
| 63 | enableForeignKeyConstraints: true, |
| 64 | open: true, |
| 65 | }) |
| 66 | yield* Effect.addFinalizer(() => Effect.sync(() => db.close())) |
| 67 | |
| 68 | if (options.disableWAL !== true && options.readonly !== true) { |
| 69 | db.exec("PRAGMA journal_mode = WAL;") |
| 70 | } |
| 71 | |
| 72 | const run = (sql: string, params: ReadonlyArray<unknown> = []) => |
| 73 | Effect.withFiber<Array<Record<string, unknown>>, SqlError>((fiber) => { |
| 74 | const statement = db.prepare(sql) |
| 75 | statement.setReadBigInts(Context.get(fiber.context, Client.SafeIntegers)) |
| 76 | try { |
| 77 | return Effect.succeed(statement.all(...(params as SQLInputValue[])) as Array<Record<string, unknown>>) |
| 78 | } catch (cause) { |
| 79 | return Effect.fail( |
| 80 | new SqlError({ |
| 81 | reason: classifySqliteError(cause, { message: "Failed to execute statement", operation: "execute" }), |
| 82 | }), |
| 83 | ) |
| 84 | } |
| 85 | }) |
| 86 | |
| 87 | const runValues = (sql: string, params: ReadonlyArray<unknown> = []) => |
| 88 | Effect.withFiber<ReadonlyArray<ReadonlyArray<unknown>>, SqlError>((fiber) => { |
| 89 | const statement = db.prepare(sql) |
| 90 | statement.setReadBigInts(Context.get(fiber.context, Client.SafeIntegers)) |
| 91 | statement.setReturnArrays(true) |
| 92 | try { |
| 93 | return Effect.succeed( |
| 94 | statement.all(...(params as SQLInputValue[])) as unknown as ReadonlyArray<ReadonlyArray<unknown>>, |
| 95 | ) |
| 96 | } catch (cause) { |
| 97 | return Effect.fail( |
| 98 | new SqlError({ |
| 99 | reason: classifySqliteError(cause, { message: "Failed to execute statement", operation: "execute" }), |
| 100 | }), |
| 101 | ) |
| 102 | } |
| 103 | }) |
| 104 | |
| 105 | return identity<SqliteConnection>({ |
| 106 | execute(sql, params, transformRows) { |