( options: SqliteClientConfig )
| 117 | * @since 4.0.0 |
| 118 | */ |
| 119 | export const make = ( |
| 120 | options: SqliteClientConfig |
| 121 | ): Effect.Effect<SqliteClient, never, Scope.Scope | Reactivity.Reactivity> => |
| 122 | Effect.gen(function*() { |
| 123 | const compiler = Statement.makeCompilerSqlite(options.transformQueryNames) |
| 124 | const transformRows = options.transformResultNames ? |
| 125 | Statement.defaultTransforms( |
| 126 | options.transformResultNames |
| 127 | ).array : |
| 128 | undefined |
| 129 | |
| 130 | const makeConnection = Effect.gen(function*() { |
| 131 | const readonly = options.readonly === true |
| 132 | const db = new Database(options.filename, { |
| 133 | readonly, |
| 134 | readwrite: readonly ? false : options.readwrite ?? true, |
| 135 | create: readonly ? false : options.create ?? true |
| 136 | } as any) |
| 137 | yield* Effect.addFinalizer(() => Effect.sync(() => db.close())) |
| 138 | const busyTimeout = Math.min( |
| 139 | MAX_BUSY_TIMEOUT, |
| 140 | Math.max(0, Math.round(Duration.toMillis(options.busyTimeout ?? Duration.seconds(5)))) |
| 141 | ) |
| 142 | db.run(`PRAGMA busy_timeout = ${busyTimeout};`) |
| 143 | |
| 144 | if (options.disableWAL !== true && !readonly) { |
| 145 | db.run("PRAGMA journal_mode = WAL;") |
| 146 | } |
| 147 | |
| 148 | const prepare = (sql: string, useSafeIntegers: boolean) => { |
| 149 | const statement = db.query(sql) |
| 150 | // @ts-ignore bun-types missing safeIntegers method, fixed in https://github.com/oven-sh/bun/pull/26627 |
| 151 | statement.safeIntegers(useSafeIntegers) |
| 152 | return statement |
| 153 | } |
| 154 | |
| 155 | const run = ( |
| 156 | sql: string, |
| 157 | params: ReadonlyArray<unknown> = [] |
| 158 | ) => |
| 159 | Effect.withFiber<Array<any>, SqlError>((fiber) => { |
| 160 | const useSafeIntegers = Context.get(fiber.context, Client.SafeIntegers) |
| 161 | try { |
| 162 | return Effect.succeed((prepare(sql, useSafeIntegers).all(...(params as any)) ?? []) as Array<any>) |
| 163 | } catch (cause) { |
| 164 | return Effect.fail(new SqlError({ reason: classifyError(cause, "Failed to execute statement", "execute") })) |
| 165 | } |
| 166 | }) |
| 167 | |
| 168 | const runValues = ( |
| 169 | sql: string, |
| 170 | params: ReadonlyArray<unknown> = [] |
| 171 | ) => |
| 172 | Effect.withFiber<Array<any>, SqlError>((fiber) => { |
| 173 | const useSafeIntegers = Context.get(fiber.context, Client.SafeIntegers) |
| 174 | try { |
| 175 | return Effect.succeed((prepare(sql, useSafeIntegers).values(...(params as any)) ?? []) as Array<any>) |
| 176 | } catch (cause) { |
no test coverage detected