Skip to main content

CSVHandler

CSVHandler provides CSV import and export with automatic encoding conversion. It wraps the csv npm package and iconv-lite, defaulting to Windows-1250 encoding and semicolon delimiter — the standard for Czech CSV files.

Package: io-oidis-hub (backend only)

import { CSVHandler } from "@io-oidis-hub/Io/Oidis/Hub/IOApi/Handlers/CSVHandler.js";

Import​

Parse a CSV file or Buffer into an array of objects (keyed by column headers):

const csv : CSVHandler = new CSVHandler();
const records : any[] = await csv.Import("/path/to/file.csv");

From Buffer​

const buffer : Buffer = getFileBuffer();
const records : any[] = await csv.Import(buffer);

Custom Options​

const records : any[] = await csv.Import(buffer, {
encoding: "utf8",
delimiter: ",",
skipEmptyLines: true,
relaxColumnCount: true,
progress: ($data : IFetchProgress) : void => {
LogIt.Info(`Processed ${$data.index} of ${$data.from} rows`);
}
});

ICSVImportOptions​

OptionTypeDefaultDescription
encodingstring"win1250"Input encoding (iconv-lite codec)
delimiterstring";"Column separator
skipEmptyLinesbooleantrueSkip blank lines
relaxColumnCountbooleantrueAllow rows with fewer/more columns than header
progress($data : IFetchProgress) => voidnullProgress callback (index/from)

Export​

Generate a CSV file from structured data:

const csv : CSVHandler = new CSVHandler();
const result : IExportStatus = await csv.Export({
columns: ["name", "ean", "type"],
rows: [
{name: "Site A", ean: "859182400...", type: "D"},
{name: "Site B", ean: "859182400...", type: "O"}
]
}, ($progress : IFetchProgress) : void => {
LogIt.Info(`Export progress: ${$progress.index}/${$progress.from}`);
});

ICSVExportData​

PropertyTypeDescription
rowsany[]Array of data objects
columnsstring[]Column keys to include (determines order)
isPartialboolean?Flag for partial exports

IExportStatus​

The returned IExportStatus contains:

PropertyTypeDescription
idstringSHA-1 based export identifier
isPartialbooleanWhether export was partial
dataobjectContains data (base64 string or Buffer), encoding, format, name

Export always encodes output to Windows-1250 with semicolon delimiter and includes a header row.

Notes​

  • CSVHandler is a backend-only utility — it requires Node.js require() for csv and iconv-lite packages
  • The csv npm package must be available in project dependencies
  • For frontend CSV parsing (e.g., user-uploaded files parsed in the browser), decode the file with TextDecoder and send the string content to a backend method that uses CSVHandler

See also: Utilities Overview | FileSystemHandler | Convert