npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@giorgi-g/csv-parser

v1.1.3

Published

CSV parser for migrations with Sequelizer

Downloads

18

Readme

CSV PARSER ON STEROIDS

CSV files parser on TypeScript with Sequelizer. For easier migration.


Install npm modules

npm install

Then configure a database according to the .env.example file

In config.ts you will also see sample default values...


We can define different options upon reading a file e.g:

interface CSVParserOptions {
    mapKeyIndexes?: number[];
    mapKeySeparator?: string;
    classPath?: string;
    classObjectGetterName?: string;
    delimiter?: string;
    rootDir?: string;
    rootEnv?: string;
    fileExtension?: string;
   mergeMapKeyValues?: boolean;
}

  1. mapKeyIndexes

    • After reading a file a Map<any|number, any> is generated by default it has numeric index as a key, but we can assign a key generated by the properties.
    • If we want records to have a custom key from CSV file e.g: email we can set mapKeyIndexes: [3] where 3 is the index in CSV for email.
    • We can also have combined key e.g: [0, 3] which will give us ${ID}-${EMAIL}
    • Note: the keys are separated by the Dash symbol if you want to override the default you can change the value of mapKeySeparator key.
  2. classPath && classObjectGetterName

    • After parsing the data from CSV you can cast it to a certain Class which by default can be created inside entities' folder. e.g: { classPath: '../entities/Profile' }
    • If you want value of the map to be some certain property or getter inside the Class you can provide a value for classObjectGetterName e.g: { classObjectGetterName: 'profile' } which is the profile property inside the class
  3. delimiter

    • As a delimiter your CSV file can contain [comma: ,] symbol if that is not the default for your CSV you can provide a different value defined inside.
  4. mergeMapKeyValues

    • While setting a map to the key if you want to receive the same key values in an array make this value true

Example:

const fileName = "sample_profiles"; // file name inside /files/FILE_NAME.csv

 const options: CSVParserOptions = {
   classPath: '../entities/Profile', // Class name if you want to cast result into a class
   classObjectGetterName: 'profile', // Name of the property inside the Class which will be the value inside the Map
   mapKeyIndexes: [3] // Email as a key of the Map
 };

 // Initialize the parser with properties
 const csvParser = new CSVParser(
     fileName,
     options,
 );

 // Read the data inside the parser
 csvParser.Read().then((data) => {
   data.forEach((profile) => {
       console.log('>>> profile', profile);
   })
 });





const connection = dbConnection("profile", 100, 0).then((response) => {
   console.log('>>> response from db:', response);
}).catch((error) => {
   console.log('>>> error: ', error);
});


dbConnection = async (schema?: string, limit: number = 0, offset: number = 0) => {
   const DB = Sequelizer(schema);
   return DB.query(`SELECT uuid, brand_id FROM 
                profile.profiles
            WHERE brand_id IS NOT NULL
            ORDER BY uuid DESC LIMIT ${limit} OFFSET ${offset}`,
           {
              type: QueryTypes.SELECT,
              logging: false,
           });
}