A progressive Node.js framework for building efficient and scalable server-side applications, heavily inspired by Angular.
CSV Parser for NestJS
Nest framework TypeScript starter repository.
Wrapper for csv-parser library
Installation
$ npm install nest-csv-parser
$ yarn add nest-csv-parser
Add nest-csv-parser as a dependency.
import { Module } from '@nestjs/common'
import { CsvModule } from 'nest-csv-parser'
@Module({
imports: [
CsvModule,
...
],
controllers: [ ... ],
providers: [ ... ]
})
export class AppModule {}
Usage
Parser will create instance of entity for each line in CSV stream.
import { Injectable } from '@nestjs/common'
import { CsvParser } from 'nest-csv-parser'
class Entity {
foo: string
bar: string
}
@Injectable()
export class AppService {
constructor(
private readonly csvParser: CsvParser
) {}
async parse() {
const stream = fs.createReadStream(__dirname + '/some.csv')
const entities: Entity[] = await csvParser.parse(stream, Entity)
return entities
}
}
API
csvParser.parse(stream, Entity, count, offset, csvConfig)
has 5 parameters.
stream
required
First parameter has to be the stream of the CSV file. NodeJS Reference
Entity
required
Has to be object from which will parser create instance.
count (optional)
default: null
How many lines you want to parse.
offset (optional)
default: null
Offset is similar to SQL databases. Skips the N lines from the beginning of the file.
csvConfig (optional)
default
{ strict: true, separator: ';' }
Just a configuration object for csv-parser library options you can find here
Development
$ git clone git@github.com:mCzolko/nest-csv-parser.git
$ cd nest-csv-parser
$ yarn install
$ yarn test:watch
Test
$ yarn test
Author
Michael Czolko