( options: SqliteClientMemoryConfig )
| 129 | * @category constructors |
| 130 | * @since 4.0.0 |
| 131 | */ |
| 132 | export const makeMemory = ( |
| 133 | options: SqliteClientMemoryConfig |
| 134 | ): Effect.Effect<SqliteClient, SqlError, Scope.Scope | Reactivity.Reactivity> => |
| 135 | Effect.gen(function*() { |
| 136 | const reactivity = yield* Reactivity.Reactivity |
| 137 | const compiler = Statement.makeCompilerSqlite(options.transformQueryNames) |
| 138 | const transformRows = options.transformResultNames ? |
| 139 | Statement.defaultTransforms( |
| 140 | options.transformResultNames |
| 141 | ).array : |
| 142 | undefined |
| 143 | |
| 144 | const makeConnection = Effect.gen(function*() { |
| 145 | const sqlite3 = yield* initEffect |
| 146 | |
| 147 | if (registered.has("memory-vfs") === false) { |
| 148 | registered.add("memory-vfs") |
| 149 | const module = yield* initModule |
| 150 | // @ts-expect-error |
| 151 | const vfs = new MemoryVFS("memory-vfs", module) |
| 152 | sqlite3.vfs_register(vfs as any, false) |
| 153 | } |
| 154 | const db = yield* Effect.acquireRelease( |
| 155 | Effect.try({ |
| 156 | try: () => sqlite3.open_v2(":memory:", undefined, "memory-vfs"), |
| 157 | catch: (cause) => new SqlError({ reason: classifyError(cause, "Failed to open database", "openDatabase") }) |
| 158 | }), |
| 159 | (db) => Effect.sync(() => sqlite3.close(db)) |
| 160 | ) |
| 161 | |
| 162 | if (options.installReactivityHooks) { |
| 163 | sqlite3.update_hook(db, (_op, _db, table, rowid) => { |
| 164 | if (!table) return |
| 165 | const id = String(Number(rowid)) |
| 166 | reactivity.invalidateUnsafe({ [table]: [id] }) |
| 167 | }) |
| 168 | } |
| 169 | |
| 170 | const run = ( |
| 171 | sql: string, |
| 172 | params: ReadonlyArray<unknown> = [], |
| 173 | rowMode: "object" | "array" = "object" |
| 174 | ) => |
| 175 | Effect.try({ |
| 176 | try: () => { |
| 177 | const results: Array<any> = [] |
| 178 | for (const stmt of sqlite3.statements(db, sql)) { |
| 179 | let columns: Array<string> | undefined |
| 180 | sqlite3.bind_collection(stmt, params as any) |
| 181 | while (sqlite3.step(stmt) === WaSqlite.SQLITE_ROW) { |
| 182 | columns = columns ?? sqlite3.column_names(stmt) |
| 183 | const row = sqlite3.row(stmt) |
| 184 | if (rowMode === "object") { |
| 185 | const obj: Record<string, any> = {} |
| 186 | for (let i = 0; i < columns.length; i++) { |
| 187 | Rec.assignProperty(obj, columns[i], row[i]) |
| 188 | } |
no test coverage detected
searching dependent graphs…