Skip to content

Commit fc3572a

Browse files
Mike-Daxpieh
authored andcommitted
feat(gatsby-transformer-csv): support customizing node type names (#18104)
* feat: Added additional options for transformer-csv * chore: Linting improvements * bug: Fixed default if plugin options are empty * chore: Tests for transformer-csv * chore: Lint fixes for transformer-csv * refactor: Improved transformer-csv API surface * refactor: Moved exports for transformer-csv default functions * chore: Fixed file permission problem with transformer-csv
1 parent d3d9020 commit fc3572a

File tree

7 files changed

+1309
-48
lines changed

7 files changed

+1309
-48
lines changed
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
11
/*.js
2-
!index.js

packages/gatsby-transformer-csv/README.md

Lines changed: 160 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,9 @@ You can see an example project at https://github.com/gatsbyjs/gatsby/tree/master
5757

5858
## Parsing algorithm
5959

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.
6161

62-
So if your project has a `letters.csv` with
62+
If your project has a `letters.csv` with:
6363

6464
```
6565
letter,value
@@ -68,7 +68,7 @@ b,66
6868
c,67
6969
```
7070

71-
the following three nodes would be created.
71+
The following three nodes would be created:
7272

7373
```json
7474
[
@@ -78,9 +78,149 @@ the following three nodes would be created.
7878
]
7979
```
8080

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.
84+
85+
Two predefined functions are provided.
86+
87+
```javascript
88+
const { typeNameFromDir, typeNameFromFile } = require("gatsby-transformer-csv")
89+
```
90+
91+
`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`.
96+
97+
```javascript
98+
// In your gatsby-config.js
99+
const _ = require(`lodash`)
100+
const path = require(`path`)
101+
102+
module.exports = {
103+
plugins: [
104+
{
105+
resolve: `gatsby-transformer-csv`,
106+
options: {
107+
typeName: ({ node, object }) =>
108+
_.upperFirst(_.camelCase(`${path.basename(node.dir)} Data`)),
109+
},
110+
},
111+
],
112+
}
113+
```
114+
115+
The suffix `Csv` is not added when providing your own function.
116+
117+
If you wanted to have a group per folder with the suffix "Csv", the `typeNameFromDir` provided function would be appropriate.
118+
119+
```javascript
120+
// In your gatsby-config.js
121+
const { typeNameFromDir } = require("gatsby-transformer-csv")
122+
123+
module.exports = {
124+
plugins: [
125+
{
126+
resolve: `gatsby-transformer-csv`,
127+
options: {
128+
typeName: typeNameFromDir,
129+
},
130+
},
131+
],
132+
}
133+
```
134+
135+
## Alternate content behaviour
136+
137+
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+
... on File {
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+
81221
## How to query
82222

83-
You'd be able to query your letters like:
223+
In the default configuration, items can be queried like this:
84224

85225
```graphql
86226
{
@@ -97,28 +237,28 @@ You'd be able to query your letters like:
97237

98238
Which would return:
99239

100-
```javascript
240+
```json
101241
{
102-
allLettersCsv: {
103-
edges: [
242+
"allLettersCsv": {
243+
"edges": [
104244
{
105-
node: {
106-
letter: "a",
107-
value: 65,
108-
},
245+
"node": {
246+
"letter": "a",
247+
"value": 65
248+
}
109249
},
110250
{
111-
node: {
112-
letter: "b",
113-
value: 66,
114-
},
251+
"node": {
252+
"letter": "b",
253+
"value": 66
254+
}
115255
},
116256
{
117-
node: {
118-
letter: "c",
119-
value: 67,
120-
},
121-
},
257+
"node": {
258+
"letter": "c",
259+
"value": 67
260+
}
261+
}
122262
]
123263
}
124264
}

packages/gatsby-transformer-csv/index.js

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)