content icon indicating copy to clipboard operation
content copied to clipboard

fix: fix csv file collections

Open Jasonzyt opened this issue 4 months ago β€’ 18 comments

πŸ”— Linked issue

Close #3511

❓ Type of change

  • [ ] πŸ“– Documentation (updates to the documentation or readme)
  • [x] 🐞 Bug fix (a non-breaking change that fixes an issue)
  • [ ] πŸ‘Œ Enhancement (improving an existing functionality like performance)
  • [ ] ✨ New feature (a non-breaking change that adds functionality)
  • [ ] ⚠️ Breaking change (fix or feature that would cause existing functionality to change)

πŸ“š Description

Currently, CSV collections don’t behave as described in the documentation. Instead of allowing each CSV file to contain multiple entries (as the documentation states), the current implementation treats each CSV file as a single piece of content.

πŸ“ Checklist

  • [x] I have linked an issue or discussion.
  • [ ] I have updated the documentation accordingly.

Jasonzyt avatar Aug 24 '25 12:08 Jasonzyt

@Jasonzyt is attempting to deploy a commit to the NuxtLabs Team on Vercel.

A member of the Team first needs to authorize it.

vercel[bot] avatar Aug 24 '25 12:08 vercel[bot]

npm i https://pkg.pr.new/@nuxt/content@3513

commit: 364b2d8

pkg-pr-new[bot] avatar Aug 24 '25 12:08 pkg-pr-new[bot]

Thanks for the PR @Jasonzyt It shame that I've missed this changes in documentation update. Documentation is wrong. But this behavior is not planned to support. Instead module could support this behavior only for single file CSV collections.

people: defineCollection({
    type: "data",
    source: "org/people.csv",
    schema: z.object({
      "name": z.string(),
      "email": z.string().email(),
    }),
  }),

It is important to create 1 to 1 and predictable mapping between files in content directory and documents in database. Splitting CSV files outside of collection source login, breaks this predictability.

farnabaz avatar Sep 12 '25 12:09 farnabaz

In my case, I only need single file CSV support. I agree with you, so documentation should be updated.

My PR can implement single CSV support, but tbh some logic needs to be optimized. And I think CSV data type support (currently only string) should be added too.

Jasonzyt avatar Sep 12 '25 12:09 Jasonzyt

I'll update your PR, we can test and improve

farnabaz avatar Sep 12 '25 12:09 farnabaz

@Jasonzyt Could you check and test the behavior? We need to update docs to document both single file and multi file behaviors

farnabaz avatar Sep 12 '25 13:09 farnabaz

And the facts prove this point: after installing the latest package, errors occurred.

In my case

const { data } = await useAsyncData("albumsMeta", () => {
  return queryCollection("albumsMeta").order("updated", "DESC").all();
});

returns

[ { id: 'albumsMeta/albums-meta/albums-meta.csv',                                                                                                                   
    cover: null,
    description: null,
    extension: 'csv',
    meta: { path: '/albums-meta/albums-meta', body: [Array], title: 'Albums Meta' },
    name: null,
    stem: 'albums-meta/albums-meta',
    updated: null,
    urlFormat: null,
    __hash__: 'qZyKG0MlhJb1M0gDHO6L8sGT9FxtlwB_nASlz1K0ooM' } ]

This is exactly the bug mentioned by #3511

Jasonzyt avatar Sep 12 '25 15:09 Jasonzyt

@Jasonzyt could you share your reproduction repository? or provide minimal one?

farnabaz avatar Sep 12 '25 15:09 farnabaz

Here's my repo: https://github.com/Jasonzyt/gallery

Jasonzyt avatar Sep 12 '25 15:09 Jasonzyt

@Jasonzyt There was a mistake in a string, should be good now. Try with https://pkg.pr.new/@nuxt/content@0f6d610

farnabaz avatar Sep 12 '25 16:09 farnabaz

I installed npm i https://pkg.pr.new/@nuxt/content@3513 and it worked fine. But after few hours when i am installing this package in my different project it give the same error as earlier.

Edit: And after installing npm i https://pkg.pr.new/@nuxt/content@0f6d610 its giving the same problem

tazim404 avatar Sep 12 '25 18:09 tazim404

Please solve this problem urgently. I have projects to submit

tazim404 avatar Sep 12 '25 20:09 tazim404

@tazim404 Try https://pkg.pr.new/@nuxt/content@c1cefd2 ? c1cefd2 works for me

but the later changes broke it

Jasonzyt avatar Sep 13 '25 15:09 Jasonzyt

@Jasonzyt There was a mistake in a string, should be good now. Try with https://pkg.pr.new/@nuxt/content@0f6d610

@farnabaz Still the same bug behaved like #3511

Jasonzyt avatar Sep 13 '25 15:09 Jasonzyt

@Jasonzyt I checked with your projects and it works as expected with single file sources. Screenshot 2025-09-15 at 11 59 05

try with npm i https://pkg.pr.new/@nuxt/content@f790439

Note: I had to remove styles and some other deps because I was facing issue with oxc in you repo Note2: It is not recommended to use useAsyncData outside of script setup. this utility is designed to use directly in script

farnabaz avatar Sep 15 '25 10:09 farnabaz

Okay, I found the problem: You can't use **.csv in the content.config.ts

export default defineContentConfig({
  collections: {
    albumsMeta: defineCollection({
      type: "data",
+      source: "test.csv",
-      source: "test/**.csv",
      schema: z.object({
        id: z.string(),
        name: z.string(),
        description: z.string(),
        cover: z.string(),
        updated: z.string(),
        urlFormat: z.string(),
      }),
    }),
    ...defineAlbumCollections(),
  },
});

Now everything works well!

@farnabaz Thanks for the review! I think we should complete docs before merging

Jasonzyt avatar Oct 26 '25 01:10 Jasonzyt

@farnabaz It seems there's some bug parsing multiple .csv files Single .csv file works well The currently behavior is not matched with the newly updated docs.

Jasonzyt avatar Oct 26 '25 02:10 Jasonzyt

@farnabaz Okay now I understand everything. Sry for my misunderstand of your newly updated docs. TBH some code is really confusing. I spent hours reading it and finally figured it out. I just reorganized the docs and make it clearer. Now I think the PR is ready to merge! Thanks for your work.

Jasonzyt avatar Nov 15 '25 20:11 Jasonzyt