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

csv2sql-lite

v0.0.6

Published

nodejs stream for .csv to MySQL USE, CREATE and INSERT .sql

Downloads

2,168

Readme

csv2sql-lite

  _______ _______ ___ ___ _______ _______ _______ ___
 |   _   |   _   |   Y   |       |   _   |   _   |   |
 |.  1___|   1___|.  |   |___|   |   1___|.  |   |.  |
 |.  |___|____   |.  |   |/  ___/|____   |.  |   |.  |___
 |:  1   |:  1   |:  1   |:  1  \|:  1   |:  1   |:  1   |
 |::.. . |::.. . |\:.. ./|::.. . |::.. . |::..   |::.. . |
 `-------`-------' `---' `-------`-------`----|:.`-------' lite
                                              `--'

csv2sql-lite is a transform stream which is both writable and readable. You would write a .csv file/string into it, and read out MySQL INSERT statements. Useful for large .csv files so one does not have to buffer the .csv into memory.

Note: See csv2sql-stream on npm for an alternative.

Caveat: The .csv parsing is very rudimentary, however it can be replaced easily by many modules on npm, look for lineToInsert() in the source.

Caveat: Only tested on *nix OS, YMMV on Windoze.

Usage

Install:

npm install csv2sql-lite

Use:

var CSV2SQL = require('csv2sql-lite');
var csv2sql = CSV2SQL(opts);

See below for the documentation of opts, the options object.

Example

Open up a read stream to the .csv file and a write stream to where you want the .sql file to be output:

//csv_stream.js
var fs = require('fs');
var rstream = fs.createReadStream('./data.csv');
var wstream = fs.createWriteStream('./mysql.sql');

Load the csv2sql-lite module, with options:

var CSV2SQL = require('csv2sql-lite');
var csv2sql = CSV2SQL({
  tableName: 'myTableName',
  dbName: 'myFancyDatabaseName',
});

Wire up the streams with pipe():

rstream.pipe(csv2sql).pipe(wstream);

Execute the program:

$ nodejs csv_stream.js

If you started with data.csv like this:

username,email,password
john,[email protected],p455w0rd
suzie,[email protected],ilovejohn

You'll end up with mysql.sql looking like this:

use myFancyDatabaseName;
INSERT INTO myTableName (username,email,password) VALUES
("john","[email protected]","p455w0rd")
,("suzie","[email protected]","ilovejohn")
;

Then you can easily load the .sql file into MySQL:

$ mysql -u root -p < mysql.sql

Options

You can pass an options object to csv2sql containing any of the following:

Option | Type | Default | Explanation ------------- | -------------| ------------- | ------------ tableName | String | 'undefined' | The name of the table to INSERT into dbName | String | false | Optionally insert USE dbName; at beginning of .sql file dropTable | Boolean | false | Optionally insert DROP TABLE IF EXISTS tableName; at beginning of .sql file seperator | String | ',' | Optionally specify .csv file field seperator lineSeperator | String | '\n' | Optionally specify .csv file EOL seperator

Testing

Run npm install && npm test from the base directory to run tests.

License

MIT