Skip to content

prepareLoaderResult

function prepareLoaderResult<TKey, TDBResult, TFinalResult>(options): Promise<TFinalResult[][]>;

Defined in: prepare-loader-result.ts:117

Map a batch of DB results back to the original keys in order.

DataLoader requires the results array to be in the same order as the keys. This utility handles the common pattern of: query returns unordered rows → map each row to its key → return results aligned with the original key order.

Supports:

  • One-to-one (default): one result per key
  • One-to-many (mode: "many"): multiple results per key (returns arrays)
  • Async mapValue: transform rows with async operations
  • Custom defaultValue: fallback for missing keys (instead of undefined)
  • Composite keys via keyToString

Without getKey, a row’s key is row.id, else row.uid; a row with neither makes the call throw. Always returns a Promise (because mapValue may be async).

Type Parameter Default type
TKey -
TDBResult -
TFinalResult TDBResult
Parameter Type
options PrepareLoaderResultManyOptions<TKey, TDBResult, TFinalResult>

Promise<TFinalResult[][]>

Align rows with keys

import { prepareLoaderResult } from "@xtandard/lib";
const keys = [3, 1, 2];
const rows = [
{ id: 1, name: "Ada" },
{ id: 2, name: "Linus" },
];
await prepareLoaderResult({ keys, results: rows });
// [undefined, { id: 1, name: "Ada" }, { id: 2, name: "Linus" }]

Inside a DataLoader batch function (rows keyed by id or uid)

const loader = createLoader<User | undefined, Ctx>(async ({ keys }) => {
const rows = await db.users.findMany({ where: { id: { in: [...keys] } } });
return prepareLoaderResult({ keys, results: rows });
});

One-to-many

return prepareLoaderResult({
keys,
results: rows,
mode: "many",
getKey: (row) => row.userId,
});
// Returns: [Photo[], Photo[], ...] — one array per key

With async mapValue

return prepareLoaderResult({
keys,
results: rows,
mapValue: async (row) => ({
...row,
permissions: await loadPermissions({ context, id: row.roleId }),
}),
});

With defaultValue

return prepareLoaderResult({
keys,
results: rows,
defaultValue: null,
});
// Missing keys return null instead of undefined

Composite keys

await prepareLoaderResult({
keys: [
{ org: "x", slug: "home" },
{ org: "y", slug: "home" },
],
results: [{ org: "y", slug: "home", title: "Y home" }],
getKey: (row) => ({ org: row.org, slug: row.slug }),
keyToString: (key) => `${key.org}:${key.slug}`, // match object keys by value
mapValue: (row) => row.title,
});
// [undefined, "Y home"]
function prepareLoaderResult<TKey, TDBResult, TFinalResult, TDefault>(options): Promise<(TFinalResult | TDefault)[]>;

Defined in: prepare-loader-result.ts:121

Map a batch of DB results back to the original keys in order.

DataLoader requires the results array to be in the same order as the keys. This utility handles the common pattern of: query returns unordered rows → map each row to its key → return results aligned with the original key order.

Supports:

  • One-to-one (default): one result per key
  • One-to-many (mode: "many"): multiple results per key (returns arrays)
  • Async mapValue: transform rows with async operations
  • Custom defaultValue: fallback for missing keys (instead of undefined)
  • Composite keys via keyToString

Without getKey, a row’s key is row.id, else row.uid; a row with neither makes the call throw. Always returns a Promise (because mapValue may be async).

Type Parameter Default type
TKey -
TDBResult -
TFinalResult TDBResult
TDefault undefined
Parameter Type
options PrepareLoaderResultOneOptions<TKey, TDBResult, TFinalResult, TDefault>

Promise<(TFinalResult | TDefault)[]>

Align rows with keys

import { prepareLoaderResult } from "@xtandard/lib";
const keys = [3, 1, 2];
const rows = [
{ id: 1, name: "Ada" },
{ id: 2, name: "Linus" },
];
await prepareLoaderResult({ keys, results: rows });
// [undefined, { id: 1, name: "Ada" }, { id: 2, name: "Linus" }]

Inside a DataLoader batch function (rows keyed by id or uid)

const loader = createLoader<User | undefined, Ctx>(async ({ keys }) => {
const rows = await db.users.findMany({ where: { id: { in: [...keys] } } });
return prepareLoaderResult({ keys, results: rows });
});

One-to-many

return prepareLoaderResult({
keys,
results: rows,
mode: "many",
getKey: (row) => row.userId,
});
// Returns: [Photo[], Photo[], ...] — one array per key

With async mapValue

return prepareLoaderResult({
keys,
results: rows,
mapValue: async (row) => ({
...row,
permissions: await loadPermissions({ context, id: row.roleId }),
}),
});

With defaultValue

return prepareLoaderResult({
keys,
results: rows,
defaultValue: null,
});
// Missing keys return null instead of undefined

Composite keys

await prepareLoaderResult({
keys: [
{ org: "x", slug: "home" },
{ org: "y", slug: "home" },
],
results: [{ org: "y", slug: "home", title: "Y home" }],
getKey: (row) => ({ org: row.org, slug: row.slug }),
keyToString: (key) => `${key.org}:${key.slug}`, // match object keys by value
mapValue: (row) => row.title,
});
// [undefined, "Y home"]