(
options:
& PgliteClientConfig.Base
& {
readonly liveClient: PGliteInterface
}
)
| 180 | * @since 4.0.0 |
| 181 | */ |
| 182 | export 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 | }), |
no test coverage detected
searching dependent graphs…