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

Function fromClient

repos/effect/packages/sql/pglite/src/PgliteClient.ts:182–270  ·  view source on GitHub ↗
(
  options:
    & PgliteClientConfig.Base
    & {
      readonly liveClient: PGliteInterface
    }
)

Source from the content-addressed store, hash-verified

180 * @since 4.0.0
181 */
182export const fromClient = (
183 options:
184 & PgliteClientConfig.Base
185 & {
186 readonly liveClient: PGliteInterface
187 }
188): Effect.Effect<PgliteClient, SqlError, Scope.Scope | Reactivity.Reactivity> =>
189 Effect.gen(function*() {
190 const pglite = options.liveClient
191 const compiler = makeCompiler(options.transformQueryNames, options.transformJson)
192 const transformRows = options.transformResultNames
193 ? Statement.defaultTransforms(options.transformResultNames, options.transformJson).array
194 : undefined
195
196 const spanAttributes: Array<[string, unknown]> = [
197 ...(options.spanAttributes ? Object.entries(options.spanAttributes) : []),
198 [ATTR_DB_SYSTEM_NAME, "postgresql"]
199 ]
200
201 const connection = new PgliteConnection(pglite)
202 const semaphore = Semaphore.makeUnsafe(1)
203 const acquirer = semaphore.withPermits(1)(Effect.succeed(connection))
204 const transactionAcquirer = Effect.uninterruptibleMask((restore) => {
205 const fiber = Fiber.getCurrent()!
206 const scope = Context.getUnsafe(fiber.context, Scope.Scope)
207 return Effect.as(
208 Effect.tap(
209 restore(semaphore.take(1)),
210 () => Scope.addFinalizer(scope, semaphore.release(1))
211 ),
212 connection
213 )
214 })
215
216 const config = options as PgliteClientConfig
217 const client = yield* Client.make({
218 acquirer,
219 compiler,
220 transactionAcquirer,
221 spanAttributes,
222 transformRows
223 })
224
225 return Object.assign(
226 client,
227 {
228 [TypeId]: TypeId as TypeId,
229 config,
230 pglite,
231 json: (_: unknown) => Statement.fragment([PgJson(_)]),
232 listen: (channel: string) =>
233 Stream.callback<string, SqlError>((queue) =>
234 Effect.acquireRelease(
235 Effect.tryPromise({
236 try: () =>
237 pglite.listen(channel, (payload) => {
238 Queue.offerUnsafe(queue, payload)
239 }),

Callers 3

makeClientFunction · 0.85
makeFunction · 0.85

Calls 15

escapeLiteralFunction · 0.85
getUnsafeMethod · 0.80
addFinalizerMethod · 0.80
offerUnsafeMethod · 0.80
execMethod · 0.80
withPermitMethod · 0.80
makeCompilerFunction · 0.70
PgJsonInterface · 0.70
classifyErrorFunction · 0.70
withPermitsMethod · 0.65
takeMethod · 0.65
releaseMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…