question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[csv-transformer] Make a node from each line

See original GitHub issue

Summary

CSV is very convenient format because it is imitate relational database. Right now (v 0.1.0) csv-transformer makes one node per csv file. And I didn’t find a way to create referencies across different node types because all data stored in records field.

Basic example

It is good to have an option for csv-transformer to make one node per line. One column can be a reference to another node type, e.g. category.

Motivation

It is pretty easy to manage certain types of data in CSV file because you have a bird’s eye view on big data set.


This is pretty much a follow up for #293. @hjvedvik mentioned that we need some changes in core to make it possible. Could you enumerate those blockers.

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
Al-Rozhkovcommented, Jul 30, 2019

That’s pretty much how I do it.

  1. Run yarn add xlsx.
  2. Create a folder for custom plugin in the root of your project, e.g. source-xls-plugin.
  3. Create index.js inside source-xls-plugin:
const XLSX = require('xlsx')

const createNodes = (contentType, filePath) => {
  const xls = XLSX.readFile(filePath)
  xls.SheetNames.forEach((n) => {
    const parsedContent = XLSX.utils.sheet_to_json(xls.Sheets[n], {raw: false})

    for (let i = 0; i < parsedContent.length; i++) {
      // Check if row contain required columns
      if ('series' in parsedContent[i] && 'name' in parsedContent[i]) {
        contentType.addNode(parsedContent[i])
      }
    }
  })
}

module.exports = function (api) {
  api.loadSource({ addContentType } => {
    
    // Add new content type
    const SomeProductItem = addContentType({
      typeName: 'SomeProductItem'
    })
    SomeProductItem.addReference('series', 'ParentProduct')
    createNodes(SomeProductItem, 'products/path-to-file.xls')
  })
}

  1. Register custom plugin inside gridsome.config.js like this:
module.exports = {
  siteName: 'YOUR SITE',
  plugins: [
    {
      use: '~/source-xls-plugin',
      options: {}
    }
  ]
}
1reaction
NickSteescommented, Jul 23, 2019

Any update on generating content from CSV files? I really can’t even figure out how to read a single CSV file and get it into GraphQL. I found out its the same as using Markdown source and the transformer auto detects the csv file. It be great to improve the docs for that plugin just a little bit further.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Memory-efficient CSV transformation into CSV or text reports ...
CSV files often start with a line like this, where these values are the name for each column. The purpose is similar to...
Read more >
csv-transformer - npm
Transforms a CSV file -- well, any delimited file, really, into another file using a template. You can use this as a stand...
Read more >
convert CSV lines into Javascript objects - Stack Overflow
What I want to do it get each line of the CSV, convert it to a JavaScript object, store them into an array,...
Read more >
How To Read and Write CSV Files in Node.js Using Node-CSV
All the rows in the CSV file have been transformed into arrays using the csv-parse transform stream. Because logging happens each time a...
Read more >
csvtojson | Yarn - Package Manager
csvtojson module is a comprehensive nodejs csv parser to convert csv to json or column arrays. It can be used as node.js library...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found