Skip to content

Commit

Permalink
Add new plugin to handle csv files
Browse files Browse the repository at this point in the history
  • Loading branch information
ssonal committed Jul 13, 2017
1 parent 1bd79ed commit 6f913b7
Show file tree
Hide file tree
Showing 6 changed files with 515 additions and 5 deletions.
98 changes: 97 additions & 1 deletion packages/gatsby-transformer-csv/README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,99 @@
# gatsby-transformer-csv

Stub README
Parses CSV files into JSON arrays.

## Install

`npm install --save gatsby-transformer-csv`

## How to use

```javascript
// In your gatsby-config.js
plugins: [
`gatsby-transformer-csv`,
]
```
Above is the minimal configuration required to begin working. Additional customization
of the parsing process is possible using the parameters listed in
[csvtojson](https://github.com/Keyang/node-csvtojson#parameters).

```javascript
// In your gatsby-config.js
plugins: [
{
resolve: `gatsby-transformer-csv`,
options: {
noheader: true
}
}
]
```

## Parsing algorithm

Each row is converted into a node with CSV headers as the keys.

So if your project has a `letters.csv` with
```
letter,value
a,65
b,66
c,67
```
the following three nodes would be created.

```javascript
[
{ letter: 'a', value: 65, type: 'LettersCsv' },
{ letter: 'b', value: 66, type: 'LettersCsv' },
{ letter: 'c', value: 67, type: 'LettersCsv' },
]
```

## How to query

You'd be able to query your letters like:

```graphql
{
allLettersCsv {
edges {
node {
letter
value
}
}
}
}
```

Which would return:

```javascript
{
allLettersCsv: {
edges: [
{
node: {
letter: 'a'
value: 65
}
},
{
node: {
letter: 'b'
value: 66
}
},
{
node: {
letter: 'c'
value: 67
}
}
]
}
}
```

15 changes: 11 additions & 4 deletions packages/gatsby-transformer-csv/package.json
Original file line number Diff line number Diff line change
@@ -1,18 +1,25 @@
{
"name": "gatsby-transformer-csv",
"version": "1.0.1",
"description": "Stub description for gatsby-transformer-csv",
"description": "Gatsby transformer plugin for CSV files",
"main": "index.js",
"scripts": {
"build": "babel src --out-dir . --ignore __tests__",
"watch": "babel -w src --out-dir . --ignore __tests__"
},
"keywords": [
"gatsby"
"gatsby",
"csv",
"gatsby-plugin"
],
"author": "Kyle Mathews <mathews.kyle@gmail.com>",
"author": "Sonal Saldanha <sonal.saldanha@gmail.com>",
"license": "MIT",
"devDependencies": {
"babel-cli": "^6.24.1"
"babel-cli": "^6.24.1",
"json2csv":"^3.7"
},
"dependencies": {
"bluebird": "^3.5.0",
"csvtojson": "^1.1"
}
}
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,231 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Process nodes correctly correctly creates nodes from JSON which is an array of objects 1`] = `
Array [
Array [
Object {
"blue": "true",
"children": Array [],
"funny": "yup",
"id": "whatever [0] >>> CSV",
"internal": Object {
"contentDigest": "5005aee6b2557974cae0aabc712e125a",
"type": "TestCsv",
},
"parent": "whatever",
},
],
Array [
Object {
"blue": "false",
"children": Array [],
"funny": "nope",
"id": "whatever [1] >>> CSV",
"internal": Object {
"contentDigest": "0af87da7e572c2d6d8493bcd642c2c78",
"type": "TestCsv",
},
"parent": "whatever",
},
],
]
`;

exports[`Process nodes correctly correctly creates nodes from JSON which is an array of objects 2`] = `
Array [
Array [
Object {
"child": Object {
"blue": "true",
"children": Array [],
"funny": "yup",
"id": "whatever [0] >>> CSV",
"internal": Object {
"contentDigest": "5005aee6b2557974cae0aabc712e125a",
"type": "TestCsv",
},
"parent": "whatever",
},
"parent": Object {
"children": Array [],
"content": "\\"blue\\",\\"funny\\"
true,\\"yup\\"
false,\\"nope\\"",
"extension": "csv",
"id": "whatever",
"internal": Object {
"contentDigest": "whatever",
"mediaType": "text/csv",
},
"name": "test",
"parent": "SOURCE",
},
},
],
Array [
Object {
"child": Object {
"blue": "false",
"children": Array [],
"funny": "nope",
"id": "whatever [1] >>> CSV",
"internal": Object {
"contentDigest": "0af87da7e572c2d6d8493bcd642c2c78",
"type": "TestCsv",
},
"parent": "whatever",
},
"parent": Object {
"children": Array [],
"content": "\\"blue\\",\\"funny\\"
true,\\"yup\\"
false,\\"nope\\"",
"extension": "csv",
"id": "whatever",
"internal": Object {
"contentDigest": "whatever",
"mediaType": "text/csv",
},
"name": "test",
"parent": "SOURCE",
},
},
],
]
`;

exports[`Process nodes correctly correctly handles the options object that is passed to it 1`] = `
Array [
Array [
Object {
"children": Array [],
"field1": "blue",
"field2": "funny",
"id": "whatever [0] >>> CSV",
"internal": Object {
"contentDigest": "49605f5e32a2250d7ca740c5d2e79784",
"type": "TestCsv",
},
"parent": "whatever",
},
],
Array [
Object {
"children": Array [],
"field1": "true",
"field2": "yup",
"id": "whatever [1] >>> CSV",
"internal": Object {
"contentDigest": "2bf0bc0e8539ef5f6bbcf67c8b733233",
"type": "TestCsv",
},
"parent": "whatever",
},
],
Array [
Object {
"children": Array [],
"field1": "false",
"field2": "nope",
"id": "whatever [2] >>> CSV",
"internal": Object {
"contentDigest": "f5f617ce4243d639de924543dec5600e",
"type": "TestCsv",
},
"parent": "whatever",
},
],
]
`;

exports[`Process nodes correctly correctly handles the options object that is passed to it 2`] = `
Array [
Array [
Object {
"child": Object {
"children": Array [],
"field1": "blue",
"field2": "funny",
"id": "whatever [0] >>> CSV",
"internal": Object {
"contentDigest": "49605f5e32a2250d7ca740c5d2e79784",
"type": "TestCsv",
},
"parent": "whatever",
},
"parent": Object {
"children": Array [],
"content": "blue,funny
true,yup
false,nope",
"extension": "csv",
"id": "whatever",
"internal": Object {
"contentDigest": "whatever",
"mediaType": "text/csv",
},
"name": "test",
"parent": "SOURCE",
},
},
],
Array [
Object {
"child": Object {
"children": Array [],
"field1": "true",
"field2": "yup",
"id": "whatever [1] >>> CSV",
"internal": Object {
"contentDigest": "2bf0bc0e8539ef5f6bbcf67c8b733233",
"type": "TestCsv",
},
"parent": "whatever",
},
"parent": Object {
"children": Array [],
"content": "blue,funny
true,yup
false,nope",
"extension": "csv",
"id": "whatever",
"internal": Object {
"contentDigest": "whatever",
"mediaType": "text/csv",
},
"name": "test",
"parent": "SOURCE",
},
},
],
Array [
Object {
"child": Object {
"children": Array [],
"field1": "false",
"field2": "nope",
"id": "whatever [2] >>> CSV",
"internal": Object {
"contentDigest": "f5f617ce4243d639de924543dec5600e",
"type": "TestCsv",
},
"parent": "whatever",
},
"parent": Object {
"children": Array [],
"content": "blue,funny
true,yup
false,nope",
"extension": "csv",
"id": "whatever",
"internal": Object {
"contentDigest": "whatever",
"mediaType": "text/csv",
},
"name": "test",
"parent": "SOURCE",
},
},
],
]
`;
Loading

0 comments on commit 6f913b7

Please sign in to comment.