From f0d7bde81a1bc02f76df29363015e113e12041e3 Mon Sep 17 00:00:00 2001 From: Marcel Samyn Date: Sun, 21 Sep 2025 08:33:32 +0200 Subject: [PATCH] Fix source insertion metadata alignment --- src/lib/sources.ts | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/src/lib/sources.ts b/src/lib/sources.ts index d8f3171..f5a1c0f 100644 --- a/src/lib/sources.ts +++ b/src/lib/sources.ts @@ -76,6 +76,19 @@ export class SourceService { }), ); + const inputLookup = new Map(); + const makeLookupKey = ( + userId: string, + type: SourceType, + externalId: string, + ) => `${userId}:${type}:${externalId}`; + inputs.forEach((input) => { + inputLookup.set( + makeLookupKey(input.userId, input.sourceType, input.externalId), + input, + ); + }); + const inserted = await this.db .insert(sources) .values(insertRows) @@ -85,9 +98,15 @@ export class SourceService { .returning(); // 2. Handle payloads - for (let i = 0; i < inserted.length; i++) { - const row = inserted[i]!; - const input = inputs[i]!; + for (const row of inserted) { + const lookupKey = makeLookupKey(row.userId, row.type, row.externalId); + const input = inputLookup.get(lookupKey); + if (!input) { + console.warn( + `No matching input found for inserted source ${row.id} (${lookupKey})`, + ); + continue; + } const key = `${input.userId}/${row.id}`; // Inline payload if small enough or content provided