MCPcopy Create free account
hub / github.com/Effect-TS/effect / PinnedPgConnection

Class PinnedPgConnection

packages/sql/pg/src/PgConnection.ts:1038–1120  ·  view source on GitHub ↗

* The view of a session returned by `pin`: statements skip the ownership * queue and re-pinning is a no-op, making `pin` reentrant for `stream` and * `listen` running inside a transaction.

Source from the content-addressed store, hash-verified

1036 * `listen` running inside a transaction.
1037 */
1038class PinnedPgConnection implements PgConnection {
1039 readonly [TypeId]: TypeId = TypeId
1040 readonly base: PgConnectionImpl
1041 readonly [internalsKey]: ConnectionInternals
1042 readonly pin: Effect.Effect<PinnedPgConnection, never, Scope.Scope>
1043 released = false
1044 listeners = 0
1045 unlistening = 0
1046 /** No transaction was open when the pin took the session. */
1047 readonly startedIdle: boolean
1048
1049 constructor(base: PgConnectionImpl) {
1050 this.base = base
1051 this[internalsKey] = base[internalsKey]
1052 this.pin = Effect.succeed(this)
1053 this.startedIdle = base.transactionStatus === "I"
1054 }
1055
1056 // Check the lease under the wire permit so queued work cannot run after release.
1057 private cycle(
1058 encode: EncodeCycle,
1059 output: Output,
1060 cache: PreparedCache | undefined
1061 ): Effect.Effect<QueryOutput, SqlError> {
1062 return this.base.wire.withPermit(Effect.suspend(() =>
1063 this.released
1064 ? Effect.fail(pinReleasedError())
1065 : this.base.cycleOwned(encode, output, cache)
1066 ))
1067 }
1068
1069 readonly interrupt: Effect.Effect<void> = Effect.suspend(() => this.released ? Effect.void : this.base.cancel)
1070
1071 get config(): Config {
1072 return this.base.config
1073 }
1074
1075 get processId(): number {
1076 return this.base.processId
1077 }
1078
1079 readonly query = (sql: string, params?: ReadonlyArray<unknown>, prepare = true): Effect.Effect<Result, SqlError> =>
1080 Effect.map(
1081 this.cycle(this.base.queryCycle(sql, params ?? emptyParams), "rows", prepare ? this.base.prepared : undefined),
1082 takeResult
1083 )
1084
1085 readonly queryValues = (
1086 sql: string,
1087 params?: ReadonlyArray<unknown>,
1088 prepare = true
1089 ): Effect.Effect<ReadonlyArray<ReadonlyArray<unknown>>, SqlError> =>
1090 Effect.map(
1091 this.cycle(this.base.queryCycle(sql, params ?? emptyParams), "values", prepare ? this.base.prepared : undefined),
1092 takeValues
1093 )
1094
1095 readonly queryTextValues = (

Callers

nothing calls this directly

Calls 6

cycleMethod · 0.95
streamRowsFunction · 0.85
listenChannelFunction · 0.85
sliceMethod · 0.80
mapMethod · 0.45
succeedMethod · 0.45

Tested by

no test coverage detected