npm package: @sagacify/csv2es
Description: Stream your csv to ElasticSearch
Author: @oliviercuyp
Latest version: 1.0.1

# csv2es

[![ci](https://github.com/sagacify/csv2es/actions/workflows/ci.yml/badge.svg)](https://github.com/Sagacify/csv2es/actions/workflows/ci.yml)
[![npm version](https://img.shields.io/npm/v/@sagacify/csv2es.svg)](https://www.npmjs.com/package/@sagacify/csv2es)
[![semantic-release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg)](https://github.com/semantic-release/semantic-release)

Stream your csv to your Elasticsearch !

## Installation

```sh
$ npm install csv2es
```

## Requirement

The csv file/stream first line must contain the headers

## Usage

### Import in your project

```js
// require csv2es
const { csv2es } = require('csv2es');
// import csv2es
import { csv2es } from 'csv2es';
```

### Stream a csv file to Elasticsearch

```js
const elasticsearch = require('http://localhost:9200');
const fs = require('fs');

const main = async () => {
  const fileStream = fs.createReadStream('/path/to/file.csv');
  const client = new elasticsearch.Client({
    node:
  });

  await csv2es(csvStream, client, index, {
    bom: true,
    delimiter: ',',
    quote: "'",
    escape: "'",
    idField: ['ID'],
    jsonFields: ['jsonField1', 'jsonField2'],
    bulkSize: 500
  });
}

main();
```

## Api

**csv2es(csvStream, client, index, [options])**
Returns: `Promise`

### parameters

- `csvStream` <stream.Readable> (required) the csv stream
- `client` <Object> (required) see: [elasticsearch](https://www.npmjs.com/package/@elastic/elasticsearch)
- `index` <string> (required) name of the elasticsearch index

### options

- `bom` <boolean> (default: false) indicate if the file contains a BOM (use when your file is an export from excel)
- `delimiter` <string> (default: ',') single character used as column spearator
- `quote` <string> (default: '"') single character used to delimit a field
- `escape`<string> (default: '"') single character used escape value in a quoted field
- `idField` <string> (default: '') name of the field value that should be used as id for the eslasticsearch index
- `jsonFields` <Array> (default: []) list of the fields that needs to be JSON parsed
- `bulkSize` <number> (default: 100) number of rows that needs to be sent by bulk operation on elasticsearch

## Npm scripts

### Running lint tests

```sh
$ npm test:lint
```

### Running tests

```sh
$ npm test:spec
```

### Running coverage tests

```sh
$ npm test:cover
```

This will create a coverage folder with all the report in `coverage/index.html`

### Running all tests

```sh
$ npm test
```

*Note: that's the one you want to use most of the time*

## Reporting bugs and contributing

If you want to report a bug or request a feature, please open an issue.
If want to help us improve autoroute, fork and make a pull request.
Please use commit format as described [here](https://github.com/angular/angular.js/blob/master/DEVELOPERS.md#-git-commit-guidelines).
And don't forget to run `npm run format` before pushing commit.

## Repository

- [https://github.com/sagacify/csv2es](https://github.com/sagacify/csv2es)

## License

The MIT License (MIT)

Copyright (c) 2021 Sagacify

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.