Skip to content

Commit 15367d1

Browse files
committed
fix: fix processCollectionItems for csv files
1 parent f5e1d21 commit 15367d1

File tree

2 files changed

+25
-7
lines changed

2 files changed

+25
-7
lines changed

src/module.ts

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -323,18 +323,36 @@ async function processCollectionItems(nuxt: Nuxt, collections: ResolvedCollectio
323323
body: content,
324324
path: fullPath,
325325
})
326-
if (parsedContent) {
327-
db.insertDevelopmentCache(keyInCollection, JSON.stringify(parsedContent), checksum)
328-
}
329326
}
330-
327+
331328
// Add manually provided components from the content
332329
if (parsedContent?.__metadata?.components) {
333330
usedComponents.push(...parsedContent.__metadata.components)
334331
}
335332

336-
const { queries, hash } = generateCollectionInsert(collection, parsedContent)
337-
list.push([key, queries, hash])
333+
// Special handling for CSV files
334+
if (parsedContent.__metadata?.rows) {
335+
const rows = parsedContent.__metadata?.rows as Array<Record<string, string>>;
336+
// Since csv files can contain multiple rows, we can't process it as a single ParsedContent
337+
// As for id, priority: `id` field > first column > index
338+
for (let i = 0; i < rows.length; i++) {
339+
const rowid = rows[i].id || rows[i][Object.keys(rows[i])[0]] || String(i)
340+
const rowContent = {
341+
id: parsedContent.id + "/" + rowid,
342+
...rows[i]
343+
}
344+
db.insertDevelopmentCache(parsedContent.id + "/" + rowid, JSON.stringify(parsedContent), checksum)
345+
const { queries, hash } = generateCollectionInsert(collection, rowContent)
346+
list.push([key, queries, hash])
347+
}
348+
}
349+
else {
350+
if (parsedContent) {
351+
db.insertDevelopmentCache(keyInCollection, JSON.stringify(parsedContent), checksum)
352+
}
353+
const { queries, hash } = generateCollectionInsert(collection, parsedContent)
354+
list.push([key, queries, hash])
355+
}
338356
}
339357
catch (e: unknown) {
340358
logger.warn(`"${keyInCollection}" is ignored because parsing is failed. Error: ${e instanceof Error ? e.message : 'Unknown error'}`)

src/utils/content/transformers/csv/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ export default defineTransformer({
5555

5656
return {
5757
id: file.id,
58-
body: result,
58+
__metadata: { rows: result },
5959
}
6060
},
6161
})

0 commit comments

Comments
 (0)