MCPcopy
hub / github.com/Effect-TS/effect / makeClient

Function makeClient

packages/sql-pg/src/PgClient.ts:106–393  ·  view source on GitHub ↗
(
  pool: Pg.Pool,
  config: PgClientConfig,
  options: ClientOptions
)

Source from the content-addressed store, hash-verified

104}
105
106const makeClient = (
107 pool: Pg.Pool,
108 config: PgClientConfig,
109 options: ClientOptions
110): Effect.Effect<PgClient, SqlError, Scope.Scope | Reactivity.Reactivity> =>
111 Effect.gen(function*() {
112 const compiler = makeCompiler(
113 options.transformQueryNames,
114 options.transformJson
115 )
116 const transformRows = options.transformResultNames ?
117 Statement.defaultTransforms(
118 options.transformResultNames,
119 options.transformJson
120 ).array :
121 undefined
122
123 class ConnectionImpl implements Connection {
124 readonly pg: Pg.PoolClient | undefined
125 constructor(pg?: Pg.PoolClient) {
126 this.pg = pg
127 }
128
129 private runWithClient<A>(f: (client: Pg.PoolClient, resume: (_: Effect.Effect<A, SqlError>) => void) => void) {
130 if (this.pg !== undefined) {
131 return Effect.async<A, SqlError>((resume) => {
132 f(this.pg!, resume)
133 return makeCancel(pool, this.pg!)
134 })
135 }
136 return Effect.async<A, SqlError>((resume) => {
137 let done = false
138 let cancel: Effect.Effect<void> | undefined = undefined
139 let client: Pg.PoolClient | undefined = undefined
140 function onError(cause: Error) {
141 cleanup(cause)
142 resume(Effect.fail(new SqlError({ cause, message: "Connection error" })))
143 }
144 function cleanup(cause?: Error) {
145 if (!done) client?.release(cause)
146 done = true
147 client?.off("error", onError)
148 }
149 pool.connect((cause, client_) => {
150 if (cause) {
151 return resume(Effect.fail(new SqlError({ cause, message: "Failed to acquire connection" })))
152 } else if (!client_) {
153 return resume(
154 Effect.fail(
155 new SqlError({ message: "Failed to acquire connection", cause: new Error("No client returned") })
156 )
157 )
158 } else if (done) {
159 client_.release()
160 return
161 }
162 client = client_
163 client.once("error", onError)

Callers 2

makeFunction · 0.70
PgClient.tsFile · 0.70

Calls 14

PgJsonInterface · 0.85
unsafeGetMethod · 0.80
syncMethod · 0.80
entriesMethod · 0.80
makeCompilerFunction · 0.70
failMethod · 0.65
addFinalizerMethod · 0.65
mapMethod · 0.65
makeMethod · 0.65
pipeMethod · 0.65
endMethod · 0.65
getMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…