Files
Epicnabbo-Catalogus-Updated…/src/lib/prisma-facade-relations.test.ts
T

51 lines
1.2 KiB
TypeScript

import { describe, expect, it } from "vitest";
/**
* Mirrors the many-relation grouping fix in prisma-facade queryRelationRows.
* Keeps the contract local so we don't export private helpers.
*/
function groupRelationRows(
rows: Array<{ __fk: string; id: number }>,
mode: "one" | "many",
): Map<string, unknown> {
const map = new Map<string, unknown>();
for (const row of rows) {
const key = String(row.__fk);
const cleaned = { id: row.id };
if (mode === "many") {
const list = map.get(key);
if (Array.isArray(list)) list.push(cleaned);
else map.set(key, [cleaned]);
} else if (!map.has(key)) {
map.set(key, cleaned);
}
}
return map;
}
describe("prisma-facade relation grouping", () => {
it("accumulates many rows per parent key", () => {
const map = groupRelationRows(
[
{ __fk: "1", id: 10 },
{ __fk: "1", id: 11 },
{ __fk: "2", id: 20 },
],
"many",
);
expect(map.get("1")).toEqual([{ id: 10 }, { id: 11 }]);
expect(map.get("2")).toEqual([{ id: 20 }]);
});
it("keeps a single row for one relations", () => {
const map = groupRelationRows(
[
{ __fk: "1", id: 10 },
{ __fk: "1", id: 11 },
],
"one",
);
expect(map.get("1")).toEqual({ id: 10 });
});
});