CKN Technology Data · CSV File

CKN Technology Data
CSVFile Class

This page documents the CSVFile class which extends the generic File abstraction and integrates with Table, Row, and the csv library for parsing and writing CSV data in a structured way.

Import

The CSVFile class is provided by @ckn-technology/data and depends on:

  • File – generic file abstraction.
  • Table – in-memory table representation.
  • Row – row wrapper object.
  • parse from the csv library – for CSV parsing.
import { CSVFile } from "@ckn-technology/data";

1. Overview

CSVFile is a convenient wrapper for reading and writing CSV files into structured Table and Row objects.

It provides:

  • Creating and resetting an internal Table.
  • Adding rows manually with Row objects.
  • Reading CSV directly from disk into a Table.
  • Writing table contents back to a CSV file.
  • Converting the internal table into JSON text.
import { CSVFile } from "@ckn-technology/data";

const csv = new CSVFile("./data/users.csv");

2. Constructor

constructor(filePath)

Description
Creates a new CSVFile instance for the given file path. Internally calls the base File constructor.

Signature

constructor(filePath: string)

Example

const csv = new CSVFile("./data/users.csv");

3. Table Initialization

create()

Description
Initializes a new empty Table inside the CSV file object. Typically used to reset the internal table before adding rows or writing output.

Signature

create(): void

Example

const csv = new CSVFile("./data/data.csv");
csv.create();   // resets internal table

4. Adding Data

4.1 addRow(row)

Description
Adds a single Row instance to the internal Table.

Signature

addRow(row: Row): void

Example

csv.create();
csv.addRow(new Row({ id: 1, name: "Alice" }));

4.2 addRows(rows)

Description
Adds multiple Row objects to the internal Table.

Signature

addRows(rows: Row[]): void

Example

csv.addRows([
  new Row({ id: 1, name: "Alice" }),
  new Row({ id: 2, name: "Bob" })
]);

5–6. Reading & Writing CSV

5.1 read()

Description
Reads a CSV file from disk and loads its contents into a new internal Table.

  • Uses the parse function from the "csv" package.
  • The parser is configured with { columns: true }, so the first row becomes object keys.
  • Each parsed row is wrapped and added to the internal table.
Important Note:
The implementation uses parse(this.fullPath, ...), which is an unusual usage pattern. Normally, parse() expects CSV text, not a file path. In environments where this does not work, you should load the CSV content using fs.readFileSync() and pass the text to parse.

Signature

read(): void

Example

const csv = new CSVFile("./data/users.csv");
csv.read();

// Internal table now contains Row objects
console.log(csv.toJSON());

6.1 write()

Description
Writes the internal table to CSV format and saves it to disk.

  • Gets CSV string via this.#table.toCSV().
  • Writes the CSV text using the underlying writeText method from File.

Signature

write(): Promise<void>

Example

csv.create();
csv.addRow(new Row({ id: 1, name: "Alice" }));
csv.addRow(new Row({ id: 2, name: "Bob" }));

await csv.write(); 
// saves: 
// id,name
// 1,Alice
// 2,Bob

7. JSON Conversion

7.1 toJSON()

Description
Returns the internal Table in JSON format.

  • Internally calls this.#table.toJSON().
  • Table.toJSON() produces a JSON string representation of all rows.

Signature

toJSON(): string

Example

const json = csv.toJSON();
console.log(json);
// Example: '[{"id":1,"name":"Alice"}]'

8. Summary

The CSVFile class is a focused wrapper for reading and writing CSV data on top of the generic File abstraction.

  • Integrates with Table and Row for structured, row-based data handling.
  • Uses the csv library’s parse function to map CSV rows into objects where headers form the keys.
  • Provides simple methods to:
    • Initialize a new in-memory table (create()).
    • Add single or multiple rows (addRow(), addRows()).
    • Read CSV from a file path into the internal table (read()).
    • Write the table’s contents back to CSV (write()).
    • Export the data as a JSON string (toJSON()).

With these capabilities, CSVFile provides a clean bridge between flat CSV files and higher-level structured data operations throughout @ckn-technology/data.