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

Function make

packages/sql/sqlite-bun/src/SqliteClient.ts:131–304  ·  view source on GitHub ↗
(
  options: SqliteClientConfig
)

Source from the content-addressed store, hash-verified

129 * @since 4.0.0
130 */
131export const make = (
132 options: SqliteClientConfig
133): Effect.Effect<SqliteClient & Client.SqlClient, SqlError, Scope.Scope | Reactivity.Reactivity> =>
134 Effect.gen(function*() {
135 const compiler = Statement.makeCompilerSqlite(options.transformQueryNames)
136 const transformRows = options.transformResultNames ?
137 Statement.defaultTransforms(
138 options.transformResultNames
139 ).array :
140 undefined
141
142 const makeConnection = Effect.gen(function*() {
143 const readonly = options.readonly === true
144 const openOptions = {
145 readonly,
146 readwrite: readonly ? false : options.readwrite ?? true,
147 create: readonly ? false : options.create ?? true
148 }
149 const db = yield* Effect.acquireRelease(
150 Effect.try({
151 try: () =>
152 new Database(
153 options.filename,
154 options.filename.startsWith("file:")
155 ? constants.SQLITE_OPEN_URI |
156 (openOptions.readonly ? constants.SQLITE_OPEN_READONLY : 0) |
157 (openOptions.readwrite || openOptions.create ? constants.SQLITE_OPEN_READWRITE : 0) |
158 (openOptions.create ? constants.SQLITE_OPEN_CREATE : 0)
159 : openOptions
160 ),
161 catch: (cause) => new SqlError({ reason: classifyError(cause, "Failed to open database", "connect") })
162 }),
163 (db) => Effect.sync(() => db.close())
164 )
165 const busyTimeout = Math.min(
166 MAX_BUSY_TIMEOUT,
167 Math.max(0, Math.round(Duration.toMillis(options.busyTimeout ?? Duration.seconds(5))))
168 )
169 const exec = (sql: string) =>
170 Effect.try({
171 try: () => db.run(sql),
172 catch: (cause) => new SqlError({ reason: classifyError(cause, "Failed to configure database", "connect") })
173 })
174 yield* exec(`PRAGMA busy_timeout = ${busyTimeout};`)
175
176 if (options.disableWAL !== true && !readonly) {
177 // Switching to WAL can fail with SQLITE_BUSY without invoking the busy
178 // handler, so retry it until busyTimeout has elapsed. Each attempt's
179 // native busy wait is limited to the remaining time.
180 const deadline = (yield* Clock.monotonicTimeNanos) + BigInt(busyTimeout) * NANOS_PER_MILLI
181 const remainingMillis = Effect.map(
182 Clock.monotonicTimeNanos,
183 (now) => Math.max(0, Number((deadline - now) / NANOS_PER_MILLI))
184 )
185 yield* remainingMillis.pipe(
186 Effect.flatMap((remaining) => exec(`PRAGMA busy_timeout = ${remaining}`)),
187 Effect.andThen(exec("PRAGMA journal_mode = WAL;")),
188 Effect.retry({

Callers 1

layerFunction · 0.70

Calls 13

assignMethod · 0.80
classifyErrorFunction · 0.70
execFunction · 0.70
closeMethod · 0.65
pipeMethod · 0.65
makeMethod · 0.65
BigIntInterface · 0.50
NumberFunction · 0.50
identityFunction · 0.50
syncMethod · 0.45
mapMethod · 0.45
retryMethod · 0.45

Tested by

no test coverage detected