Something went wrong. Try again.
opencode v2 mirror
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101import { DatabaseSync, type SQLInputValue } from "node:sqlite"import { Context, Effect, Layer } from "effect"import { Reactivity } from "effect/unstable/reactivity"import { SqlClient } from "effect/unstable/sql"import { classifySqliteError, SqlError } from "effect/unstable/sql/SqlError"import { Sqlite } from "./sqlite.js"
const TypeId = "~@opencode/core/database/SqliteNode" as const
export const supportsTuningPragmas = true
// Foreign keys default OFF and can be toggled per connection.export const supportsForeignKeyToggle = true
interface Config extends Sqlite.ClientConfig { readonly filename: string readonly readonly?: boolean readonly create?: boolean readonly readwrite?: boolean readonly disableWAL?: boolean readonly timeout?: number readonly allowExtension?: boolean}
const make = (options: Config) => Effect.gen(function* () { const native = (yield* Sqlite.Native) as DatabaseSync
const run = (query: string, params: ReadonlyArray<unknown> = []) => Effect.withFiber<Array<Record<string, unknown>>, SqlError>((fiber) => { const statement = native.prepare(query) statement.setReadBigInts(Context.get(fiber.context, SqlClient.SafeIntegers)) try { return Effect.succeed(statement.all(...(params as SQLInputValue[])) as Array<Record<string, unknown>>) } catch (cause) { return Effect.fail( new SqlError({ reason: classifySqliteError(cause, { message: "Failed to execute statement", operation: "execute" }), }), ) } })
const runValues = (query: string, params: ReadonlyArray<unknown> = []) => Effect.withFiber<ReadonlyArray<ReadonlyArray<unknown>>, SqlError>((fiber) => { const statement = native.prepare(query) statement.setReadBigInts(Context.get(fiber.context, SqlClient.SafeIntegers)) statement.setReturnArrays(true) try { return Effect.succeed( statement.all(...(params as SQLInputValue[])) as unknown as ReadonlyArray<ReadonlyArray<unknown>>, ) } catch (cause) { return Effect.fail( new SqlError({ reason: classifySqliteError(cause, { message: "Failed to execute statement", operation: "execute" }), }), ) } })
const connection = Sqlite.makeConnection(run, runValues, { loadExtension: (path: string) => Effect.try({ try: () => native.loadExtension(path), catch: (cause) => new SqlError({ reason: classifySqliteError(cause, { message: "Failed to load extension", operation: "loadExtension" }), }), }), })
return yield* Sqlite.makeClient(options, connection, TypeId, (acquirer) => ({ loadExtension: (path: string) => Effect.flatMap(acquirer, (_) => _.loadExtension(path)), })) })
const nativeLayer = (config: Config) => Layer.effect( Sqlite.Native, Effect.gen(function* () { const native = new DatabaseSync(config.filename, { readOnly: config.readonly, timeout: config.timeout, allowExtension: config.allowExtension, enableForeignKeyConstraints: true, open: true, }) yield* Effect.addFinalizer(() => Effect.sync(() => native.close())) if (config.disableWAL !== true && config.readonly !== true) native.exec("PRAGMA journal_mode = WAL;") return native }), )
const clientLayer = (config: Config) => Layer.effect(SqlClient.SqlClient, make(config))
export const sqliteLayer = (config: Config) => { const native = nativeLayer(config) return Layer.merge(native, clientLayer(config).pipe(Layer.provide(native))).pipe(Layer.provide(Reactivity.layer))}