You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -57,9 +57,9 @@ You can see an example project at https://github.com/gatsbyjs/gatsby/tree/master
57
57
58
58
## Parsing algorithm
59
59
60
-
Each row is converted into a node with CSV headers as the keys.
60
+
By default each row is converted into a node with CSV headers as the keys.
61
61
62
-
So if your project has a `letters.csv` with
62
+
If your project has a `letters.csv` with:
63
63
64
64
```
65
65
letter,value
@@ -68,7 +68,7 @@ b,66
68
68
c,67
69
69
```
70
70
71
-
the following three nodes would be created.
71
+
The following three nodes would be created:
72
72
73
73
```json
74
74
[
@@ -78,9 +78,149 @@ the following three nodes would be created.
78
78
]
79
79
```
80
80
81
+
Alternatively the `typeName` plugin option can be used to modify this behaviour.
82
+
83
+
Its arguments are either a string denoting the type name or a function that accepts an argument object of `{ node, object }` which should return the string type name.
`typeNameFromFile` will produce a type per CSV file. When the `typeName` plugin option is undefined, this is the default case. A file name of `letters.csv` will produce a type of `LettersCsv`.
92
+
93
+
`typeNameFromDir` will produce a type per folder of CSVs. A folder called `things` containing CSVs will return a type of `ThingsCsv`.
94
+
95
+
As an example of a custom function, if the CSVs are in a group of folders, and you wish to create a group per folder with the suffix "Data". In this case a folder called `things` containing CSVs will return a type of `ThingsData`.
The `nodePerFile` plugin option can either be `false`, which creates a node per line like above, `true`, which creates a node per file, with the key `items` containing the content, or a string which is the key containing the content.
138
+
139
+
For example, if there are a series of csv files called `vegetables.csv`, `grains.csv`, the following config would produce the following result.
140
+
141
+
The config:
142
+
143
+
```javascript
144
+
// In your gatsby-config.js
145
+
module.exports= {
146
+
plugins: [
147
+
{
148
+
resolve:`gatsby-transformer-csv`,
149
+
options: {
150
+
typeName: () =>`Foodstuffs`,
151
+
nodePerFile:`ingredients`,
152
+
},
153
+
},
154
+
],
155
+
}
156
+
```
157
+
158
+
A query:
159
+
160
+
```graphql
161
+
{
162
+
allFoodstuffs {
163
+
nodes {
164
+
ingredients {
165
+
ingredient
166
+
amount
167
+
}
168
+
parent {
169
+
...onFile {
170
+
name
171
+
}
172
+
}
173
+
}
174
+
}
175
+
}
176
+
```
177
+
178
+
The result:
179
+
180
+
```json
181
+
{
182
+
"data": {
183
+
"allFoodstuffs": {
184
+
"nodes": [
185
+
{
186
+
"parent": {
187
+
"name": "vegetables"
188
+
},
189
+
"ingredients": [
190
+
{
191
+
"ingredient": "potato",
192
+
"amount": 32
193
+
},
194
+
{
195
+
"ingredient": "lettuce",
196
+
"amount": 12
197
+
}
198
+
]
199
+
},
200
+
{
201
+
"parent": {
202
+
"name": "grains"
203
+
},
204
+
"ingredients": [
205
+
{
206
+
"ingredient": "barley",
207
+
"amount": 2
208
+
},
209
+
{
210
+
"ingredient": "wheat",
211
+
"amount": 42
212
+
}
213
+
]
214
+
}
215
+
]
216
+
}
217
+
}
218
+
}
219
+
```
220
+
81
221
## How to query
82
222
83
-
You'd be able to query your letters like:
223
+
In the default configuration, items can be queried like this:
84
224
85
225
```graphql
86
226
{
@@ -97,28 +237,28 @@ You'd be able to query your letters like:
0 commit comments