跳转到内容

Schema SDK

eidos.space.schema 对象提供了管理表、字段和视图生命周期的各种方法。

创建一个包含指定字段的新表。

async createTable(input: CreateTableInput): Promise<TableInfo>

示例:

const table = await eidos.space.schema.createTable({
name: "任务清单",
fields: [
{
name: "优先级",
columnName: "priority",
type: "select",
property: {
options: [
{ name: "", color: "red" },
{ name: "", color: "blue" },
],
},
},
{ name: "截止日期", columnName: "deadline", type: "date" },
],
})
console.log("创建的表格 ID:", table.id)

获取特定表的详细信息。

async getTable(tableId: string): Promise<TableInfo>

示例:

const tableInfo = await eidos.space.schema.getTable("table_id_here")
console.log(`表格 ${tableInfo.name} 共有 ${tableInfo.fields.length} 个字段。`)

列出当前空间中的所有表。

async listTables(): Promise<TableListItem[]>

示例:

const tables = await eidos.space.schema.listTables()
tables.forEach((t) => console.log(`${t.name} (${t.id})`))

更新表元数据 (例如重命名)。

async updateTable(tableId: string, input: UpdateTableInput): Promise<TableInfo>

示例:

await eidos.space.schema.updateTable("table_id", { name: "新名称" })

永久删除一个表。

async deleteTable(tableId: string): Promise<boolean>

向现有表添加新字段。

async addField(tableId: string, input: CreateFieldInput): Promise<FieldInfo>

示例:

await eidos.space.schema.addField("table_id", {
name: "新字段",
columnName: "new_field",
type: "text",
})

更新现有字段的元数据。

async updateField(tableId: string, columnName: string, input: UpdateFieldInput): Promise<FieldInfo>

示例:

await eidos.space.schema.updateField("table_id", "priority", {
name: "任务优先级",
})

从表中删除字段。

async deleteField(tableId: string, columnName: string): Promise<boolean>

为表创建新视图。

async createView(tableId: string, input: CreateViewInput): Promise<ViewInfo>

示例:

await eidos.space.schema.createView("table_id", {
name: "我的看板",
type: "kanban",
})

列出与表关联的所有视图。

async listViews(tableId: string): Promise<ViewInfo[]>

删除特定视图。

async deleteView(tableId: string, viewId: string): Promise<boolean>

这两个方法允许你将表格结构(字段定义)以紧凑的可移植字符串形式分享——不包含任何行数据。

将表的 Schema 导出为可移植对象。系统字段(_idtitle、时间戳等)会自动排除。

async export(tableId: string): Promise<TableSchemaExport>
interface TableSchemaExport {
version: 1
name: string
fields: CreateFieldInput[]
}

示例 — 导出并复制为 base64 字符串:

const schema = await eidos.space.schema.export("table_id_here")
// 编码为 base64,便于复制分享
const encoded = btoa(
encodeURIComponent(JSON.stringify(schema)).replace(
/%([0-9A-F]{2})/g,
(_, p1) => String.fromCharCode(parseInt(p1, 16))
)
)
console.log(encoded) // "eyJ2ZXJzaW9uIjox..."

根据之前导出的 Schema 创建新表,是 export() 的逆操作。

async import(schema: TableSchemaExport, nameOverride?: string): Promise<TableInfo>

参数:

  • schemaTableSchemaExport 对象(例如从 base64 字符串解码而来)
  • nameOverride — 可选:覆盖 Schema 中的表名

示例 — 解码 base64 并重建表格:

// 将 base64 字符串解码为 Schema 对象
const schema = JSON.parse(
decodeURIComponent(
atob(encodedString)
.split("")
.map((c) => "%" + c.charCodeAt(0).toString(16).padStart(2, "0"))
.join("")
)
)
// 创建表格
const table = await eidos.space.schema.import(schema)
console.log("已创建:", table.name, "", table.fields.length, "个字段")
// 使用不同名称创建
const copy = await eidos.space.schema.import(schema, "我的副本")

完整往返示例:

// 从源表导出
const schema = await eidos.space.schema.export(sourceTableId)
// 分享 schema 或编码后,在另一个 space 重建:
const newTable = await eidos.space.schema.import(schema, "任务(副本)")