Skip to content

Commit 47b0bee

Browse files
authored
docs: improve the csv documentation to align with the other pages (#3388)
1 parent b01e307 commit 47b0bee

File tree

1 file changed

+74
-29
lines changed

1 file changed

+74
-29
lines changed

docs/content/docs/3.files/4.csv.md

Lines changed: 74 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,55 +3,99 @@ title: CSV
33
description: How to define, write and query CSV data.
44
---
55

6-
Nuxt Content supports CSV files out of the box. You can store and query data in CSV format, with options for JSON conversion and custom delimiters.
7-
8-
## Configuration
9-
10-
You can configure how CSV files are parsed in your `nuxt.config.ts`:
11-
12-
```ts [nuxt.config.ts]
13-
export default defineNuxtConfig({
14-
content: {
15-
build: {
16-
csv: {
17-
// Convert CSV data to JSON objects
18-
json: true,
19-
// Specify custom delimiter (default is ',')
20-
delimiter: ','
21-
}
22-
}
6+
## Define Collection
7+
8+
```ts [content.config.ts]
9+
import { defineCollection, defineContentConfig, z } from '@nuxt/content'
10+
11+
export default defineContentConfig({
12+
collections: {
13+
authors: defineCollection({
14+
type: 'data',
15+
source: 'authors/**.csv',
16+
schema: z.object({
17+
name: z.string(),
18+
email: z.string(),
19+
avatar: z.string()
20+
})
21+
})
2322
}
2423
})
24+
2525
```
2626

27-
## Example Usage
27+
## Create `.csv` files
2828

29-
Create a CSV file in your content directory:
29+
Create author files in `content/authors/` directory.
3030

31-
```csv [content/users.csv]
31+
::code-group
32+
```csv [users.csv]
3233
id,name,email
3334
3435
2,Jane Smith,[email protected]
36+
3,Alice Johnson,[email protected]
3537
```
3638

37-
Query the data in your components:
39+
```csv [team.csv]
40+
name,role,avatar
41+
John Doe,Developer,https://avatars.githubusercontent.com/u/1?v=4
42+
Jane Smith,Designer,https://avatars.githubusercontent.com/u/2?v=4
43+
```
44+
::
45+
46+
::warning
47+
Each CSV file should have a header row that defines the column names, which will be used as object keys when parsed.
48+
::
49+
50+
## Query Data
51+
52+
Now we can query authors:
3853

3954
```vue
40-
<script setup>
41-
const { data } = await useAsyncData('users', () =>
42-
queryCollection('users').find()
43-
)
55+
<script lang="ts" setup>
56+
// Find a single author
57+
const { data: author } = await useAsyncData('john-doe', () => {
58+
return queryCollection('authors')
59+
.where('name', '=', 'John Doe')
60+
.first()
61+
})
62+
63+
// Get all authors
64+
const { data: authors } = await useAsyncData('authors', () => {
65+
return queryCollection('authors')
66+
.order('name', 'ASC')
67+
.all()
68+
})
4469
</script>
4570
4671
<template>
4772
<ul>
48-
<li v-for="user in data" :key="user.id">
49-
{{ user.name }} ({{ user.email }})
73+
<li v-for="author in authors" :key="author.id">
74+
{{ author.name }} ({{ author.email }})
5075
</li>
5176
</ul>
5277
</template>
5378
```
5479

80+
## Configuration
81+
82+
You can configure how CSV files are parsed in your `nuxt.config.ts`:
83+
84+
```ts [nuxt.config.ts]
85+
export default defineNuxtConfig({
86+
content: {
87+
build: {
88+
csv: {
89+
// Convert CSV data to JSON objects
90+
json: true,
91+
// Specify custom delimiter (default is ',')
92+
delimiter: ','
93+
}
94+
}
95+
}
96+
})
97+
```
98+
5599
With `json: true` in the configuration, each row will be converted to a JavaScript object with the header row used as keys:
56100

57101
```json
@@ -87,11 +131,12 @@ export default defineNuxtConfig({
87131

88132
This would parse CSV files like:
89133

90-
```csv
134+
```csv [semicolon-data.csv]
91135
id;name;email
92136
137+
2;Jane Smith;[email protected]
93138
```
94139

95140
::note
96141
The CSV parser can be disabled by setting `csv: false` in the configuration if you don't need CSV support.
97-
::
142+
::

0 commit comments

Comments
 (0)