# import-csv-mysql

> This package makes it really easy to import a CSV file into a mysql db table.

Latest version **1.0.1** (published 2021-04-27) · MIT license · 0 weekly downloads

## Install

```sh
npm install import-csv-mysql
pnpm add import-csv-mysql
yarn add import-csv-mysql
bun add import-csv-mysql
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.1 |
| Published | 2021-04-27 |
| First published | 2021-04-26 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 3 |
| Unpacked size | 5.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Chuks Agbakuru |
| Maintainers | chuksys |
| Keywords | csv, import, data, mysql, csv-mysql, import-csv-mysql |

## Links

- npm: https://www.npmjs.com/package/import-csv-mysql
- Repository: https://github.com/chuksys/import-csv-mysql
- Homepage: https://github.com/chuksys/import-csv-mysql#readme
- Issues: https://github.com/chuksys/import-csv-mysql/issues
- npm.io page: https://npm.io/package/import-csv-mysql

## Dependencies (3)

- [mysql](https://npm.io/package/mysql.md) ^2.18.1
- [bcrypt](https://npm.io/package/bcrypt.md) ^5.0.1
- [csv-parser](https://npm.io/package/csv-parser.md) ^3.0.0

## Alternatives

- [@libsql/sqlite3](https://npm.io/package/@libsql/sqlite3.md) — 39.8K weekly downloads
- [@fortemi/core](https://npm.io/package/@fortemi/core.md) — 461 weekly downloads
- [cdb-converter](https://npm.io/package/cdb-converter.md) — 341 weekly downloads
- [@uplo/adapter-prisma](https://npm.io/package/@uplo/adapter-prisma.md) — 75 weekly downloads
- [typeorm-aios](https://npm.io/package/typeorm-aios.md) — 30 weekly downloads

## Recent versions

- 1.0.1 (latest) — 2021-04-27
- 1.0.0 — 2021-04-26

## README

# Import a CSV file into a MYSQL table with ease.

To use this package, simply do the following:

1. Make sure the first row of your CSV file contains the column titles for your file.


2. Make sure these column titles in your CSV file match the field names of your MYSQL db table.


3. Run 
    ```
    npm i import-csv-mysql
    ```


4. In your code, import this package, destructure & call the readCSVFile function
    ```
    const { readCSVFile } = require('import-csv-mysql')
    ```


5. Get the path of your CSV file. You can use the `path` package to achieve this easily.
    ```
    npm i path
    ```

    ```
    const path = require('path')

    const root = path.parse(process.cwd()).root
    const fileToRead = path.join(root, 'path', 'to', 'your', 'file.csv')
    ```


6. Provide valid values for the following keys:

    ```
    const dbSettings = {
        host: 'xxxx',
        user: 'xxxx',
        password: 'xxx',
        dbName: 'xxx',
        tableName: 'xxx',
        passwordHashSalt: 'xxx',	
        tableFields: ['pass', 'mysql', 'table', 'fieldnames', 'that', 'match', 'with', 'csv', 'column', 'titles']
    }
    ```
    If you don't have a password column in your CSV file to pass password values to dbTable, ignore the `passwordHashSalt` key-value pair.

    If you do have a password column in your CSV file to pass password values, include the `passwordHashSalt` key-value pair and pass the password salt which you'll like to use to hash the passwords in your file, before inserting into db. 
    
    #### **Note:**
    Do not pass an empty value to `passwordHashSalt`; if you don't have a password hashing salt, please omit the `passwordHashSalt` key-value pair entirely or pass `null` as the value.

    The value of ***tableFields*** must be an array of strings e.g. 
    
    ```

    ['first_name', 'last_name', 'email_address']
    ```


7. Pass the `fileToRead` and `dbSettings` variables as arguments to the `readCSVFile` function

    ```
    readCSVFile(fileToRead, dbSettings) 
    ```


8. Run your code.

9. If you followed these instructions, the records in your CSV file should now be imported into the mysql dbTable provided in `dbSettings`.

---
_Source: https://npm.io/package/import-csv-mysql · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
