Coder Social home page Coder Social logo

mysql-insert-csv's Introduction

MySQL Insert CSV

Build Status Maintainability Test Coverage npm version

Easily insert CSV rows into a MySQL database table.

Note This will break up insert commands to limit the maximum number of bytes per statement. This is to allow usage with the Aurora Data API. To remove this limitation, set the maxChars setting to NaN.

Usage

First, create an instance:

import { CsvInsert } from "mysql-insert-csv";

const insert = CsvInsert((statement: string) => mysql.runSql(statement), {
  numericColumns: ["total_orders"],
  maxChars: NaN
});

Now, open a read stream to a CSV file, and pass it into the new instance:

const reader = fs.createReadStream("some/file/path.csv");

await insert(reader, "some_table");

Progress Callback

If you want to display the current progress outside of the default std.err output you can specify a callback that accepts the current progress and table name.

const insert = CsvInsert((statement: string) => mysql.runSql(statement), {
  progressCallback: (progress: number, tableName: string) =>
    console.info(`Current progress: ${progress.toFixed(2)}%`)
});

Column Transformers

If a specific column needs to be modified before insert, you can do that by defining column transformers.

const insert = CsvInsert((statement: string) => mysql.runSql(statement), {
  columnTransformers: {
    some_column: (value: string) => value.toUpperCase()
  }
});

Remove Non Printable Characters

Sometimes non-printable characters can get added to a file when its edited using an application like Excel. To avoid running into problems, you can set the filterInput argument to either true or a regular expression to select the characters to be removed.

The default selector is /[^\000-\031]+/gi, this should remove all non-printable characters.

const insert = CsvInsert((statement: string) => mysql.runSql(statement), {
  filterInput: true
});

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.