tse-data-classes
v1.0.3
Published
Classes to normalize and work with ThoughtSpot data payloads.
Maintainers
Readme
Data Classes
Overview
The data classes in src/tse-data-classes.ts wrap the ThoughtSpot payloads so you can treat every payload as tabular data with column metadata, normalized rows, and helper methods to inspect the data. TabularData is the shared base class: it keeps the original payload, lets you set and look up column names (with case-insensitive matching), exposes row/column counts, and provides getDataAsTable() plus populateDataByRow() and populateDataByColumn() so downstream UI code can treat every payload the same way.
Tested Versions
These classes have been tested against ThoughtSpot 10.15.
Installation
Install the published package from npm and use it in your project:
npm install tse-data-classesThe package exports the same classes described below from the default entry point so you can import { TabularData, ActionData } from "tse-data-classes";.
Development
Clone the repository, install the dev dependencies, and build the distributable before publishing or linking:
npm install
npm run buildnpm run build emits CommonJS modules plus .d.ts declarations into the dist directory, which is the only directory published to npm.
Each subclass handles one of the payload shapes that ThoughtSpot emits (actions, context menus, liveboards, search results, Spotter responses, etc.). Every class exposes a static createFromJSON(...) factory that normalizes the payload into the tabular structure described above.
Using the Factory Methods
Generic Factory (Recommended)
If you don't know which payload type you're receiving, use the generic factory method on TabularData. It automatically detects the payload structure and returns the appropriate data class instance:
import { TabularData, DataClassType } from "tse-data-classes";
// Auto-detect the payload type
const data = TabularData.createFromJSON(payload);
const rows = data.getDataAsTable();For cases where auto-detection might be ambiguous (e.g., distinguishing between SpotterData and ActionData), you can provide an explicit type hint:
// Explicit type hint
const spotterData = TabularData.createFromJSON(payload, DataClassType.SpotterData);The DataClassType enum includes: ActionData, ContextActionData, VizPointClickData, LiveboardActionData, LiveboardData, SearchData, AnswerData, and SpotterData.
Type-Specific Factories
Alternatively, call the factory method that corresponds to the payload you receive from ThoughtSpot. The factories read the embedded column metadata, reorder the values to match the column names, and populate the base TabularData so you can immediately call methods like columnNames, nbrRows, getDataAsTable(), or hasColumn().
Example:
const actionData = ActionData.createFromJSON(payload);
const rows = actionData.getDataAsTable();The classes and their typical sources are:
ActionData.createFromJSON(payload)— menu actions on answers (including visualizations in pinboards) orEventType.Data. Use this for primary actions that return embedded answer data.ContextActionData.createFromJSON(payload)— context menu actions or clicks on selected points. Each selected attribute/measure becomes a column.VizPointClickData.createFromJSON(payload)— visualization point click events. This class stores click metadata (vizId,clickType,status), theembedAnswerDatapayload, and the tabular columns that describe the clicked/selected points.LiveboardActionData.createFromJSON(payload)— liveboard menu actions. It creates aVizDatainstance for every visualization container found in the pinboard details.LiveboardData.createFromJSON(payload)— response from the liveboard data API. Each visualization from the API becomes aVizDataentry with its own columns and rows.SearchData.createFromJSON(payload)— search results (Search Data API andfetchAnswerDatacalls). The columns and rows are pulled straight fromcontents[0].AnswerData.createFromJSON(payload)—payload.answerService.fetchData(...)calls. Functionally the same asSearchDatabut emitted in the answer API shape.SpotterData.createFromJSON(payload)— Spotter API responses. Spotter returns a slightly different embed answer structure, but the factory extracts the columns and values into the same tabular format.
TabularData also exposes utilities such as getOriginalColumnName() and hasColumn() that make it easier to search for columns in a case-insensitive way once the factory has normalized the payload.
TabularData Public API
Every factory returns a TabularData subclass instance with the following public getters and helpers:
columnNames— returns the normalized column names so you can build consistent headers or iterate columns in the original order.nbrColumns— reports how many columns were detected in the payload.nbrRows— reports how many rows are stored in the instance.getOriginalColumnName(columnName)— performs a case-insensitive lookup to return the properly cased column name when it exists.hasColumn(columnName)— checks for the presence of a column (case-insensitive).getDataAsTable(columnNames?)— returns the data as an array of rows for the requested columns (all columns when omitted).
Use the public API to inspect an action payload:
const actionData = ActionData.createFromJSON(payload);
console.log("Columns:", actionData.columnNames);
console.log("Row count:", actionData.nbrRows);
if (actionData.hasColumn("Status")) {
const table = actionData.getDataAsTable();
const firstRow = table[0];
console.log("First status value:", firstRow["Status"]);
}Helper Methods
sortObjects(array, attr)— sorts an array of objects in place by theattrkey. Useful when you need deterministic ordering before rendering.tabularDataToHTML(tabularData)— renders anyTabularDatasubclass as an HTML table (<table>with header and body rows) using the configured CSS classes (tabular-data,tabular-data-th, etc.).tabularDataToCSV(tabularData)— produces a CSV string (prefixed withdata:text/csv) by quoting and escaping all values fromgetDataAsTable().const actionData = ActionData.createFromJSON(payload); const csv = tabularDataToCSV(actionData); console.log(csv);
These helpers ensure any of the data class instances can be exported or displayed consistently.
