savvytable
v1.1.7
Published
Use the SeaTable API in NodeJS
Downloads
317
Readme
savvytable
Installing
npm install savvytable
About
- Use the SeaTable API in your NodeJS project
- Typesafe with zod
- Extend your row schemas with zod
Examples
- Base: Initialization
- Base: Authentication
- Query Schema
- Base: Query
- Table: Initialization
- Row Schema
- Table: Get Row
- Table: Get Rows
- Table: Append Row
- Table: Insert Row
- Table: Append Rows
- Table: Update Row
- Table: Update Rows
- Table: Delete Row
- Table: Delete Rows
- Admin: Initialization
- Admin: Get User Info
- Extend Row Schema With Zod
- Check Webhook (Express Example)
Base: Initialization
import { Base } from 'savvytable';
const base = new Base({
url: 'https://table.example.com'.
token: '<TOKEN>'
});
Base: Authentication
await base.auth();
Query Schema
import { QueryResult, QueryTypes, AsType } from 'savvytable';
const EmployeeRowSchema = QueryResult({
ID: QueryTypes.AutoNumber,
Nr: QueryTypes.Number,
Name: QueryTypes.Text,
Employee: QueryTypes.Collaborator,
Worktime: QueryTypes.Link,
});
type EmployeeRow = AsType<typeof EmployeeRowSchema>;
Base: Query
const result: EmployeeRow[] = await base.query({
query: 'SELECT * FROM Employee',
rowSchema: EmployeeRowSchema,
});
Table: Initialization
const table = base.Table({
name: 'Employee',
});
Row Schema
import { Row, RowTypes, AsType } from 'savvytable';
const EmployeeRowSchema = Row({
_id: RowTypes._Id,
ID: RowTypes.AutoNumber,
Nr: Optional(RowTypes.Number),
Name: Optional(RowTypes.Text),
Employee: Optional(RowTypes.Collaborator),
Worktime: Optional(RowTypes.Link),
});
type EmployeeRow = AsType<typeof EmployeeRowSchema>;
Table: Get Row
const result: EmployeeRow = await table.getRow({
rowId: 'VV_vVwlESmWZ86ANOIt1fQ',
rowSchema: EmployeeRowSchema,
});
Table: Get Rows
const result: EmployeeRow[] = await table.getRows({
rowSchema: EmployeeRowSchema,
});
Table: Append Row
const result = await table.addRow({
row: {
Name: 'TESTNAME',
},
});
Table: Insert Row
const result = await table.addRow({
row: {
Name: 'TEST_INSERT_ABOVE',
},
anchorRowId: 'MNJ3ylTORW631nDJ3OBxeQ',
rowInsertPosition: 'above',
});
Table: Append Rows
const result = await table.addRows({
rows: [
{
Name: 'ADD_001',
},
{
Name: 'ADD_002',
},
],
});
Table: Update Row
await table.updateRow({
rowId: 'PsgeZOn5Q7Cm49zZusGjow',
row: {
Name: '...',
},
});
Table: Update Rows
await table.updateRows({
rows: [
{
rowId: 'PsgeZOn5Q7Cm49zZusGjow',
data: {
Name: 'UPDATE_ROW_1',
},
},
{
rowId: 'JEyEg166RvCGl1lsoV-6TQ',
data: {
Name: 'UPDATE_ROW_2',
},
},
],
});
Table: Delete Row
await table.deleteRow({
rowId: 'GDmShJ84SYaB4KOs_v_Tzw',
});
Table: Delete Rows
await table.deleteRows({
rowIds: ['L0AJLFTMTamxpjwxR_CNbQ', 'NVGuoFibTESXme8-ySAA9w'],
});
Admin: Initialization
import { Admin } from 'savvytable';
const admin = await Admin.withCredentials({
url: 'https://table.example.com',
username: '[email protected]',
password: '123456',
});
Admin: Get User Info
const result = await admin.getUser({
userId: '[email protected]',
});
Check Webhook (Express Example)
import { WebhookSchema, Webhook } from 'savvytable';
import express from 'express';
const app = express.app();
app.post('/v1/base-updated/', (req, res) => {
const parsed = WebhookSchema.safeParse(req.body);
if (parsed.success) {
const data: Webhook = parsed.data;
} else {
// wrong webhook data
}
});
Extend Row Schema With Zod
import * as z from 'zod';
import { Row } from 'savvytable';
const EmployeeRowSchema = Row({
_id: RowTypes._Id,
ID: z.string().length(4),
Nr: Optional(RowTypes.Number),
Name: Optional(RowTypes.Text),
Employee: Optional(RowTypes.Collaborator),
Worktime: Optional(z.string().url().startsWith('https://')),
});