Skip to content

Commit db542ba

Browse files
authored
🐛 fix: Fix source insertion metadata alignment (#25)
1 parent 481ff8f commit db542ba

1 file changed

Lines changed: 22 additions & 3 deletions

File tree

src/lib/sources.ts

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,19 @@ export class SourceService {
7676
}),
7777
);
7878

79+
const inputLookup = new Map<string, SourceCreateInput>();
80+
const makeLookupKey = (
81+
userId: string,
82+
type: SourceType,
83+
externalId: string,
84+
) => `${userId}:${type}:${externalId}`;
85+
inputs.forEach((input) => {
86+
inputLookup.set(
87+
makeLookupKey(input.userId, input.sourceType, input.externalId),
88+
input,
89+
);
90+
});
91+
7992
const inserted = await this.db
8093
.insert(sources)
8194
.values(insertRows)
@@ -85,9 +98,15 @@ export class SourceService {
8598
.returning();
8699

87100
// 2. Handle payloads
88-
for (let i = 0; i < inserted.length; i++) {
89-
const row = inserted[i]!;
90-
const input = inputs[i]!;
101+
for (const row of inserted) {
102+
const lookupKey = makeLookupKey(row.userId, row.type, row.externalId);
103+
const input = inputLookup.get(lookupKey);
104+
if (!input) {
105+
console.warn(
106+
`No matching input found for inserted source ${row.id} (${lookupKey})`,
107+
);
108+
continue;
109+
}
91110
const key = `${input.userId}/${row.id}`;
92111

93112
// Inline payload if small enough or content provided

0 commit comments

Comments
 (0)