When you point a collection to a single CSV file (instead of a glob), Nuxt Content treats each data row as a separate item in the collection.
source to the path of a single .csv file.body array).#<rowNumber>, where #1 is the first data row after the header.import { defineCollection, defineContentConfig } from '@nuxt/content'
import { z } from 'zod'
export default defineContentConfig({
collections: {
people: defineCollection({
type: 'data',
source: 'org/people.csv',
schema: z.object({
name: z.string(),
email: z.string().email()
})
})
}
})
name,email
Alice,alice@example.com
Bob,bob@example.com
Each row produces its own item. For example, the first data row will have an ID ending with #1 and the second with #2. You can query by any column:
const { data: alice } = await useAsyncData('alice', () =>
queryCollection('people')
.where('email', '=', 'alice@example.com')
.first()
)
const { data: allPeople } = await useAsyncData('all-people', () =>
queryCollection('people')
.order('name', 'ASC')
.all()
)
body).body, use a glob source like org/**.csv instead of a single file.
:::*/**.csv as source in configuration, Nuxt Content will treat them differently from single-file collections.body field in item object as an array.import { defineCollection, defineContentConfig } from '@nuxt/content'
import { z } from 'zod'
export default defineContentConfig({
collections: {
charts: defineCollection({
type: 'data',
source: 'charts/**.csv',
schema: z.object({
// Body is important in CSV files, without body field you cannot access to data array
body: z.array(z.object({
label: z.string(),
value: z.number()
}))
})
})
}
})
content/charts/ directory.label,value
A,100
B,200
C,300
label,value
Foo,123
Bar,456
Baz,789
<script lang="ts" setup>
// Find a single chart
const { data: chart1 } = await useAsyncData('chart1', () => {
return queryCollection('charts')
.where('id', '=', 'charts/charts/chart1.csv')
.first()
})
// Get all charts
const { data: charts } = await useAsyncData('charts', () => {
return queryCollection('charts')
.order('id', 'ASC')
.all()
})
</script>
<template>
<ul>
<li v-for="chart in charts" :key="chart.id">
<!-- CSV data are in `chart.body` as an array -->
<p v-for="data in chart.body">
{{ data.label }} - {{ data.value }}
</p>
</li>
</ul>
</template>
nuxt.config.ts:export default defineNuxtConfig({
content: {
build: {
csv: {
// Convert CSV data to JSON objects
json: true,
// Specify custom delimiter (default is ',')
delimiter: ','
}
}
}
})
json: true in the configuration, each row will be converted to a JavaScript object with the header row used as keys:[
{
"id": "1",
"name": "John Doe",
"email": "john@example.com"
},
{
"id": "2",
"name": "Jane Smith",
"email": "jane@example.com"
}
]
export default defineNuxtConfig({
content: {
build: {
csv: {
delimiter: ';' // Use semicolon as delimiter
}
}
}
})
id;name;email
1;John Doe;john@example.com
2;Jane Smith;jane@example.com
csv: false in the configuration if you don't need CSV support.