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

Function make

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

Source from the content-addressed store, hash-verified

117 * @since 4.0.0
118 */
119export const make = (
120 options: SqliteClientConfig
121): Effect.Effect<SqliteClient, never, Scope.Scope | Reactivity.Reactivity> =>
122 Effect.gen(function*() {
123 const compiler = Statement.makeCompilerSqlite(options.transformQueryNames)
124 const transformRows = options.transformResultNames ?
125 Statement.defaultTransforms(
126 options.transformResultNames
127 ).array :
128 undefined
129
130 const makeConnection = Effect.gen(function*() {
131 const readonly = options.readonly === true
132 const db = new Database(options.filename, {
133 readonly,
134 readwrite: readonly ? false : options.readwrite ?? true,
135 create: readonly ? false : options.create ?? true
136 } as any)
137 yield* Effect.addFinalizer(() => Effect.sync(() => db.close()))
138 const busyTimeout = Math.min(
139 MAX_BUSY_TIMEOUT,
140 Math.max(0, Math.round(Duration.toMillis(options.busyTimeout ?? Duration.seconds(5))))
141 )
142 db.run(`PRAGMA busy_timeout = ${busyTimeout};`)
143
144 if (options.disableWAL !== true && !readonly) {
145 db.run("PRAGMA journal_mode = WAL;")
146 }
147
148 const prepare = (sql: string, useSafeIntegers: boolean) => {
149 const statement = db.query(sql)
150 // @ts-ignore bun-types missing safeIntegers method, fixed in https://github.com/oven-sh/bun/pull/26627
151 statement.safeIntegers(useSafeIntegers)
152 return statement
153 }
154
155 const run = (
156 sql: string,
157 params: ReadonlyArray<unknown> = []
158 ) =>
159 Effect.withFiber<Array<any>, SqlError>((fiber) => {
160 const useSafeIntegers = Context.get(fiber.context, Client.SafeIntegers)
161 try {
162 return Effect.succeed((prepare(sql, useSafeIntegers).all(...(params as any)) ?? []) as Array<any>)
163 } catch (cause) {
164 return Effect.fail(new SqlError({ reason: classifyError(cause, "Failed to execute statement", "execute") }))
165 }
166 })
167
168 const runValues = (
169 sql: string,
170 params: ReadonlyArray<unknown> = []
171 ) =>
172 Effect.withFiber<Array<any>, SqlError>((fiber) => {
173 const useSafeIntegers = Context.get(fiber.context, Client.SafeIntegers)
174 try {
175 return Effect.succeed((prepare(sql, useSafeIntegers).values(...(params as any)) ?? []) as Array<any>)
176 } catch (cause) {

Callers 1

layerFunction · 0.70

Calls 13

identityFunction · 0.85
addFinalizerMethod · 0.80
getUnsafeMethod · 0.80
assignMethod · 0.80
classifyErrorFunction · 0.70
closeMethod · 0.65
makeMethod · 0.65
withPermitsMethod · 0.65
takeMethod · 0.65
releaseMethod · 0.65
syncMethod · 0.45
runMethod · 0.45

Tested by

no test coverage detected