Giter Club home page Giter Club logo

Comments (8)

MichalCz avatar MichalCz commented on May 25, 2024 2

Have you taken a look at the shift method? If you put it after lines it should do what you want, like this:

  StringStream.from(
    fs.createReadStream('contries.csv')
  )
.lines()
.shift(x => console.log("first line:", x)) // here.
.parse()
.setOptions()
.each()
.run()
.then()

There's also a csv parsing method on StringStream called CSVParse - that may be useful.

As to the docs, we're working on a new website and, subsequently, on better documentation. Would you be so kind to review and give us some tips where we should start improvements? Ideally to: [email protected]

from scramjet.

MichalCz avatar MichalCz commented on May 25, 2024 2

No problem - yes, in the shift operation you need to pass the number of chunks you want to shift - so the call should be:

   .shift(1, (x) => console.log("Shifted", x)) // an array should appear.

And move it after parse - this will make sure we work on objects, not lines so the bytes won't kick in.

from scramjet.

Extarys avatar Extarys commented on May 25, 2024

I will! I'm glad you are working on a new documentation website! 😄
I'll try this right away, sorry I missed it (I saw it but interpreted it for something else)

from scramjet.

MichalCz avatar MichalCz commented on May 25, 2024

Interesting... and what did you think it did? Maybe I'd reword the description somehow?

from scramjet.

Extarys avatar Extarys commented on May 25, 2024

I read it "wrong". I read the shift function from the StringStream object.

stringStream.shift(bytes, func)

I was unsure on how to calculate the number of bytes I wanted remove.

Since I started with StringStream as you can see on my other issue, I thought it would override the shift function from DataStream and I wouldn't be able to remove the entire line. I got confused.

from scramjet.

MichalCz avatar MichalCz commented on May 25, 2024

You're right, that doesn't make much sense if you look from this perspective. DataStream..shift operates on whole chunks (objects) - so that's how the line is removed when used after parse.

Ok. I see the point - this needs a major rethink - perhaps shiftBytes should be other than shift for compatibility with DataStream.

We need to consider changing that in v5 - of course a backwards compatible version will be provided for the current v4 line.

from scramjet.

Extarys avatar Extarys commented on May 25, 2024

Oh shiftBytes would be a great idea!

Hmm may I ask, in my logic if I do the following, no lines are added to the database, in fact the console.log doesn't even trigger. But if I remove the .shift function it works and all lines are added including the header and...an invisible last line that doesn't appear in the file (may be something I'll look later)

Same code:

async function insert_countries(that) {
  const { StringStream } = require('scramjet')
  const fs = require('fs')
  const path = require('path')

  const client = await that.pg.connect()

  const INSERT_ENTRY =
    'INSERT INTO countries (iso3, iso2, name, dialing_code, dialing_exit, population, area_km2) VALUES ($1, $2, $3, $4, $5, $6, $7);'

  StringStream.from(
    fs.createReadStream(
      '/media/DarkHawk/srv/NodesProjects/___Ackanet/database/table-countries.csv'
    )
  )
    .lines()
    .shift(x => console.log('Skipped', x))
    .parse(line => {
      // Each line need to be properly formated
      const entry = line.split(';') //comma split

      let reconstruction = []

      // The following fields need formating
      reconstruction.push(entry[4])
      reconstruction.push(entry[3])
      reconstruction.push(entry[0])
      reconstruction.push(entry[1])
      reconstruction.push(entry[2])
      reconstruction.push(entry[5] != '' ? parseInt(entry[5]) : null)
      reconstruction.push(entry[6] != '' ? parseInt(entry[6]) : null)
      return reconstruction
    })
    .setOptions({ maxParallel: 1 })
    .each(async reconstruction => {
      console.log('arg:', reconstruction)

      try {
        await client.query(INSERT_ENTRY, reconstruction)
      } catch (err) {
        console.log('Error while adding line...', err)
        // some more logic could be here?
      } finally {
        // client.release()
      }
    })
    .run()
    .then(
      () => {
        console.log('Read entire file.')
        // return resolve(true)
      },
      e => console.log('Error while handling file.', err)
    )
}

Total output: Read entire file.

I'll let this issue closed for now as it may be my code and not an actuel bug since I'm not very familiar yet with the package.

And I wanted to thank you for the time you take answering my questions. Most of the time we get better help in the repo than on Q&A websites.

from scramjet.

Extarys avatar Extarys commented on May 25, 2024

Duh, thanks

from scramjet.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.