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

Function findById

packages/sql/src/SqlResolver.ts:345–416  ·  view source on GitHub ↗
(
  tag: T,
  options:
    | {
      readonly Id: Schema.Schema<I, II, RI>
      readonly Result: Schema.Schema<A, IA>
      readonly ResultId: (result: Types.NoInfer<A>, row: Types.NoInfer<Row>) => I
      readonly execute: (
        requests: Array<Types.NoInfer<II>>
      ) => Effect.Effect<ReadonlyArray<Row>, E>
      readonly withContext?: false
    }
    | {
      readonly Id: Schema.Schema<I, II, RI>
      readonly Result: Schema.Schema<A, IA, RA>
      readonly ResultId: (result: Types.NoInfer<A>, row: Types.NoInfer<Row>) => I
      readonly execute: (
        requests: Array<Types.NoInfer<II>>
      ) => Effect.Effect<ReadonlyArray<Row>, E, R>
      readonly withContext: true
    }
)

Source from the content-addressed store, hash-verified

343 * @category resolvers
344 */
345export const findById = <T extends string, I, II, RI, A, IA, Row, E, RA = never, R = never>(
346 tag: T,
347 options:
348 | {
349 readonly Id: Schema.Schema<I, II, RI>
350 readonly Result: Schema.Schema<A, IA>
351 readonly ResultId: (result: Types.NoInfer<A>, row: Types.NoInfer<Row>) => I
352 readonly execute: (
353 requests: Array<Types.NoInfer<II>>
354 ) => Effect.Effect<ReadonlyArray<Row>, E>
355 readonly withContext?: false
356 }
357 | {
358 readonly Id: Schema.Schema<I, II, RI>
359 readonly Result: Schema.Schema<A, IA, RA>
360 readonly ResultId: (result: Types.NoInfer<A>, row: Types.NoInfer<Row>) => I
361 readonly execute: (
362 requests: Array<Types.NoInfer<II>>
363 ) => Effect.Effect<ReadonlyArray<Row>, E, R>
364 readonly withContext: true
365 }
366): Effect.Effect<SqlResolver<T, I, Option.Option<A>, E, RI>, never, RA | R> => {
367 const decodeResults = Schema.decodeUnknown(Schema.Array(options.Result))
368 const resolver = RequestResolver.makeBatched(
369 (requests: NonEmptyArray<SqlRequest<T, Option.Option<A>, E>>) => {
370 const [inputs, spanLinks, idMap] = partitionRequestsById<I, II>()(requests)
371 return options.execute(inputs as any).pipe(
372 Effect.bindTo("rawResults"),
373 Effect.bind("results", ({ rawResults }) => decodeResults(rawResults)),
374 Effect.flatMap(({ rawResults, results }) =>
375 Effect.forEach(
376 results,
377 (result, i) => {
378 const id = options.ResultId(result, rawResults[i])
379 const request = MutableHashMap.get(idMap, id)
380 if (request._tag === "None") {
381 return Effect.void
382 }
383 MutableHashMap.remove(idMap, id)
384 return Request.succeed(request.value, Option.some(result))
385 },
386 { discard: true }
387 )
388 ),
389 Effect.tap((_) => {
390 if (MutableHashMap.size(idMap) === 0) {
391 return Effect.void
392 }
393 return Effect.forEach(
394 idMap,
395 ([, request]) => Request.succeed(request, Option.none()),
396 { discard: true }
397 )
398 }),
399 Effect.catchAllCause((cause) =>
400 Effect.forEach(
401 requests,
402 (request) => Request.failCause(request, cause),

Callers

nothing calls this directly

Calls 9

partitionRequestsByIdFunction · 0.85
makeResolverFunction · 0.85
failCauseMethod · 0.80
identifiedMethod · 0.65
pipeMethod · 0.65
getMethod · 0.65
removeMethod · 0.65
sizeMethod · 0.65
executeMethod · 0.45

Tested by

no test coverage detected