UNPKG

15.5 kBMarkdownView Raw
1# Usage
2
3The 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.
4
5## Installation
6
7Using npm:
8
9```typescript
10npm install @treelab/treelab
11```
12
13Using yarn:
14
15```typescript
16yarn add @treelab/treelab
17```
18
19# Object
20
21## Workspace
22
23A workspace contains a collection of cores.
24
25## Core
26
27A core contains a collection of tables. A table can belong to multiple Cores.
28
29## Table
30
31A table is a collection of fields and rows. Each table is also made up of a series of different views.
32
33## Views
34
35A 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:
36
37- `Grid View`
38- `Timeline View`
39- `List View`
40- `Form View (*After alpha release)`
41- `Kanban View (*After alpha release)`
42
43Views 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.
44
45## Field Types
46
47Treelab has a lot of rich field types but the main field types are:
48
49- `Text`
50- `Number`
51- `Record Reference`
52- `Table Reference`
53- `Core Reference`
54- `Select`
55- `Multi-Select`
56- `Multi-Attachment`
57
58# Events
59
60Treelab 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.
61
62```typescript
63// Example
64EventPayload {
65 eventname: 'creb5528ed9c50539b8.CoreCreated',
66 workspaceid: 'wspb5506fe4160c6310',
67 coreid: 'creb5528ed9c50539b8',
68 tableid: '',
69 columnid: '',
70 rowid: '',
71 corename: 'core name',
72 color: 'dark',
73 icon: 'check',
74 tablename: '',
75 columnconfig: undefined,
76 view: undefined,
77 value: undefined,
78 metadata: { source: 'USER' }
79}
80
81```
82
83# Examples
84
85Initialize it with your apiKey:
86
87```typescript
88// Reference
89import { Treelab } from 'treelab';
90
91// Create instance
92const treelab = new Treelab({
93 token: 'your_token_here',
94});
95```
96
97Now you can do some querys or mutations with the instance.
98
99```typescript
100// Create workspace
101const workspace = await treelab.createWorkspace({ name: 'workspace name' });
102```
103
104There are some properties under every instance
105
106```typescript
107console.log(workspace.id); // workspace id
108console.log(workspace.name); // workspace name
109```
110
111```typescript
112// Get one core
113const core = await workspace.core('coreId');
114
115// Get one table
116const table = await core.table('tableId');
117```
118
119The table instance contains a map of rows and views as propertity
120
121```typescript
122// Map {
123// 'viwb5506fe4f384edda' => View {
124// workspaceId: 'wspb5506fe4160c6310',
125// coreId: 'creb5506fe46a88de54',
126// tableId: 'tblb5506fe4ca8408d8',
127// id: 'viwb5506fe4f384edda',
128// name: 'testView',
129// type: 'GRID',
130// columns:
131// Map {
132// 'colb5506fe502834416' => [Column],
133// 'colb5506fe5028c62d9' => [Column],
134// 'colb5506fe502004bdd' => [Column]
135// }
136// }
137// }
138console.log(table.views);
139
140// Map {
141// 'rowb5506fe58205e2f6' => Row {
142// workspaceId: 'wspb5506fe4160c6310',
143// coreId: 'creb5506fe46a88de54',
144// tableId: 'tblb5506fe4ca8408d8',
145// id: 'rowb5506fe58205e2f6',
146// rowData:
147// Map {
148// 'colb5506fe502834416' => [Cell],
149// 'colb5506fe502004bdd' => [Cell],
150// 'colb5506fe5028c62d9' => [Cell]
151// }
152// }
153// }
154console.log(table.rows);
155```
156
157Listen to a instance
158
159```typescript
160const subscription = core.onUpdated(e => {
161 // do something here
162});
163subscription.unsubscribe();
164core.on(e => {
165 // listen all events under the core
166});
167```
168
169# Treelab object array class
170
171All `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.
172
173```typescript
174// Get core object array class
175const coreArrayClass = await workspace.cores();
176
177// Get table object array class from creating
178const tableArrayClass = await core.createTables(...);
179
180// Get an array of all tables: [Table, Table...]
181console.log(tableArrayClass.tables);
182
183// Get three tables
184const selectedTableArrayClass = await tableArrayClass.select(3);
185
186// Get filtered tables
187const filteredTableArrayClass = await tableArrayClass.select((i) => i.name === 'table name'));
188
189// Get sorted tables by asc(Sort like `arrayObject.sort(sortby)`)
190const filteredTableArrayClass = await tableArrayClass.sort((a, b) => a.name.localeCompare(b.name));
191
192// Listen for all abjects
193const subscriptions = tableArrayClass.onCellUpdated(e => {
194 // do something here
195})
196```
197
198# API
199
200## Treelab
201
202### createWorkspace
203
204```typescript
205treelab.createWorkspace(createWorkspaceInput:{ name: string }): Promise<Workspace>;
206
207// Example
208const workspace = await treelab.createWorkspace({ name: 'workspace name' });
209```
210
211### createWorkspaces
212
213```typescript
214treelab.createWorkspaces(): Promise<WorkspaceArray>;
215
216// Example
217const workspaceArrayClass = await treelab.createWorkspaces([
218 { name: 'workspace 1' },
219 { name: 'workspace 2' },
220]);
221```
222
223### workspace
224
225```typescript
226treelab.workspace(id: string ): Promise<Workspace>;
227
228// Example
229const workspace = await treelab.workspace('wspb5506fe4160c6310');
230```
231
232### getWorkspaces
233
234```typescript
235treelab.getWorkspaces(): Promise<WorkspaceArray>;
236
237// Example
238const workspaceArrayClass = await treelab.getWorkspaces();
239```
240
241## Workspace
242
243### createCore
244
245```typescript
246treelab.createCore(createCoreInput:{ name: string, color: Color, icon: Icon }): Promise<Core>;
247
248// Example
249const workspace = await treelab.createCore({
250 name: 'core name',
251 color: Color.blue,
252 icon: Icon.untitle
253});
254```
255
256### createCores
257
258```typescript
259treelab.createCores(createCoreInput:{ name: string, color: Color, icon: Icon }): Promise<CoreArray>;
260
261// Example
262const workspace = await treelab.createCores([
263 { name: 'core1', color: Color.blue, icon: Icon.untitle },
264 { name: 'core2', color: Color.black, icon: Icon.check }
265]);
266```
267
268### core
269
270```typescript
271workspace.core(id: string ): Promise<Core>;
272
273// Example
274const core = await workspace.core('creb5506fe46a88de54');
275```
276
277### getCores
278
279```typescript
280workspace.getCores(): Promise<CoreArray>;
281
282// Example
283const coreArrayClass = await workspace.getCores();
284```
285
286### select
287
288```typescript
289workspaceArrayClass.select(maxSize: number, filter: FilterFn): WorkspaceArray;
290
291// Example, select 3 workspaces whose names are 'test'
292const workspaceArrayClass = await workspaceArrayClass.select(3, (i) => {
293 i.name === 'test'
294});
295```
296
297### sort
298
299```typescript
300workspaceArrayClass.sort(sortFn: SortFn): WorkspaceArray;
301
302// Example, sort workspaces in ascending order
303// Sort like `arrayObject.sort(sortby)
304const workspaceArrayClass = await workspaceArrayClass.sort((a, b) => a.name.localeCompare(b.name));
305```
306
307### onCoreCreated
308
309```typescript
310workspace.onCoreCreated(cb: EventCallback): Subscription
311
312// Example
313workspace.onCoreCreated(e => {
314 console.log('workspace.onCoreCreated:', e);
315})
316```
317
318### onCoreAdded
319
320```typescript
321workspace.onCoreAdded(cb: EventCallback): Subscription
322
323// Example
324workspace.onCoreAdded(e => {
325 console.log('workspace.onCoreAdded:', e);
326})
327```
328
329### on
330
331Listen every events under the workspace
332
333```typescript
334workspace.on(cb: EventCallback): Subscription
335
336// Example
337workspace.on(e => {
338 console.log('workspace.on:', e);
339})
340```
341
342## Core
343
344### createTable
345
346```typescript
347const table = core.createTable(createTableInput:{
348 name: string,
349 view?: ViewInput, // defaultView: GRID
350 columns?: ColumnConfig[],
351 data?: any[][]
352}): Promise<Table>;
353
354// Example
355const table = await core.createTable({
356 name: 'table',
357 view: { name: 'view', type: ViewType.GRID },
358 columns: [
359 { type: ColumnType.TEXT, name: 'f1' },
360 { type: ColumnType.TEXT, name: 'f2' },
361 { type: ColumnType.TEXT, name: 'f3' },
362 ],
363 data: [['1', '2', '3'], ['21', '22', '23'], ['31', '32', '33']],
364});
365```
366
367### createTables
368
369```typescript
370const tableArray = core.createTables(createTableInput[]:{
371 name: string,
372 view?: ViewInput, // defaultView: GRID
373 columns?: ColumnConfig[],
374 data?: any[][]
375}[]): Promise<TableArray>;
376
377// Example, create a 2 * 3 table
378const tableArrayClass = await core.createTables([
379 {
380 name: 'table1',
381 view: { name: 'view1', type: ViewType.GRID },
382 columns: [
383 { type: ColumnType.TEXT, name: 'f1' },
384 { type: ColumnType.TEXT, name: 'f2' },
385 { type: ColumnType.TEXT, name: 'f3' },
386 ],
387 data: [['1', '2', '3'], ['21', '22', '23']],
388 },
389 {
390 name: 'table2',
391 view: { name: 'view2', type: ViewType.GRID },
392 columns: [
393 { type: ColumnType.TEXT, name: 'f1' },
394 { type: ColumnType.TEXT, name: 'f2' },
395 { type: ColumnType.TEXT, name: 'f3' },
396 ],
397 data: [['1', '2', '3'], ['21', '22', '23']],
398 }
399]);
400```
401
402### table
403
404```typescript
405core.table(id: string ): Promise<Table>;
406
407// Example
408const table = await core.table('tblb5528eda11885051');
409```
410
411### getTables
412
413```typescript
414core.getTables(): Promise<TableArray>;
415
416// Example
417const tableArrayClass = await core.getTables();
418```
419
420### select
421
422```typescript
423coreArrayClass.select(maxSize: number, filter: FilterFn): CoreArray;
424
425// Example, select 3 cores whose names are 'test'
426const coreArrayClass = await coreArrayClass.select(3, (i) => {
427 i.name === 'test'
428});
429```
430
431### sort
432
433```typescript
434coreArrayClass.sort(sortFn: SortFn): CoreArray;
435
436// Example, sort cores in ascending order
437// Sort like `arrayObject.sort(sortby)
438const coreArrayClass = await coreArrayClass.sort((a, b) => a.name.localeCompare(b.name));
439```
440
441### onTableCreated
442
443```typescript
444core.onTableCreated(cb: EventCallback): Subscription
445
446// Example
447core.onTableCreated(e => {
448 console.log('core.onTableCreated:', e);
449})
450```
451
452### onTableAdded
453
454```typescript
455core.onTableAdded(cb: EventCallback): Subscription
456
457// Example
458core.onTableAdded(e => {
459 console.log('core.onTableAdded:', e);
460})
461```
462
463### on
464
465Listen every events under the core
466
467```typescript
468core.on(cb: EventCallback): Subscription
469
470// Example
471core.on(e => {
472 console.log('core.on:', e);
473})
474```
475
476## Table
477
478### addView
479
480```typescript
481table.addView(addViewInput: {
482 name: string;
483 type: ViewType;
484}): Promise<View>;
485
486// Example
487const view = await table.addView({ name: 'grid view', type: ViewType.GRID });
488```
489
490### addRow
491
492```typescript
493table.addRow(): Promise<Row>;
494
495// Example
496const row = await table.addRow();
497```
498
499### table
500
501```typescript
502table.addColumn(conlumnConfig: {
503 type: ColumnType;
504 name: string;
505 order?: number;
506 visibility?: boolean;
507 foreignTableId?: string;
508 defaultNumber?: number;
509 precision?: number;
510 choices?: Choice[];
511}): Promise<Column>;
512
513// Example
514const column = await table.addColumn({
515 type: ColumnType.TEXT,
516 name: "column",
517});
518```
519
520### view
521
522```typescript
523table.view(id: string): View;
524
525// Example
526const view = await table.view('viwb5506fe6370f0e4b');
527```
528
529### updateCell
530
531```typescript
532table.updateCell(updateCellInput: { rowId: string, columnId: string, value: Value}): Promise<Cell>;
533
534// Example
535const row = await table.updateCell({
536 rowId: "rowb545d20fe08226ed",
537 columnId: "colb545910eed85035e",
538 value: {
539 type: ColumnType.TEXT,
540 text: "newCell"
541 }
542});
543```
544
545### addRows
546
547```typescript
548table.addRows(number: number): Promise<RowArray>;
549
550// Example, add 5 rows
551const rowArray = await table.addRows(5);
552```
553
554### addColumns
555
556```typescript
557table.addColumns(columnConfigs: ColumnConfig[]): Promise<Column>;
558
559// Example
560const columnArray = await table.addColumns([
561 { type: ColumnType.TEXT, name: "column1" },
562 { type: ColumnType.TEXT, name: "column2" },
563]);
564```
565
566### updateCells
567
568```typescript
569table.updateCells(updateCellInput[]: {
570 rowId: string;
571 columnId: string;
572 value: Value;
573}[]): Promise<CellArray>;
574
575// Example
576const cellArray = await table.updateCells([
577 {
578 rowId: "rowb545d20fe08226ed",
579 columnId: "colb545910eed85035e",
580 value: {
581 type: ColumnType.TEXT,
582 text: "newCell1"
583 },
584 {
585 rowId: "rowb545d20fe08226ec",
586 columnId: "colb545910eed85035c",
587 value: {
588 type: ColumnType.TEXT,
589 text: "newCell2"
590 }
591 }
592]);
593```
594
595### select
596
597```typescript
598tableArrayClass.select(maxSize: number, filter: FilterFn): TableArray;
599
600// Example, select 3 tables whose names are 'test'
601const tableArrayClass = await tableArrayClass.select(3, (i) => {
602 i.name === 'test'
603});
604```
605
606### sort
607
608```typescript
609tableArrayClass.sort(sortFn: SortFn): TableArray;
610
611// Example, sort tables in ascending order
612// Sort like `arrayObject.sort(sortby)
613const tableArrayClass = await tableArrayClass.sort((a, b) => a.name.localeCompare(b.name));
614```
615
616### onViewAdded
617
618```typescript
619table.onViewAdded(cb: EventCallback): Subscription
620
621// Example
622table.onViewAdded(e => {
623 console.log('table.onViewAdded:', e);
624})
625```
626
627### onColumnAdded
628
629```typescript
630table.onColumnAdded(cb: EventCallback): Subscription
631
632// Example
633table.onColumnAdded(e => {
634 console.log('table.onColumnAdded:', e);
635})
636```
637
638### onRowAdded
639
640```typescript
641table.onRowAdded(cb: EventCallback): Subscription
642
643// Example
644table.onRowAdded(e => {
645 console.log('table.onRowAdded:', e);
646})
647```
648
649### onCellUpdated
650
651```typescript
652table.onCellUpdated(cb: EventCallback): Subscription
653
654// Example
655table.onCellUpdated(e => {
656 console.log('table.onCellUpdated:', e);
657})
658```
659
660### on
661
662Listen every events under the table
663
664```typescript
665table.on(cb: EventCallback): Subscription
666
667// Example
668table.on(e => {
669 console.log('table.on:', e);
670})
671```
672
673## View
674
675### column
676
677```typescript
678view.column(id: string): View
679
680// Example
681const view = await view.column('colb5506fe5028c62d9')
682```
683
684### getColumns
685
686```typescript
687view.getColumns(): ColumnArray
688
689// Example
690const viewArray = await view.getColumns()
691```
692
693### row
694
695```typescript
696view.row(id: string): Row;
697
698// Example
699const row = await view.row('rowb5506fe5eb875868');
700```
701
702### getRows
703
704```typescript
705view.getRows(): ColumnArray
706
707// Example
708const viewArray = await view.getRows()
709```
710
711## Row
712
713### cell
714
715```typescript
716row.cell(columnId: string): Cell
717
718// Example
719const cell = await row.cell('colb5506fe5028c62d9')
720```
721
722### onCellUpdated
723
724```typescript
725row.onCellUpdated(cb: EventCallback): Subscription
726
727// Example
728row.onCellUpdated(e => {
729 console.log('row.onCellUpdated:', e);
730})
731```
732
733### on
734
735```typescript
736row.on(cb: EventCallback): Subscription
737
738// Example
739row.on(e => {
740 console.log('row.on:', e);
741})
742```
743
744## Column
745
746### cell
747
748```typescript
749column.cell(rowId: string): Cell
750
751// Example
752const cell = await column.cell('rowb5506fe5eb875868')
753```
754
755### onCellUpdated
756
757```typescript
758column.onCellUpdated(cb: EventCallback): Subscription
759
760// Example
761column.onCellUpdated(e => {
762 console.log('column.onCellUpdated:', e);
763})
764```
765
766### on
767
768```typescript
769column.on(cb: EventCallback): Subscription
770
771// Example
772column.on(e => {
773 console.log('column.on:', e);
774})
775```
776
777## Cell
778
779### onCellUpdated
780
781```typescript
782cell.onCellUpdated(cb: EventCallback): Subscription
783
784// Example
785cell.onCellUpdated(e => {
786 console.log('cell.onCellUpdated:', e);
787})
788```
789
790### on
791
792```typescript
793cell.on(cb: EventCallback): Subscription
794
795// Example
796cell.on(e => {
797 console.log('cell.on:', e);
798})
799```