1.1.2 • Published 5 years ago

auto-sql2csv v1.1.2

Weekly downloads
2
License
MIT
Repository
github
Last release
5 years ago

Installation

NPM

NPM

Run npm install auto-sql2csv.

Why?

This project was made for database reporting. Auto in the first is because that sql2csv is already taken by CLI abandoned 5 year old project.

Usage

Usage is pretty straight forward. Currently this package has support only for mysql driver, but others may work when they have similar syntax (conn.query(str, (err, result)=>{})).

Options

namedefaultdescription
prefix'SQL2CSV 'Prefix to use when logging
connnullConnection to use, must be created outside
colorstrueWhether to use colors or not when logging
crlffalseUse windows crlf instead of linux lf on line ends
skipMysqlCheckfalseSkip check for mysql driver (doesn't do anything other than warning)
showNamesfalseShow column names in first row
loggingfalseLog to console

Methods

nameparametersdescription
logstring to logLogs to console using prefix + string
colorLogstring to log, colorSame as log but with color prefix parameter, used to disable coloring when colors: false
warnstring to warnSame as log but with yellow color
constructoroptionsRun when class instanciated, options are optional
querysqlThis is where magic happens. Runs MySQL query and converts result (if any) to csv. Using promise. Rejected when empty result or error happens.
setOptionoption name, new valueSets new value to option
setOptionsoptionsOverwrites options object with the one provided
setConnectionconnSets new connection

Example

const sql2csv = require("sql2csv");
const mysql = require("mysql");

var con = mysql.createConnection({
  host: "localhost",
  user: "root",
  password: ""
});

const s2c = new sql2csv({
  logging: true,//Log to console
  skipMysqlCheck: true,//Skip mysql check, as it's surely there
  colors: true,//Use colors, default value (could be ommited)
  showNames: true,//Show column names in first row
  crlf: false,//Use only linux lf
  conn: con //Connection to use
  });

console.log(s2c.query("SELECT * FROM db.table"));
/*
Shows something like
id,column1,column2
1,Daniel,Bulant
2,John,Doe
*/