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

Function make

repos/effect/packages/sql/sqlite-react-native/src/SqliteClient.ts:121–225  ·  view source on GitHub ↗
(
  options: SqliteClientConfig
)

Source from the content-addressed store, hash-verified

119 * @since 4.0.0
120 */
121export const make = (
122 options: SqliteClientConfig
123): Effect.Effect<SqliteClient, never, Scope.Scope | Reactivity.Reactivity> =>
124 Effect.gen(function*() {
125 const clientOptions: Parameters<typeof Sqlite.open>[0] = {
126 name: options.filename
127 }
128 if (options.location) {
129 clientOptions.location = options.location
130 }
131 if (options.encryptionKey) {
132 clientOptions.encryptionKey = options.encryptionKey
133 }
134
135 const compiler = Statement.makeCompilerSqlite(options.transformQueryNames)
136 const transformRows = options.transformResultNames ?
137 Statement.defaultTransforms(options.transformResultNames).array :
138 undefined
139
140 const makeConnection = Effect.gen(function*() {
141 const db = Sqlite.open(clientOptions) as DB
142 yield* Effect.addFinalizer(() => Effect.sync(() => db.close()))
143
144 const run = (
145 sql: string,
146 params: ReadonlyArray<unknown> = []
147 ) =>
148 Effect.withFiber<Array<any>, SqlError>((fiber) => {
149 if (fiber.getRef(AsyncQuery)) {
150 return Effect.map(
151 Effect.tryPromise({
152 try: () => db.execute(sql, params as Array<any>),
153 catch: (cause) =>
154 new SqlError({ reason: classifyError(cause, "Failed to execute statement (async)", "execute") })
155 }),
156 (result) => result.rows
157 )
158 }
159 return Effect.try({
160 try: () => db.executeSync(sql, params as Array<any>).rows,
161 catch: (cause) => new SqlError({ reason: classifyError(cause, "Failed to execute statement", "execute") })
162 })
163 })
164
165 const runValues = (
166 sql: string,
167 params: ReadonlyArray<unknown> = []
168 ) =>
169 Effect.withFiber<Array<any>, SqlError>((fiber) => {
170 if (fiber.getRef(AsyncQuery)) {
171 return Effect.tryPromise({
172 try: () => db.executeRaw(sql, params as Array<any>),
173 catch: (cause) =>
174 new SqlError({ reason: classifyError(cause, "Failed to execute statement (async)", "execute") })
175 })
176 }
177 return Effect.try({
178 try: () => db.executeRawSync(sql, params as Array<any>),

Callers 1

layerFunction · 0.70

Calls 10

identityFunction · 0.85
addFinalizerMethod · 0.80
syncMethod · 0.80
closeMethod · 0.80
getUnsafeMethod · 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…