MCPcopy Create free account
hub / github.com/effect-app/libs / make

Function make

repos/effect/packages/sql/sqlite-do/src/SqliteClient.ts:176–305  ·  view source on GitHub ↗
(
  options: SqliteClientConfig
)

Source from the content-addressed store, hash-verified

174 * @category constructors
175 * @since 4.0.0
176 */
177export const make = (
178 options: SqliteClientConfig
179): Effect.Effect<SqliteClient, never, Scope.Scope | Reactivity.Reactivity> =>
180 Effect.gen(function*() {
181 const compiler = Statement.makeCompilerSqlite(options.transformQueryNames)
182 const transformRows = options.transformResultNames
183 ? Statement.defaultTransforms(options.transformResultNames).array
184 : undefined
185 const db = options.storage?.sql ?? options.db
186
187 if (db === undefined) {
188 return yield* Effect.die("SqliteClient.make requires either a Durable Object storage or sql storage")
189 }
190 const sqlStorage = db
191
192 const makeConnection = Effect.gen(function*() {
193 function* runIterator(
194 sql: string,
195 params: ReadonlyArray<unknown> = []
196 ) {
197 const cursor = sqlStorage.exec(sql, ...params)
198 const columns = cursor.columnNames
199 for (const result of cursor.raw()) {
200 const obj: any = {}
201 for (let i = 0; i < columns.length; i++) {
202 const value = result[i]
203 Rec.assignProperty(obj, columns[i], value instanceof ArrayBuffer ? new Uint8Array(value) : value)
204 }
205 yield obj
206 }
207 }
208
209 const runStatement = (
210 sql: string,
211 params: ReadonlyArray<unknown> = []
212 ): Effect.Effect<ReadonlyArray<any>, SqlError, never> =>
213 Effect.try({
214 try: () => Array.from(runIterator(sql, params)),
215 catch: (cause) => new SqlError({ reason: classifyError(cause, "Failed to execute statement", "execute") })
216 })
217
218 const runValues = (
219 sql: string,
220 params: ReadonlyArray<unknown> = []
221 ): Effect.Effect<ReadonlyArray<any>, SqlError, never> =>
222 Effect.try({
223 try: () =>
224 Array.from(sqlStorage.exec(sql, ...params).raw(), (row) => {
225 for (let i = 0; i < row.length; i++) {
226 const value = row[i]
227 if (value instanceof ArrayBuffer) {
228 row[i] = new Uint8Array(value) as any
229 }
230 }
231 return row
232 }),
233 catch: (cause) => new SqlError({ reason: classifyError(cause, "Failed to execute statement", "execute") })

Callers 1

layerFunction · 0.70

Calls 10

identityFunction · 0.85
getUnsafeMethod · 0.80
addFinalizerMethod · 0.80
makeMethod · 0.65
withPermitsMethod · 0.65
takeMethod · 0.65
releaseMethod · 0.65
succeedMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…