(
options:
& PgliteClientConfig.Base
& {
readonly liveClient: PGliteInterface
}
)
| 205 | * @since 4.0.0 |
| 206 | */ |
| 207 | export const fromClient = ( |
| 208 | options: |
| 209 | & PgliteClientConfig.Base |
| 210 | & { |
| 211 | readonly liveClient: PGliteInterface |
| 212 | } |
| 213 | ): Effect.Effect<PgliteClient & Client.SqlClient, SqlError, Scope.Scope | Reactivity.Reactivity> => |
| 214 | Effect.gen(function*() { |
| 215 | const pglite = options.liveClient |
| 216 | const compiler = makeCompiler(options.transformQueryNames, options.transformJson) |
| 217 | const transformRows = options.transformResultNames |
| 218 | ? Statement.defaultTransforms(options.transformResultNames, options.transformJson).array |
| 219 | : undefined |
| 220 | |
| 221 | const spanAttributes = Object.entries({ |
| 222 | [ATTR_DB_SYSTEM_NAME]: "postgresql", |
| 223 | ...options.spanAttributes |
| 224 | }) |
| 225 | |
| 226 | const connection = new PgliteConnection(pglite) |
| 227 | const semaphore = Semaphore.makeUnsafe(1) |
| 228 | // Statements, streams and transactions hold the permit until their scope |
| 229 | // closes, so no other fiber can use the connection while they run. |
| 230 | const acquirer = Effect.as( |
| 231 | Effect.acquireRelease(semaphore.take(1), () => semaphore.release(1), { interruptible: true }), |
| 232 | connection |
| 233 | ) |
| 234 | |
| 235 | const config = options as PgliteClientConfig |
| 236 | const client = yield* Client.make({ |
| 237 | driver: "pglite", |
| 238 | acquirer, |
| 239 | compiler, |
| 240 | releaseSavepoint: (name) => `RELEASE SAVEPOINT ${name}`, |
| 241 | spanAttributes, |
| 242 | transformRows |
| 243 | }) |
| 244 | |
| 245 | return Object.assign( |
| 246 | client, |
| 247 | { |
| 248 | [TypeId]: TypeId as TypeId, |
| 249 | config, |
| 250 | pglite, |
| 251 | json: (_: unknown) => Statement.fragment([PgJson(_)]), |
| 252 | listen: Effect.fnUntraced(function*(channel: string) { |
| 253 | const queue = yield* Queue.unbounded<string>() |
| 254 | yield* Effect.acquireRelease( |
| 255 | Effect.tryPromise({ |
| 256 | try: () => |
| 257 | pglite.listen(channel, (payload) => { |
| 258 | Queue.offerUnsafe(queue, payload) |
| 259 | }), |
| 260 | catch: (cause) => new SqlError({ reason: classifyError(cause, "Failed to listen", "listen") }) |
| 261 | }), |
| 262 | (unlisten) => Effect.promise(() => unlisten()), |
| 263 | { interruptible: true } |
| 264 | ) |
no test coverage detected