Security News
tea.xyz Spam Plagues npm and RubyGems Package Registries
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
csv2sql-lite
Advanced tools
Readme
_______ _______ ___ ___ _______ _______ _______ ___
| _ | _ | 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.
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.
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,john@email.com,p455w0rd
suzie,suzie@email.com,ilovejohn
You'll end up with mysql.sql
looking like this:
use myFancyDatabaseName;
INSERT INTO myTableName (username,email,password) VALUES
("john","john@email.com","p455w0rd")
,("suzie","suzie@email.com","ilovejohn")
;
Then you can easily load the .sql
file into MySQL:
$ mysql -u root -p < mysql.sql
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 |
Run npm install && npm test
from the base directory to run tests.
MIT
FAQs
nodejs stream for .csv to MySQL USE, CREATE and INSERT .sql
The npm package csv2sql-lite receives a total of 353 weekly downloads. As such, csv2sql-lite popularity was classified as not popular.
We found that csv2sql-lite demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Security News
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.
Security News
UnitedHealth Group disclosed that the ransomware attack on Change Healthcare compromised protected health information for millions in the U.S., with estimated costs to the company expected to reach $1 billion.