Skip to content

Batcher

Batcher: object

Defined in: Core/Batcher.ts:32

make: <K, E, V>(options) => Batcher<K, E, V> = makeBatcher

Creates a stateful Batcher with the provided configuration options.

K

E

V

Options<K, E, V>

Batcher<K, E, V>

const userBatcher = Batcher.make({
  fetch: (ids: readonly string[]) =>
    pipe(
      db.users.findMany({ where: { id: { in: ids } } }),
      Task.Result.map(Arr.indexBy((u) => u.id)),
    ),
  maxBatchSize: 100,
});

Outcome: object

Synchronous elimination, type guards, and converters for Batcher.Outcome.

getOrElse: <B>(fallback) => <E, V, K>(outcome) => B | V = getOrElseOutcome

Returns the value if BatcherOk, otherwise returns the fallback value.

B

() => B

<E, V, K>(outcome) => B | V

Batcher.Outcome.match to handle all three cases explicitly.

const user = pipe(outcome, Batcher.Outcome.getOrElse(() => defaultGuestUser));

is: object

Type guards for checking Batcher.Outcome variants.

err: <E, V, K>(outcome) => outcome is Err<E> = isErr

Checks whether an outcome is a BatcherErr.

E

V

K

Outcome<E, V, K>

outcome is Err<E>

  • Batcher.Outcome.is.ok to check for a resolved value.
  • Batcher.Outcome.is.missing to check for an absent key.
if (Batcher.Outcome.is.err(outcome)) {
  console.error(outcome.error);
}

missing: <E, V, K>(outcome) => outcome is Missing<K> = isMissing

Checks whether an outcome is a BatcherMissing.

E

V

K

Outcome<E, V, K>

outcome is Missing<K>

  • Batcher.Outcome.is.ok to check for a resolved value.
  • Batcher.Outcome.is.err to check for a query failure.
if (Batcher.Outcome.is.missing(outcome)) {
  console.warn(`Key missing: ${outcome.key}`);
}

ok: <E, V, K>(outcome) => outcome is Ok<V> = isOk

Checks whether an outcome is a BatcherOk.

E

V

K

Outcome<E, V, K>

outcome is Ok<V>

  • Batcher.Outcome.is.err to check for a query failure.
  • Batcher.Outcome.is.missing to check for an absent key.
if (Batcher.Outcome.is.ok(outcome)) {
  console.log(outcome.value);
}

make: object

Constructors for creating Batcher.Outcome variants.

err: <E, V, K>(error) => Outcome<E, V, K> = makeErr

Creates a BatcherErr outcome.

E

V = never

K = never

E

Outcome<E, V, K>

  • Batcher.Outcome.make.ok to construct a successful outcome.
  • Batcher.Outcome.make.missing to construct an absent key outcome.
const err = Batcher.Outcome.make.err(new Error("DB connection lost"));

missing: <K, E, V>(key) => Outcome<E, V, K> = makeMissing

Creates a BatcherMissing outcome.

K

E = never

V = never

K

Outcome<E, V, K>

  • Batcher.Outcome.make.ok to construct a successful outcome.
  • Batcher.Outcome.make.err to construct an error outcome.
const missing = Batcher.Outcome.make.missing("u_99");

ok: <V, E, K>(value) => Outcome<E, V, K> = makeOk

Creates a BatcherOk outcome.

V

E = never

K = never

V

Outcome<E, V, K>

  • Batcher.Outcome.make.err to construct an error outcome.
  • Batcher.Outcome.make.missing to construct an absent key outcome.
const ok = Batcher.Outcome.make.ok({ id: "u1", name: "Alice" });

map: <V, B>(transform) => <E, K>(outcome) => Outcome<E, B, K> = mapOutcome

Transforms the value inside a BatcherOk outcome. Leaves Missing and Err unchanged.

V

B

(value) => B

<E, K>(outcome) => Outcome<E, B, K>

Batcher.Outcome.mapError to transform the error channel instead.

const nameOutcome = pipe(outcome, Batcher.Outcome.map((user) => user.name));

mapError: <E, F>(transform) => <V, K>(outcome) => Outcome<F, V, K> = mapErrorOutcome

Transforms the error inside a BatcherErr outcome. Leaves Ok and Missing unchanged.

E

F

(error) => F

<V, K>(outcome) => Outcome<F, V, K>

Batcher.Outcome.map to transform the value channel instead.

const mapped = pipe(outcome, Batcher.Outcome.mapError((err) => new CustomError(err)));

match: <E, V, K, B>(cases) => (outcome) => B = matchOutcome

Eliminates a Batcher.Outcome by handling all three cases (ok, missing, err).

E

V

K

B

(error) => B

(key) => B

(value) => B

(outcome) => B

Batcher.Outcome.getOrElse to extract the value with a fallback.

const status = pipe(
  outcome,
  Batcher.Outcome.match({
    ok: (user) => 200,
    missing: (key) => 404,
    err: (error) => 500,
  }),
);

to: object

Converters from Batcher.Outcome to standard library and native representations.

Maybe: <E, V, K>(outcome) => Maybe<V> = toMaybeOutcome

Converts a Batcher.Outcome to a Maybe, mapping BatcherOk to Some and both BatcherMissing and BatcherErr to None.

E = never

V = never

K = never

Outcome<E, V, K>

Maybe<V>

Batcher.Outcome.to.Result to convert to a fallible Result.

const maybe = pipe(outcome, Batcher.Outcome.to.Maybe);

nullable: <E, V, K>(outcome) => V | null = toNullableOutcome

Converts a Batcher.Outcome to a nullable value (V | null), mapping BatcherOk to V and both BatcherMissing and BatcherErr to null.

E = never

V = never

K = never

Outcome<E, V, K>

V | null

Batcher.Outcome.to.Maybe to convert to a type-safe Maybe.

const userOrNull = pipe(outcome, Batcher.Outcome.to.nullable);

Result: <K, E2>(onMissing) => <E, V>(outcome) => Result<E2 | E, V> = toResultOutcome

Converts a Batcher.Outcome to a Result, projecting BatcherMissing into a domain error.

K

E2

(key) => E2

<E, V>(outcome) => Result<E2 | E, V>

Batcher.Outcome.to.Maybe to convert to an optional value.

const res = pipe(
  outcome,
  Batcher.Outcome.to.Result((id) => ({ kind: "UserNotFound" as const, id })),
);