( options: SqliteClientMemoryConfig )
| 102 | * @since 1.0.0 |
| 103 | */ |
| 104 | export const makeMemory = ( |
| 105 | options: SqliteClientMemoryConfig |
| 106 | ): Effect.Effect<SqliteClient, SqlError, Scope.Scope | Reactivity.Reactivity> => |
| 107 | Effect.gen(function*() { |
| 108 | const reactivity = yield* Reactivity.Reactivity |
| 109 | const compiler = Statement.makeCompilerSqlite(options.transformQueryNames) |
| 110 | const transformRows = options.transformResultNames ? |
| 111 | Statement.defaultTransforms( |
| 112 | options.transformResultNames |
| 113 | ).array : |
| 114 | undefined |
| 115 | |
| 116 | const makeConnection = Effect.gen(function*() { |
| 117 | const sqlite3 = yield* initEffect |
| 118 | |
| 119 | if (registered.has("memory-vfs") === false) { |
| 120 | registered.add("memory-vfs") |
| 121 | const module = yield* initModule |
| 122 | // @ts-expect-error |
| 123 | const vfs = new MemoryVFS("memory-vfs", module) |
| 124 | sqlite3.vfs_register(vfs as any, false) |
| 125 | } |
| 126 | const db = yield* Effect.acquireRelease( |
| 127 | Effect.try({ |
| 128 | try: () => sqlite3.open_v2(":memory:", undefined, "memory-vfs"), |
| 129 | catch: (cause) => new SqlError({ cause, message: "Failed to open database" }) |
| 130 | }), |
| 131 | (db) => Effect.sync(() => sqlite3.close(db)) |
| 132 | ) |
| 133 | |
| 134 | if (options.installReactivityHooks) { |
| 135 | sqlite3.update_hook(db, (_op, _db, table, rowid) => { |
| 136 | if (!table) return |
| 137 | const id = String(Number(rowid)) |
| 138 | reactivity.unsafeInvalidate({ [table]: [id] }) |
| 139 | }) |
| 140 | } |
| 141 | |
| 142 | const run = ( |
| 143 | sql: string, |
| 144 | params: ReadonlyArray<unknown> = [], |
| 145 | rowMode: "object" | "array" = "object" |
| 146 | ) => |
| 147 | Effect.try({ |
| 148 | try: () => { |
| 149 | const results: Array<any> = [] |
| 150 | for (const stmt of sqlite3.statements(db, sql)) { |
| 151 | let columns: Array<string> | undefined |
| 152 | sqlite3.bind_collection(stmt, params as any) |
| 153 | while (sqlite3.step(stmt) === WaSqlite.SQLITE_ROW) { |
| 154 | columns = columns ?? sqlite3.column_names(stmt) |
| 155 | const row = sqlite3.row(stmt) |
| 156 | if (rowMode === "object") { |
| 157 | const obj: Record<string, any> = {} |
| 158 | for (let i = 0; i < columns.length; i++) { |
| 159 | obj[columns[i]] = row[i] |
| 160 | } |
| 161 | results.push(obj) |
no test coverage detected