1.1.7 • Published 11 months ago

savvytable v1.1.7

Weekly downloads
-
License
ISC
Repository
github
Last release
11 months ago

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

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: 'user@example.com',
  password: '123456',
});

Admin: Get User Info

const result = await admin.getUser({
  userId: '2433b1c2fac24caba92f4c48019354fa@auth.local',
});

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://')),
});
1.1.7

11 months ago

1.1.6

11 months ago

1.1.5

11 months ago

1.1.4

11 months ago

1.1.3

11 months ago

1.1.0

11 months ago

1.0.0

11 months ago