seatable_api

package module
v0.0.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 14, 2025 License: BSD-3-Clause Imports: 12 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

SeaTable API Go Client

Go Reference Go Report Card Go Version

SeaTable API Go Client 是一个用于与 SeaTable 数据库进行交互的 Go 语言客户端库。它提供了完整的 CRUD 操作、文件管理、列管理以及实时 Socket.IO 连接功能。

本项目在Seatable 4.2验证使用过。

其他版本使用问题请参考(https://github.com/seatable/openapi)作适当修改。

安装

go get github.com/tongxinCode/seatable-api-go

快速开始

基本使用
package main

import (
    "fmt"
    "log"
    
    "github.com/tongxinCode/seatable-api-go"
)

func main() {
    // 初始化客户端
    client := seatable_api.Init("your-api-token", "https://cloud.seatable.io")
    
    // 认证
    err := client.Auth(false)
    if err != nil {
        log.Fatal("认证失败:", err)
    }
    
    // 获取元数据
    metadata, err := client.GetMetadata()
    if err != nil {
        log.Fatal("获取元数据失败:", err)
    }
    
    fmt.Printf("元数据: %+v\n", metadata)
}
行操作示例
// 添加行
rowData := map[string]interface{}{
    "Name": "张三",
    "Age":  25,
    "Email": "zhangsan@example.com",
}
result, err := client.AppendRow("employees", rowData)
if err != nil {
    log.Fatal("添加行失败:", err)
}

// 批量添加行
rowsData := []interface{}{
    map[string]interface{}{"Name": "李四", "Age": 30},
    map[string]interface{}{"Name": "王五", "Age": 28},
}
result, err = client.BatchAppendRows("employees", rowsData)

// 更新行
updateData := map[string]interface{}{
    "Age": 26,
}
_, err = client.UpdateRow("employees", "row-id", updateData)

// 删除行
_, err = client.DeleteRow("employees", "row-id")
文件操作示例
// 上传本地文件
result, err := client.UploadLocalFile(
    "local-file.txt",    // 本地文件路径
    "remote-file.txt",   // 远程文件名
    "documents/",        // 相对路径
    "file",              // 文件类型
    false,               // 是否替换
)

// 上传字节数据
data := []byte("Hello, SeaTable!")
reader := bytes.NewReader(data)
result, err = client.UploadBytesFile(
    "hello.txt",
    reader,
    "",
    "file",
    false,
)

// 下载文件
err = client.DownloadFile("file-url", "local-save-path.txt")
列管理示例
// 插入新列
err := client.InsertColumn(
    "employees",           // 表名
    "Department",          // 列名
    seatable_api.ColumnTypes("text"), // 列类型
    "department_key",      // 列键
)

// 重命名列
err = client.RenameColumn("employees", "old_column_key", "NewColumnName")

// 调整列宽
err = client.ResizeColumn("employees", "column_key", 200)

// 冻结列
err = client.FreezeColumn("employees", "column_key", true)

// 删除列
err = client.DeleteColumn("employees", "column_key")
查询和过滤示例
// 过滤行
filters := []map[string]interface{}{
    {
        "column_name": "Age",
        "filter_predicate": "Greater than",
        "filter_term": 25,
    },
}
result, err := client.FilterRows("employees", filters, "", "And")

// SQL 查询
sql := "SELECT * FROM employees WHERE age > 25"
result, err = client.Query(sql, true)

// 列出所有行
rows, err := client.ListRows("employees", "")
Socket.IO 实时连接
// 初始化带 Socket.IO 的客户端
client := seatable_api.Init("your-api-token", "https://cloud.seatable.io")
err := client.Auth(true) // 启用 Socket.IO

if err != nil {
    log.Fatal("认证失败:", err)
}

// 连接 Socket.IO
err = client.Client.Connect()
if err != nil {
    log.Fatal("Socket.IO 连接失败:", err)
}

// 监听事件
err = client.Client.On("custom-event", func(channel *gosocketio.Channel, data interface{}) {
    fmt.Printf("收到事件: %+v\n", data)
})

API 参考

类型
Base

主要的客户端结构体,包含所有 SeaTable API 操作。

type Base struct {
    Token           string            // API Token
    ServerURL       string            // SeaTable 服务器 URL
    DtableServerURL string            // DTable 服务器 URL
    DtableDbUrl     string            // DTable 数据库 URL
    JwtToken        string            // JWT Token
    JwtExp          int64             // JWT 过期时间
    Headers         map[string]string // HTTP 请求头
    WorkspaceID     int               // 工作空间 ID
    DtableUUID      string            // DTable UUID
    DtableName      string            // DTable 名称
    Timeout         int               // 请求超时时间(秒)
    Client          *SocketIO         // Socket.IO 客户端
}
ColumnTypes

列类型定义。

type ColumnTypes string
函数
Init

初始化 SeaTable API 客户端。

func Init(token string, serverURL string) *Base
Auth

进行身份认证。

func (s *Base) Auth(withSocketIO bool) error
行操作方法
AppendRow

添加单行数据。

func (s *Base) AppendRow(tableName string, rowData interface{}) (map[string]interface{}, error)
BatchAppendRows

批量添加行数据。

func (s *Base) BatchAppendRows(tableName string, rowsData []interface{}) (map[string]interface{}, error)
InsertRow

在指定行后插入新行。

func (s *Base) InsertRow(tableName string, rowData interface{}, anchorRowID string) (map[string]interface{}, error)
UpdateRow

更新指定行数据。

func (s *Base) UpdateRow(tableName string, rowID string, rowData interface{}) (map[string]interface{}, error)
DeleteRow

删除指定行。

func (s *Base) DeleteRow(tableName, rowID string) (map[string]interface{}, error)
BatchDeleteRows

批量删除行。

func (s *Base) BatchDeleteRows(tableName string, rowIDs interface{}) (map[string]interface{}, error)
FilterRows

根据条件过滤行。

func (s *Base) FilterRows(tableName string, filters []map[string]interface{}, viewName string, filterConjunction string) (interface{}, error)
ListRows

列出表中固定数量的行。 The response contains a maximum of 1000 rows regardless of the specified limit.

func (s *Base) ListRows(tableName, viewName string, limit int) (interface{}, error)
列操作方法
InsertColumn

插入新列。

func (s *Base) InsertColumn(tableName, columnName string, columnType ColumnTypes, columnKey string) (map[string]interface{}, error)
RenameColumn

重命名列。

func (s *Base) RenameColumn(tableName, columnKey, newColumnName string) (map[string]interface{}, error)
ResizeColumn

调整列宽。

func (s *Base) ResizeColumn(tableName, columnKey string, newColumnWidth int) (map[string]interface{}, error)
FreezeColumn

冻结/解冻列。

func (s *Base) FreezeColumn(tableName, columnKey string, frozen bool) (map[string]interface{}, error)
ModifyColumnType

修改列类型。

func (s *Base) ModifyColumnType(tableName, columnKey string, newColumnType ColumnTypes) (map[string]interface{}, error)
DeleteColumn

删除列。

func (s *Base) DeleteColumn(tableName, columnKey string) (map[string]interface{}, error)
文件操作方法
UploadLocalFile

上传本地文件。

func (s *Base) UploadLocalFile(filePath, name, relativePath, fileType string, replace bool) (map[string]interface{}, error)
UploadBytesFile

上传字节数据作为文件。

func (s *Base) UploadBytesFile(name string, r io.Reader, relativePath, fileType string, replace bool) (map[string]interface{}, error)
DownloadFile

下载文件。

func (s *Base) DownloadFile(url, savePath string) error

获取文件下载链接。

func (s *Base) GetFileDownloadLink(path string) (interface{}, error)

获取文件上传链接。

func (s *Base) GetFileUploadLink() (map[string]interface{}, error)
链接操作方法

添加行链接。

func (s *Base) AddLink(linkID, tableName, otherTableName, rowID, otherRowID string) (map[string]interface{}, error)

移除行链接。

func (s *Base) RemoveLink(linkID, tableName, otherTableName, rowID, otherRowID string) (map[string]interface{}, error)
查询方法
Query

执行 SQL 查询。 Returns at most 10 000 rows.

func (s *Base) Query(sql string, convert bool) (interface{}, error)
GetMetadata

获取数据库元数据。

func (s *Base) GetMetadata() (interface{}, error)

常量

列操作常量
const (
    RENAME_COLUMN      = "rename_column"
    RESIZE_COLUMN      = "resize_column"
    FREEZE_COLUMN      = "freeze_column"
    MOVE_COLUMN        = "move_column"
    MODIFY_COLUMN_TYPE = "modify_column_type"
    DELETE_COLUMN      = "delete_column"
)
Socket.IO 事件常量
const (
    JOIN_ROOM        = "join-room"
    UPDATE_DTABLE    = "update-dtable"
    NEW_NOTIFICATION = "new-notification"
)

错误处理

所有方法都返回 error 类型,建议始终检查错误:

result, err := client.AppendRow("table", data)
if err != nil {
    log.Printf("操作失败: %v", err)
    return
}

依赖

贡献

欢迎提交 Issue 和 Pull Request!

相关链接

Documentation

Index

Constants

View Source
const (
	RENAME_COLUMN      = "rename_column"
	RESIZE_COLUMN      = "resize_column"
	FREEZE_COLUMN      = "freeze_column"
	MOVE_COLUMN        = "move_column"
	MODIFY_COLUMN_TYPE = "modify_column_type"
	DELETE_COLUMN      = "delete_column"

	JOIN_ROOM        = "join-room"
	UPDATE_DTABLE    = "update-dtable"
	NEW_NOTIFICATION = "new-notification"
)

Variables

View Source
var ROW_FILTER_KEYS []string = []string{"column_name", "filter_predicate", "filter_term", "filter_term_modifier"}

Functions

This section is empty.

Types

type Base

type Base struct {
	Token           string
	ServerURL       string
	DtableServerURL string
	DtableDbUrl     string
	JwtToken        string
	JwtExp          int64
	Headers         map[string]string
	WorkspaceID     int
	DtableUUID      string
	DtableName      string
	Timeout         int
	Client          *SocketIO
}

func Init

func Init(token string, serverURL string) *Base
func (s *Base) AddLink(linkID, tableName, otherTableName, rowID, otherRowID string) (map[string]interface{}, error)

func (*Base) AppendRow

func (s *Base) AppendRow(tableName string, rowData interface{}) (map[string]interface{}, error)

func (*Base) Auth

func (s *Base) Auth(withSocketIO bool) error

func (*Base) BatchAppendRows

func (s *Base) BatchAppendRows(tableName string, rowsData []interface{}) (map[string]interface{}, error)

func (*Base) BatchDeleteRows

func (s *Base) BatchDeleteRows(tableName string, rowIDs interface{}) (map[string]interface{}, error)

func (*Base) Clone

func (s *Base) Clone() (*Base, error)

func (*Base) DeleteColumn

func (s *Base) DeleteColumn(tableName, columnKey string) (map[string]interface{}, error)

func (*Base) DeleteRow

func (s *Base) DeleteRow(tableName, rowID string) (map[string]interface{}, error)

func (*Base) DownloadFile

func (s *Base) DownloadFile(url, savePath string) error

func (*Base) FilterRows

func (s *Base) FilterRows(tableName string, filters []map[string]interface{}, viewName string, filterConjunction string) (interface{}, error)

func (*Base) FreezeColumn

func (s *Base) FreezeColumn(tableName, columnKey string, frozen bool) (map[string]interface{}, error)

func (*Base) GetColumnLinkID

func (s *Base) GetColumnLinkID(tableName, columnName, viewName string) (interface{}, error)
func (s *Base) GetFileDownloadLink(path string) (interface{}, error)
func (s *Base) GetFileUploadLink() (map[string]interface{}, error)

func (*Base) GetMetadata

func (s *Base) GetMetadata() (interface{}, error)

func (*Base) InsertColumn

func (s *Base) InsertColumn(tableName, columnName string, columnType ColumnTypes, columnKey string) (map[string]interface{}, error)

func (*Base) InsertRow

func (s *Base) InsertRow(tableName string, rowData interface{}, anchorRowID string) (map[string]interface{}, error)

func (*Base) ListColumns

func (s *Base) ListColumns(tableName, viewName string) (interface{}, error)

func (*Base) ListRows

func (s *Base) ListRows(tableName, viewName string, limit int) (interface{}, error)

func (*Base) ModifyColumnType

func (s *Base) ModifyColumnType(tableName, columnKey string, newColumnType ColumnTypes) (map[string]interface{}, error)

func (*Base) Query

func (s *Base) Query(sql string, convert bool) (interface{}, error)
func (s *Base) RemoveLink(linkID, tableName, otherTableName, rowID, otherRowID string) (map[string]interface{}, error)

func (*Base) RenameColumn

func (s *Base) RenameColumn(tableName, columnKey, newColumnName string) (map[string]interface{}, error)

func (*Base) ResizeColumn

func (s *Base) ResizeColumn(tableName, columnKey string, newColumnWidth int) (map[string]interface{}, error)

func (*Base) UpdateRow

func (s *Base) UpdateRow(tableName string, rowID string, rowData interface{}) (map[string]interface{}, error)

func (*Base) UploadBytesFile

func (s *Base) UploadBytesFile(name string, r io.Reader, relativePath, fileType string, replace bool) (map[string]interface{}, error)

func (*Base) UploadLocalFile

func (s *Base) UploadLocalFile(filePath, name, relativePath, fileType string, replace bool) (map[string]interface{}, error)

type ColumnTypes

type ColumnTypes string

type Message

type Message struct {
	// contains filtered or unexported fields
}

type SocketIO

type SocketIO struct {
	Client *gosocketio.Client
	Base   *Base
}

func InitSocketIO

func InitSocketIO(base *Base) (*SocketIO, error)

func (*SocketIO) Connect

func (sio *SocketIO) Connect() error

func (*SocketIO) On

func (sio *SocketIO) On(method string, f interface{}) error

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
⌂ 换网址