重要前提
安装AI Skills的关键前提是:必须科学上网,且开启TUN模式,这一点至关重要,直接决定安装能否顺利完成,在此郑重提醒三遍:科学上网,科学上网,科学上网。查看完整安装教程 →
spacetimedb-best-practices by isyn/stdb-skills
npx skills add https://github.com/isyn/stdb-skills --skill spacetimedb-best-practicesSpacetimeDB 应用程序的全面开发指南,涵盖 TypeScript 服务器模块以及与 React 的客户端 SDK 集成。包含 8 个类别的规则,按影响优先级排序,以指导自动化重构和代码生成。
包: spacetimedb (v1.4.0+)
在以下情况下参考这些指南:
| 优先级 | 类别 | 影响 | 前缀 |
|---|---|---|---|
| 1 | 模块设计 | 关键 | module- |
| 2 | 表结构与索引 | 关键 | table- |
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
| 3 | Reducer 模式 | 高 | reducer- |
| 4 | 订阅优化 | 高 | subscription- |
| 5 | 客户端状态管理 | 中-高 | client- |
| 6 | React 集成 | 中 | react- |
| 7 | TypeScript 模式 | 中 | ts- |
| 8 | 实时同步 | 低-中 | sync- |
import { spacetimedb, table, t, ReducerContext } from 'spacetimedb';
// 使用表构建器定义表
const Player = table(
{ name: 'player', public: true },
{
identity: t.identity().primaryKey(),
name: t.string(),
score: t.u64().index(),
isOnline: t.bool().index(),
}
);
// 定义 reducers
spacetimedb.reducer('create_player', { name: t.string() }, (ctx: ReducerContext, { name }) => {
ctx.db.player.insert({
identity: ctx.sender,
name,
score: 0n,
isOnline: true,
});
});
// 生命周期钩子
spacetimedb.init((ctx: ReducerContext) => { /* 模块初始化 */ });
spacetimedb.clientConnected((ctx: ReducerContext) => { /* 客户端连接 */ });
spacetimedb.clientDisconnected((ctx: ReducerContext) => { /* 客户端断开连接 */ });
import { DbConnection } from './generated';
// 构建连接
const conn = DbConnection.builder()
.withUri('ws://localhost:3000')
.withModuleName('my-module')
.onConnect((ctx, identity, token) => {
// 设置订阅
conn.subscription(['SELECT * FROM player WHERE isOnline = true']);
})
.onDisconnect((ctx, error) => { /* 处理断开连接 */ })
.build();
// 调用 reducers
await conn.reducers.createPlayer('Alice');
// 访问表
const player = conn.db.player.identity.find(identity);
import { useTable, where, eq } from 'spacetimedb/react';
import { DbConnection, Player } from './generated';
function OnlinePlayers() {
const { rows: players } = useTable<DbConnection, Player>(
'player',
where(eq('isOnline', true))
);
return players.map(p => <div key={p.identity.toHexString()}>{p.name}</div>);
}
module-single-responsibility - 每个领域概念对应一个模块module-lifecycle - 适当使用生命周期钩子 (init, clientConnected, clientDisconnected)module-error-handling - 在模块代码中优雅地处理错误module-type-exports - 导出类型供客户端使用table-primary-keys - 选择合适的主键策略table-indexing - 为频繁查询的列添加 .index()table-relationships - 正确建模表之间的关系table-column-types - 使用合适的 SpacetimeDB 类型reducer-atomicity - 保持 reducer 的原子性和专注性reducer-validation - 在 reducer 入口验证输入reducer-authorization - 对敏感操作检查调用者身份reducer-batch-operations - 在单个 reducer 中批量处理相关变更subscription-selective - 仅订阅需要的数据subscription-filters - 使用订阅过滤器减少数据传输subscription-cleanup - 不再需要时清理订阅subscription-batching - 在客户端连接时批量设置订阅client-connection-lifecycle - 正确处理连接/重连client-optimistic-updates - 使用乐观更新以获得响应式 UIclient-error-recovery - 优雅地处理 reducer 错误client-identity - 安全地管理身份令牌react-use-subscription - 正确使用订阅钩子react-table-hooks - 使用 useTable<DbConnection, Type>() 获取响应式数据react-reducer-hooks - 调用 conn.reducers.* 并妥善处理错误react-connection-status - 向用户显示连接状态ts-generated-types - 使用 SpacetimeDB CLI 生成的类型ts-strict-mode - 启用严格 TypeScript 以获得更好的类型安全ts-discriminated-unions - 使用可辨识联合类型表示状态ts-type-guards - 实现类型守卫进行运行时验证sync-conflict-resolution - 处理并发修改sync-offline-support - 需要时设计为离线优先sync-debounce-updates - 对快速 UI 更新进行防抖处理sync-presence - 高效实现用户在线状态阅读各个规则文件以获取详细解释和代码示例:
rules/module-single-responsibility.md
rules/table-primary-keys.md
rules/_sections.md
每个规则文件包含:
查看包含所有规则扩展的完整指南:AGENTS.md
每周安装数
57
仓库
首次出现
2026年1月20日
安全审计
安装于
opencode51
codex51
gemini-cli50
cursor48
github-copilot45
amp43
Comprehensive development guide for SpacetimeDB applications, covering both TypeScript server modules and client SDK integration with React. Contains rules across 8 categories, prioritized by impact to guide automated refactoring and code generation.
Package: spacetimedb (v1.4.0+)
Reference these guidelines when:
| Priority | Category | Impact | Prefix |
|---|---|---|---|
| 1 | Module Design | CRITICAL | module- |
| 2 | Table Schema & Indexing | CRITICAL | table- |
| 3 | Reducer Patterns | HIGH | reducer- |
| 4 | Subscription Optimization | HIGH | subscription- |
| 5 | Client State Management | MEDIUM-HIGH | client- |
| 6 | React Integration | MEDIUM | react- |
| 7 | TypeScript Patterns | MEDIUM | ts- |
| 8 | Real-time Sync | LOW-MEDIUM | sync- |
import { spacetimedb, table, t, ReducerContext } from 'spacetimedb';
// Define tables with the table builder
const Player = table(
{ name: 'player', public: true },
{
identity: t.identity().primaryKey(),
name: t.string(),
score: t.u64().index(),
isOnline: t.bool().index(),
}
);
// Define reducers
spacetimedb.reducer('create_player', { name: t.string() }, (ctx: ReducerContext, { name }) => {
ctx.db.player.insert({
identity: ctx.sender,
name,
score: 0n,
isOnline: true,
});
});
// Lifecycle hooks
spacetimedb.init((ctx: ReducerContext) => { /* module init */ });
spacetimedb.clientConnected((ctx: ReducerContext) => { /* client connected */ });
spacetimedb.clientDisconnected((ctx: ReducerContext) => { /* client disconnected */ });
import { DbConnection } from './generated';
// Build connection
const conn = DbConnection.builder()
.withUri('ws://localhost:3000')
.withModuleName('my-module')
.onConnect((ctx, identity, token) => {
// Setup subscriptions
conn.subscription(['SELECT * FROM player WHERE isOnline = true']);
})
.onDisconnect((ctx, error) => { /* handle disconnect */ })
.build();
// Call reducers
await conn.reducers.createPlayer('Alice');
// Access tables
const player = conn.db.player.identity.find(identity);
import { useTable, where, eq } from 'spacetimedb/react';
import { DbConnection, Player } from './generated';
function OnlinePlayers() {
const { rows: players } = useTable<DbConnection, Player>(
'player',
where(eq('isOnline', true))
);
return players.map(p => <div key={p.identity.toHexString()}>{p.name}</div>);
}
module-single-responsibility - One module per domain conceptmodule-lifecycle - Use lifecycle hooks appropriately (init, clientConnected, clientDisconnected)module-error-handling - Handle errors gracefully in module codemodule-type-exports - Export types for client consumptiontable-primary-keys - Choose appropriate primary key strategiestable-indexing - Add .index() for frequently queried columnstable-relationships - Model relationships between tables correctlytable-column-types - Use appropriate SpacetimeDB typesreducer-atomicity - Keep reducers atomic and focusedreducer-validation - Validate inputs at reducer entryreducer-authorization - Check caller identity for sensitive operationsreducer-batch-operations - Batch related mutations in single reducersubscription-selective - Subscribe only to needed datasubscription-filters - Use subscription filters to reduce data transfersubscription-cleanup - Clean up subscriptions when no longer neededsubscription-batching - Batch subscription setup on client connectclient-connection-lifecycle - Handle connection/reconnection properlyclient-optimistic-updates - Use optimistic updates for responsive UIclient-error-recovery - Handle reducer errors gracefullyclient-identity - Manage identity tokens securelyreact-use-subscription - Use subscription hooks correctlyreact-table-hooks - Use useTable<DbConnection, Type>() for reactive datareact-reducer-hooks - Call conn.reducers.* with proper error handlingreact-connection-status - Display connection status to usersts-generated-types - Use generated types from SpacetimeDB CLIts-strict-mode - Enable strict TypeScript for better type safetyts-discriminated-unions - Use discriminated unions for statets-type-guards - Implement type guards for runtime validationsync-conflict-resolution - Handle concurrent modificationssync-offline-support - Design for offline-first when neededsync-debounce-updates - Debounce rapid UI updatessync-presence - Implement user presence efficientlyRead individual rule files for detailed explanations and code examples:
rules/module-single-responsibility.md
rules/table-primary-keys.md
rules/_sections.md
Each rule file contains:
For the complete guide with all rules expanded: AGENTS.md
Weekly Installs
57
Repository
First Seen
Jan 20, 2026
Security Audits
Gen Agent Trust HubPassSocketPassSnykPass
Installed on
opencode51
codex51
gemini-cli50
cursor48
github-copilot45
amp43
Node.js 环境配置指南:多环境管理、类型安全与最佳实践
10,500 周安装