fix(csv-loader): handle object foreign keys in reverse lookups

Update `csvToModule` to check if a foreign key value is an object
containing the default primary key before converting it to a string.
This ensures correct key mapping when references are resolved as
objects rather than primitive IDs.
This commit is contained in:
2026-04-19 23:58:09 +08:00
parent 5f941aba10
commit c3572a5b56
+4 -2
View File
@@ -1199,7 +1199,8 @@ export function csvToModule(
reverseLookupInits.push(
`const ${revLookupVar} = new Map();`,
`for (const r of _raw) {`,
` const k = String(r.${decl.foreignKey});`,
` const kv = r.${decl.foreignKey};`,
` const k = String(typeof kv === "object" && "${defaultPrimaryKey}" in kv ? kv.${defaultPrimaryKey} : kv);`,
` if (!${revLookupVar}.has(k)) ${revLookupVar}.set(k, []);`,
` ${revLookupVar}.get(k).push(r);`,
`}`,
@@ -1209,7 +1210,8 @@ export function csvToModule(
reverseLookupInits.push(
`const ${revLookupVar} = new Map();`,
`for (const r of ${varName}()) {`,
` const k = String(r.${decl.foreignKey});`,
` const kv = r.${decl.foreignKey};`,
` const k = String(typeof kv === "object" && "${defaultPrimaryKey}" in kv ? kv.${defaultPrimaryKey} : kv);`,
` if (!${revLookupVar}.has(k)) ${revLookupVar}.set(k, []);`,
` ${revLookupVar}.get(k).push(r);`,
`}`,