Usage
The Treelab Typescript API provides an easy way to integrate Treelab with any external system. The API closely follows REST semantics, uses JSON to encode objects, and relies on standard HTTP codes to signal operation outcomes.
Installation
Using npm:
npm install @treelab/treelab
Using yarn:
yarn add @treelab/treelab
Object
Workspace
A workspace contains a collection of cores.
Core
A core contains a collection of tables. A table can belong to multiple Cores.
Table
A table is a collection of fields and rows. Each table is also made up of a series of different views.
Views
A view is essentially a materialized view of a table's data. The same table data can be visualized in a variety of different ways. The current view types supported are:
Grid View
Timeline View
List View
Form View (*After alpha release)
Kanban View (*After alpha release)
Views can not only be used to create interactive visualizations, but it can also be used to pass data from one user/process to another. By applying different filter conditions, the admin user can essentially configure data flow by inviting different users to different views.
Field Types
Treelab has a lot of rich field types but the main field types are:
Text
Number
Record Reference
Table Reference
Core Reference
Select
Multi-Select
Multi-Attachment
Events
Treelab is an event-driven system that encodes all state and data changes within the system into a series of events
. These events
are then written into a series of different topics
, which can then be consumed by different parties for processing. Treelab's Typescript SDK abstracts these events into different objects that can then be used to read and write data from Treelab.
EventPayload {
eventname: 'creb5528ed9c50539b8.CoreCreated',
workspaceid: 'wspb5506fe4160c6310',
coreid: 'creb5528ed9c50539b8',
tableid: '',
columnid: '',
rowid: '',
corename: 'core name',
color: 'dark',
icon: 'check',
tablename: '',
columnconfig: undefined,
view: undefined,
value: undefined,
metadata: { source: 'USER' }
}
Examples
Initialize it with your apiKey:
import { Treelab } from 'treelab';
const treelab = new Treelab({
token: 'your_token_here',
});
Now you can do some querys or mutations with the instance.
const workspace = await treelab.createWorkspace({ name: 'workspace name' });
There are some properties under every instance
console.log(workspace.id);
console.log(workspace.name);
const core = await workspace.core('coreId');
const table = await core.table('tableId');
The table instance contains a map of rows and views as propertity
console.log(table.views);
console.log(table.rows);
Listen to a instance
const subscription = core.onUpdated(e => {
});
subscription.unsubscribe();
core.on(e => {
});
Treelab object array class
All Treelab object array
classes have an attribute, and all classes will have a select and sort method to filter or sort, and have the same listener method as a single object to batch listen for objectss.
const coreArrayClass = await workspace.cores();
const tableArrayClass = await core.createTables(...);
console.log(tableArrayClass.tables);
const selectedTableArrayClass = await tableArrayClass.select(3);
const filteredTableArrayClass = await tableArrayClass.select((i) => i.name === 'table name'));
const filteredTableArrayClass = await tableArrayClass.sort((a, b) => a.name.localeCompare(b.name));
const subscriptions = tableArrayClass.onCellUpdated(e => {
})
API
Treelab
createWorkspace
treelab.createWorkspace(createWorkspaceInput:{ name: string }): Promise<Workspace>;
const workspace = await treelab.createWorkspace({ name: 'workspace name' });
createWorkspaces
treelab.createWorkspaces(): Promise<WorkspaceArray>;
const workspaceArrayClass = await treelab.createWorkspaces([
{ name: 'workspace 1' },
{ name: 'workspace 2' },
]);
workspace
treelab.workspace(id: string ): Promise<Workspace>;
const workspace = await treelab.workspace('wspb5506fe4160c6310');
getWorkspaces
treelab.getWorkspaces(): Promise<WorkspaceArray>;
const workspaceArrayClass = await treelab.getWorkspaces();
Workspace
createCore
treelab.createCore(createCoreInput:{ name: string, color: CoreColor, icon: Icon }): Promise<Core>;
const workspace = await treelab.createCore({
name: 'core name',
color: CoreColor.blue,
icon: Icon.untitle
});
createCores
treelab.createCores(createCoreInput:{ name: string, color: CoreColor, icon: Icon }): Promise<CoreArray>;
const workspace = await treelab.createCores([
{ name: 'core1', color: CoreColor.blue, icon: Icon.untitle },
{ name: 'core2', color: CoreColor.black, icon: Icon.check }
]);
core
workspace.core(id: string ): Promise<Core>;
const core = await workspace.core('creb5506fe46a88de54');
getCores
workspace.getCores(): Promise<CoreArray>;
const coreArrayClass = await workspace.getCores();
select
workspaceArrayClass.select(maxSize: number, filter: FilterFn): WorkspaceArray;
const workspaceArrayClass = await workspaceArrayClass.select(3, (i) => {
i.name === 'test'
});
sort
workspaceArrayClass.sort(sortFn: SortFn): WorkspaceArray;
const workspaceArrayClass = await workspaceArrayClass.sort((a, b) => a.name.localeCompare(b.name));
onCoreCreated
workspace.onCoreCreated(cb: EventCallback): Subscription
workspace.onCoreCreated(e => {
console.log('workspace.onCoreCreated:', e);
})
onCoreAdded
workspace.onCoreAdded(cb: EventCallback): Subscription
workspace.onCoreAdded(e => {
console.log('workspace.onCoreAdded:', e);
})
on
Listen every events under the workspace
workspace.on(cb: EventCallback): Subscription
workspace.on(e => {
console.log('workspace.on:', e);
})
Core
createTable
const table = core.createTable(createTableInput:{
name: string,
view?: ViewInput,
columns?: ColumnConfig[],
data?: any[][]
}): Promise<Table>;
const table = await core.createTable({
name: 'table',
view: { name: 'view', type: ViewType.GRID },
columns: [
{ type: ColumnType.TEXT, name: 'f1' },
{ type: ColumnType.TEXT, name: 'f2' },
{ type: ColumnType.TEXT, name: 'f3' },
],
data: [['1', '2', '3'], ['21', '22', '23'], ['31', '32', '33']],
});
createTables
const tableArray = core.createTables(createTableInput[]:{
name: string,
view?: ViewInput,
columns?: ColumnConfig[],
data?: any[][]
}[]): Promise<TableArray>;
const tableArrayClass = await core.createTables([
{
name: 'table1',
view: { name: 'view1', type: ViewType.GRID },
columns: [
{ type: ColumnType.TEXT, name: 'f1' },
{ type: ColumnType.TEXT, name: 'f2' },
{ type: ColumnType.TEXT, name: 'f3' },
],
data: [['1', '2', '3'], ['21', '22', '23']],
},
{
name: 'table2',
view: { name: 'view2', type: ViewType.GRID },
columns: [
{ type: ColumnType.TEXT, name: 'f1' },
{ type: ColumnType.TEXT, name: 'f2' },
{ type: ColumnType.TEXT, name: 'f3' },
],
data: [['1', '2', '3'], ['21', '22', '23']],
}
]);
table
core.table(id: string ): Promise<Table>;
const table = await core.table('tblb5528eda11885051');
getTables
core.getTables(): Promise<TableArray>;
const tableArrayClass = await core.getTables();
select
coreArrayClass.select(maxSize: number, filter: FilterFn): CoreArray;
const coreArrayClass = await coreArrayClass.select(3, (i) => {
i.name === 'test'
});
sort
coreArrayClass.sort(sortFn: SortFn): CoreArray;
const coreArrayClass = await coreArrayClass.sort((a, b) => a.name.localeCompare(b.name));
onTableCreated
core.onTableCreated(cb: EventCallback): Subscription
core.onTableCreated(e => {
console.log('core.onTableCreated:', e);
})
onTableAdded
core.onTableAdded(cb: EventCallback): Subscription
core.onTableAdded(e => {
console.log('core.onTableAdded:', e);
})
on
Listen every events under the core
core.on(cb: EventCallback): Subscription
core.on(e => {
console.log('core.on:', e);
})
Table
addView
table.addView(addViewInput: {
name: string;
type: ViewType;
}): Promise<View>;
const view = await table.addView({ name: 'grid view', type: ViewType.GRID });
addRow
table.addRow(): Promise<Row>;
const row = await table.addRow();
table
table.addColumn(conlumnConfig: {
type: ColumnType;
name: string;
order?: number;
visibility?: boolean;
foreignTableId?: string;
defaultNumber?: number;
precision?: number;
choices?: Choice[];
}): Promise<Column>;
const column = await table.addColumn({
type: ColumnType.TEXT,
name: "column",
});
view
table.view(id: string): View;
const view = await table.view('viwb5506fe6370f0e4b');
updateCell
table.updateCell(updateCellInput: { rowId: string, columnId: string, value: Value}): Promise<Cell>;
const row = await table.updateCell({
rowId: "rowb545d20fe08226ed",
columnId: "colb545910eed85035e",
value: {
type: ColumnType.TEXT,
text: "newCell"
}
});
addRows
table.addRows(number: number): Promise<RowArray>;
const rowArray = await table.addRows(5);
addColumns
table.addColumns(columnConfigs: ColumnConfig[]): Promise<Column>;
const columnArray = await table.addColumns([
{ type: ColumnType.TEXT, name: "column1" },
{ type: ColumnType.TEXT, name: "column2" },
]);
updateCells
table.updateCells(updateCellInput[]: {
rowId: string;
columnId: string;
value: Value;
}[]): Promise<CellArray>;
const cellArray = await table.updateCells([
{
rowId: "rowb545d20fe08226ed",
columnId: "colb545910eed85035e",
value: {
type: ColumnType.TEXT,
text: "newCell1"
},
{
rowId: "rowb545d20fe08226ec",
columnId: "colb545910eed85035c",
value: {
type: ColumnType.TEXT,
text: "newCell2"
}
}
]);
select
tableArrayClass.select(maxSize: number, filter: FilterFn): TableArray;
const tableArrayClass = await tableArrayClass.select(3, (i) => {
i.name === 'test'
});
sort
tableArrayClass.sort(sortFn: SortFn): TableArray;
const tableArrayClass = await tableArrayClass.sort((a, b) => a.name.localeCompare(b.name));
onViewAdded
table.onViewAdded(cb: EventCallback): Subscription
table.onViewAdded(e => {
console.log('table.onViewAdded:', e);
})
onColumnAdded
table.onColumnAdded(cb: EventCallback): Subscription
table.onColumnAdded(e => {
console.log('table.onColumnAdded:', e);
})
onRowAdded
table.onRowAdded(cb: EventCallback): Subscription
table.onRowAdded(e => {
console.log('table.onRowAdded:', e);
})
onCellUpdated
table.onCellUpdated(cb: EventCallback): Subscription
table.onCellUpdated(e => {
console.log('table.onCellUpdated:', e);
})
on
Listen every events under the table
table.on(cb: EventCallback): Subscription
table.on(e => {
console.log('table.on:', e);
})
View
column
view.column(id: string): View
const view = await view.column('colb5506fe5028c62d9')
getColumns
view.getColumns(): ColumnArray
const viewArray = await view.getColumns()
row
view.row(id: string): Row;
const row = await view.row('rowb5506fe5eb875868');
getRows
view.getRows(): ColumnArray
const viewArray = await view.getRows()
Row
cell
row.cell(columnId: string): Cell
const cell = await row.cell('colb5506fe5028c62d9')
onCellUpdated
row.onCellUpdated(cb: EventCallback): Subscription
row.onCellUpdated(e => {
console.log('row.onCellUpdated:', e);
})
on
row.on(cb: EventCallback): Subscription
row.on(e => {
console.log('row.on:', e);
})
Column
cell
column.cell(rowId: string): Cell
const cell = await column.cell('rowb5506fe5eb875868')
onCellUpdated
column.onCellUpdated(cb: EventCallback): Subscription
column.onCellUpdated(e => {
console.log('column.onCellUpdated:', e);
})
on
column.on(cb: EventCallback): Subscription
column.on(e => {
console.log('column.on:', e);
})
Cell
onCellUpdated
cell.onCellUpdated(cb: EventCallback): Subscription
cell.onCellUpdated(e => {
console.log('cell.onCellUpdated:', e);
})
on
cell.on(cb: EventCallback): Subscription
cell.on(e => {
console.log('cell.on:', e);
})