CKN Technology Data · Table Utility

CKN Technology Data
Table Class

This page documents the Table class from @ckn-technology/data. The Table represents an in-memory tabular structure composed of multiple rows and provides helpers for adding rows, accessing cells, converting to JSON/CSV, and building tables from plain arrays.

Import

To use the Table class, import it from @ckn-technology/data in your data layer or repository code.

import { Table } from "@ckn-technology/data";

The Table class behaves like an array of row objects and is commonly used as an intermediate representation for query results, transformations, and exporting data.

1. Overview

The Table class is a core data structure representing an in-memory table composed of multiple Row objects.
It behaves like an array of rows and provides:

  • Adding rows (single or multiple)
  • Getting/setting individual cell values by row index and column key
  • Converting to plain Object / JSON / CSV
  • Loading from another Table instance
  • Static utility for converting a plain array into a Table
import { Table } from "@ckn-technology/data";

const table = new Table("Users");
table.addRow({ id: 1, name: "Alice" });
table.addRow({ id: 2, name: "Bob" });

console.log(table.length);   // 2
console.log(table.toCSV());
/*
id,name
1,Alice
2,Bob
*/

2. Constructor & Properties

2.1 constructor(name = "")

Description
Creates a new Table instance with an optional name stored in a private field. The name is not currently exposed publicly but can be useful for debugging or future features.

Signature

constructor(name?: string)

Example

import { Table } from "@ckn-technology/data";

const userTable = new Table("Users");
const logTable  = new Table(); // unnamed

2.2 Properties: rows & length

rows

Description
Returns the internal array of Row objects. This is a direct reference to the underlying array, so mutating it will affect the table.

Signature

get rows(): Row[]

Example

table.rows.forEach(row => {
    console.log(row);
});

length

Description
Returns the current number of rows inside the table. This is a read-only getter.

Signature

get length(): number

Example

console.log("Total rows:", table.length);

3. Adding Rows

3.1 addRow(row)

Description
Adds a single row to the table. If the provided argument is not already an instance of Row, it is converted internally using new Row(row).

Signature

addRow(row: Row | any): void

Example

import { Table, Row } from "@ckn-technology/data";

const table = new Table("Users");

// Using plain object
table.addRow({ id: 1, name: "Alice" });

// Using explicit Row
table.addRow(new Row({ id: 2, name: "Bob" }));

3.2 addRows(rows)

Description
Adds multiple rows at once. Accepts either an array of row-like objects or an array of Row instances. Each entry is handled similarly to addRow().

Signature

addRows(rows: Row[] | any[]): void

Example

table.addRows([
    { id: 3, name: "Charlie" },
    { id: 4, name: "Dana" }
]);

4. Cell Access

4.1 getCell(row, column)

Description
Returns the value stored at the specified row index and column. The column can be a numeric index or a string key (field name).

Signature

getCell(row: number, column: number | string): any

Example

// Assume row 0 = { id: 1, name: "Alice" }
const value1 = table.getCell(0, "name"); // "Alice"
const value2 = table.getCell(0, 0);      // depending on Row implementation

4.2 setCell(row, column, value)

Description
Updates the value at the specified row and column. If the column name does not exist yet, behavior depends on the underlying Row implementation (typically it will create a new field).

Signature

setCell(row: number, column: number | string, value: any): void

Example

table.setCell(0, "name", "Bob");   // update existing cell
table.setCell(1, "email", "bob@example.com");

5. JSON & Object Conversion

5.1 getJSON()

Description
Returns a deep-cloned plain representation of all rows using JSON.parse(JSON.stringify(...)). This is safe for returning to external callers without exposing internal references.

Signature

getJSON(): any

Example

const plain = table.getJSON();
console.log(Array.isArray(plain)); // true

5.2 getTable() & 5.3 setTable(table)

getTable()

Description
Returns the current table instance itself. This is mostly a convenience for fluent APIs or integration points expecting a function call.

getTable(): Table
const current = table.getTable();
// current === table

setTable(table)

Description
Loads data from another Table by cloning its rows into this table. The current rows are typically replaced with the source table's rows.

setTable(table: Table): void
const backup = new Table("Backup");
backup.setTable(table);

5.4 toObject() & 5.5 toJSON()

toObject()

Description
Returns a deep-cloned object representation of all rows. In practice, this is similar to getJSON() and can be used when you need a plain JavaScript structure.

toObject(): any
const obj = table.toObject();
console.log(obj[0].name);

toJSON()

Description
Serializes the table data as a JSON string, suitable for logging, caching, or sending across the network.

toJSON(): string
const jsonString = table.toJSON();
console.log(jsonString);

6. CSV Export

6.1 toCSV()

Description
Converts the table data into a CSV-formatted string. The first row usually contains the column headers inferred from the row objects, followed by one line per row.

Signature

toCSV(): string

Example

const table = new Table("Users");
table.addRows([
    { id: 1, name: "Alice" },
    { id: 2, name: "Bob" }
]);

console.log(table.toCSV());
/*
id,name
1,Alice
2,Bob
*/

7. Static Utilities

7.1 Table.toTable(array)

Description
Static helper that converts an array of objects (or row-like values) into a new Table instance. This is the quickest way to wrap an existing dataset into the Table abstraction so that you can use all instance methods like getCell, toCSV, etc.

Signature

static toTable(array: any[]): Table

Example

const raw = [
    { id: 1, name: "Alice" },
    { id: 2, name: "Bob" }
];

const table = Table.toTable(raw);
console.log(table.length);  // 2
console.log(table.getCell(1, "name")); // "Bob"

8. Summary

The Table class in @ckn-technology/data provides a structured way to work with row-based data in memory.

  • Construction & properties – create named tables, inspect rows and length.
  • Row managementaddRow and addRows to populate your table quickly.
  • Cell-level accessgetCell / setCell for fine-grained updates.
  • Serialization – convert to plain objects or JSON strings using getJSON, toObject, toJSON.
  • ExporttoCSV for reporting, download, or integration with spreadsheet tools.
  • Static helpersTable.toTable makes it easy to wrap existing arrays and immediately get access to the full Table API.