UNPKG

@pujansrt/data-genie

Version:

High performant ETL engine written in TypeScript

49 lines (48 loc) 1.69 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.CSVWriter = void 0; const fs_1 = require("fs"); const csv_stringify_1 = require("csv-stringify"); class CSVWriter { constructor(filePath) { this.headerWritten = false; this.fieldNames = []; // To store header names if needed this.filePath = filePath; this.outputStream = (0, fs_1.createWriteStream)(this.filePath); this.stringifier = (0, csv_stringify_1.stringify)(); this.stringifier.pipe(this.outputStream); } setFieldNamesInFirstRow(value) { // Logic to handle writing header row return this; } async write(record) { if (!this.headerWritten && this.fieldNames.length === 0) { // Infer field names from the first record if not explicitly set this.fieldNames = Object.keys(record); this.stringifier.write(this.fieldNames); this.headerWritten = true; } else if (!this.headerWritten && this.fieldNames.length > 0) { this.stringifier.write(this.fieldNames); this.headerWritten = true; } const recordArray = this.fieldNames.map((fieldName) => record[fieldName]); this.stringifier.write(recordArray); } async writeAll(records) { for await (const record of records) { await this.write(record); } } async close() { return new Promise((resolve) => { this.stringifier.end(() => { this.outputStream.end(() => { resolve(); }); }); }); } } exports.CSVWriter = CSVWriter;