@@ -3,55 +3,99 @@ title: CSV
3
3
description : How to define, write and query CSV data.
4
4
---
5
5
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
+ })
23
22
}
24
23
})
24
+
25
25
```
26
26
27
- ## Example Usage
27
+ ## Create ` .csv ` files
28
28
29
- Create a CSV file in your content directory:
29
+ Create author files in ` content/authors/ ` directory.
30
30
31
- ``` csv [content/users.csv]
31
+ :: code-group
32
+ ``` csv [users.csv]
32
33
id,name,email
33
34
34
35
36
+
35
37
```
36
38
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:
38
53
39
54
``` 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
+ })
44
69
</script>
45
70
46
71
<template>
47
72
<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 }})
50
75
</li>
51
76
</ul>
52
77
</template>
53
78
```
54
79
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
+
55
99
With ` json: true ` in the configuration, each row will be converted to a JavaScript object with the header row used as keys:
56
100
57
101
``` json
@@ -87,11 +131,12 @@ export default defineNuxtConfig({
87
131
88
132
This would parse CSV files like:
89
133
90
- ``` csv
134
+ ``` csv [semicolon-data.csv]
91
135
id;name;email
92
136
137
+
93
138
```
94
139
95
140
:: note
96
141
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