( options: SqliteClientConfig )
| 119 | * @since 4.0.0 |
| 120 | */ |
| 121 | export const make = ( |
| 122 | options: SqliteClientConfig |
| 123 | ): Effect.Effect<SqliteClient, never, Scope.Scope | Reactivity.Reactivity> => |
| 124 | Effect.gen(function*() { |
| 125 | const clientOptions: Parameters<typeof Sqlite.open>[0] = { |
| 126 | name: options.filename |
| 127 | } |
| 128 | if (options.location) { |
| 129 | clientOptions.location = options.location |
| 130 | } |
| 131 | if (options.encryptionKey) { |
| 132 | clientOptions.encryptionKey = options.encryptionKey |
| 133 | } |
| 134 | |
| 135 | const compiler = Statement.makeCompilerSqlite(options.transformQueryNames) |
| 136 | const transformRows = options.transformResultNames ? |
| 137 | Statement.defaultTransforms(options.transformResultNames).array : |
| 138 | undefined |
| 139 | |
| 140 | const makeConnection = Effect.gen(function*() { |
| 141 | const db = Sqlite.open(clientOptions) as DB |
| 142 | yield* Effect.addFinalizer(() => Effect.sync(() => db.close())) |
| 143 | |
| 144 | const run = ( |
| 145 | sql: string, |
| 146 | params: ReadonlyArray<unknown> = [] |
| 147 | ) => |
| 148 | Effect.withFiber<Array<any>, SqlError>((fiber) => { |
| 149 | if (fiber.getRef(AsyncQuery)) { |
| 150 | return Effect.map( |
| 151 | Effect.tryPromise({ |
| 152 | try: () => db.execute(sql, params as Array<any>), |
| 153 | catch: (cause) => |
| 154 | new SqlError({ reason: classifyError(cause, "Failed to execute statement (async)", "execute") }) |
| 155 | }), |
| 156 | (result) => result.rows |
| 157 | ) |
| 158 | } |
| 159 | return Effect.try({ |
| 160 | try: () => db.executeSync(sql, params as Array<any>).rows, |
| 161 | catch: (cause) => new SqlError({ reason: classifyError(cause, "Failed to execute statement", "execute") }) |
| 162 | }) |
| 163 | }) |
| 164 | |
| 165 | const runValues = ( |
| 166 | sql: string, |
| 167 | params: ReadonlyArray<unknown> = [] |
| 168 | ) => |
| 169 | Effect.withFiber<Array<any>, SqlError>((fiber) => { |
| 170 | if (fiber.getRef(AsyncQuery)) { |
| 171 | return Effect.tryPromise({ |
| 172 | try: () => db.executeRaw(sql, params as Array<any>), |
| 173 | catch: (cause) => |
| 174 | new SqlError({ reason: classifyError(cause, "Failed to execute statement (async)", "execute") }) |
| 175 | }) |
| 176 | } |
| 177 | return Effect.try({ |
| 178 | try: () => db.executeRawSync(sql, params as Array<any>), |
no test coverage detected
searching dependent graphs…