mcpField
S
spacetimedb-tables

spacetimedb-tables

github.com/danmossa数据与数据库2169 次安装19 周前更新
代码仓库

关于 spacetimedb-tables

用于设计 SpacetimeDB 模块表并改进访问模式的参考技能,面向使用 TypeScript、C#、Rust 或 C++ 的开发者,涵盖索引、可见性、迁移、事件与调度表。

主要功能

  1. 01各语言的表声明模式
  2. 02表可见性与访问规则
  3. 03索引选择与性能建议
  4. 04迁移与列默认值规则
  5. 05事件表和调度表指南

适用场景

  1. 01设计 SpacetimeDB 模块架构
  2. 02为频繁查询选择索引
  3. 03安全修改已发布的表

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/danmossa/spacetimedb-skills --skill spacetimedb-tables

安装后在 Claude Code 中输入/spacetimedb-tables确认已加载。

包含的文件

除 SKILL.md 外,安装时一起下载的参考资料和脚本。

references/
tables-access-permissions.md
tables-auto-increment.md
tables-column-types.md
tables-constraints.md
tables-default-values.md
tables-event-tables.md
tables-file-storage.md
tables-indexes.md
tables-performance.md
tables-schedule-tables.md
tables.md

详细介绍

将此技能用于模块架构和表性能。

表的核心模式

TypeScript 表使用 table(options, columns),并且必须作为对象传递给 schema():

import { schema, table, t } from "spacetimedb/server";

const player = table(
  { name: "player", public: true },
  {
    id: t.u64().primaryKey().autoInc(),
    identity: t.identity().unique(),
    name: t.string().index("btree"),
  },
);

export default schema({ player });

各语言的表声明:

  • TypeScript:{ name: "player_score" } 会创建 ctx.db.playerScore。
  • C#:[SpacetimeDB.Table(Accessor = "Player", Public = true)] 会创建 ctx.Db.Player;类型必须是 partial。
  • Rust:#[spacetimedb::table(accessor = player, public)] 会创建 ctx.db.player();pub struct 不会让表变为公开。
  • C++:SPACETIMEDB_TABLE(Type, player, Public) 会创建 ctx.db[player]。

设计规则

  • 按访问/更新模式组织数据。将频繁更新的热状态与很少更改的个人资料、设置或统计字段拆分开。
  • 客户端可通过订阅查看公开表。将仅供服务器使用的状态设为私有,并在需要时通过视图公开派生子集。
  • 主键和唯一约束会自动创建索引;不要为它们添加重复索引。
  • 仅在频繁使用等值、范围、订阅或联接查询时添加索引。默认使用 B-tree;直接索引适用于密集的无符号整数键,并非所有语言都普遍支持。
  • 在已发布的现有表中新增列时,必须设置默认值,并将新列追加到末尾。更改现有架构前,请检查数据库迁移规则。
  • 自增值由数据库生成,事务失败后可能出现间隔。
  • 使用事件表发送仅追加的客户端通知;发布后无法更改事件标记。
  • 调度表需要 ScheduleAt 列和关联的调度 reducer。在 TypeScript 中,从 spacetimedb 而不是 spacetimedb/server 导入 ScheduleAt。

参考索引

需求打开
表定义和核心模式references/tables.md
访问控制references/tables-access-permissions.md
ID 和默认值references/tables-auto-increment.md, references/tables-default-values.md
类型和约束references/tables-column-types.md, references/tables-constraints.md
索引设计references/tables-indexes.md
事件表和调度表references/tables-event-tables.md, references/tables-schedule-tables.md
存储和性能references/tables-file-storage.md, references/tables-performance.md

指南

  • 更改现有架构前,请在 spacetimedb-databases 中检查迁移影响。
  • 对于频繁运行或涉及大型表的 reducer 路径,优先使用索引查找。
  • 使用事件表发送仅追加的客户端通知,尤其是在替换 reducer 结果回调模式时。

评论

支持 Markdown