MCPcopy Create free account
hub / github.com/Effect-TS/effect / makeMemory

Function makeMemory

packages/sql/sqlite-wasm/src/SqliteClient.ts:145–301  ·  view source on GitHub ↗
(
  options: SqliteClientMemoryConfig
)

Source from the content-addressed store, hash-verified

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

Callers 1

layerMemoryFunction · 0.70

Calls 11

assignMethod · 0.80
classifyErrorFunction · 0.70
hasMethod · 0.65
addMethod · 0.65
closeMethod · 0.65
makeMethod · 0.65
StringFunction · 0.50
NumberFunction · 0.50
identityFunction · 0.50
syncMethod · 0.45
succeedMethod · 0.45

Tested by

no test coverage detected