CSV
The CSV provider exports data using json-2-csv.
Provider arguments
This provider is selected when type is csv.
Required fields
| Field | Type | Required | Notes |
|---|---|---|---|
type | 'csv' | ✅ Yes | Must be csv. |
data | Array<object> | ✅ Yes | Array of rows (objects). |
options (optional)
| Field | Type | Required | Default | Notes |
|---|---|---|---|---|
options.headers | string[] | ❌ No | — | Column order / keys list. If not provided, the provider uses the object keys. |
options.headersMap | Record<string,string> | ❌ No | — | Maps field keys to header titles. |
options.delimiter | string | ❌ No | , | CSV field delimiter. |
options.quote | string | ❌ No | — | Quote/wrap character (passed to json-2-csv). |
options.encoding | BufferEncoding | ❌ No | utf-8 | Encoding used when creating the output buffer. |
Example
export.service.ts
import { Injectable } from '@nestjs/common';
import { DataExporterService } from 'nestjs-export-module';
@Injectable()
export class ExportService {
constructor(private readonly exporter: DataExporterService) {}
async exportCsv(rows: Array<object>) {
return this.exporter.exportAsync({
type: 'csv',
data: rows,
options: {
headers: ['id', 'name', 'email'],
headersMap: { id: 'ID', name: 'Name', email: 'Email' },
delimiter: ',',
encoding: 'utf-8',
},
});
}
}
Notes
- Suitable for simple tabular exports.
- This provider returns
{ mimeType: 'text/csv', extension: 'csv', data: Buffer }.