spacetimedb-best-practices について
spacetimedb-best-practices は、SpacetimeDB のサーバーモジュールとクライアント向けの TypeScript 開発ガイドで、React 連携も扱います。優先度別のルールで、スキーマ設計、Reducer、サブスクリプション、クライアント状態、リアルタイム同期を説明します。
主な機能
- 018カテゴリの優先度付きルール
- 02モジュールとテーブルスキーマの指針
- 03Reducer の検証と認可
- 04サブスクリプションとクライアント状態の実践
- 05React とリアルタイム同期の指針
活用シーン
- 01SpacetimeDB のテーブルとインデックス設計
- 02サーバー Reducer の実装
- 03React クライアントへのサブスクリプション統合
インストール
コマンドをコピーしてターミナルで実行します。
$
npx skills add https:/ / github.com/ isyn/ stdb-skills --skill spacetimedb-best-practicesインストール後、Claude Code で/spacetimedb-best-practicesを入力して読み込まれたか確認します。
同梱ファイル
SKILL.md と一緒にインストールされる参考資料やスクリプト。
metadata.jsonREADME.mdrules/_sections.md_template.mdclient-connection-lifecycle.mdmodule-error-handling.mdmodule-lifecycle.mdmodule-single-responsibility.mdreact-reducer-hooks.mdreact-table-hooks.mdreducer-atomicity.mdreducer-authorization.mdreducer-validation.mdsubscription-selective.mdsync-debounce-updates.mdtable-indexing.mdtable-primary-keys.mdts-generated-types.md詳細
SpacetimeDB のベストプラクティス
TypeScript サーバーモジュールと React とのクライアント SDK 統合の両方を対象とした、SpacetimeDB アプリケーションの包括的な開発ガイドです。自動リファクタリングとコード生成の指針として、影響度順に優先順位付けされた 8 つのカテゴリのルールを掲載しています。
パッケージ: spacetimedb(v1.4.0+)
適用するタイミング
以下の場合に、このガイドラインを参照してください。
- TypeScript で SpacetimeDB サーバーモジュールを記述する
- テーブルスキーマとインデックスを設計する
- 状態変更用の Reducer を実装する
- クライアントのサブスクリプションとクエリを設定する
- SpacetimeDB を React アプリケーションに統合する
- リアルタイム同期のパフォーマンスを最適化する
優先度別のルールカテゴリ
| 優先度 | カテゴリ | 影響度 | プレフィックス |
|---|---|---|---|
| 1 | モジュール設計 | CRITICAL | module- |
| 2 | テーブルスキーマとインデックス作成 | CRITICAL | table- |
| 3 | Reducer パターン | HIGH | reducer- |
| 4 | サブスクリプションの最適化 | HIGH | subscription- |
| 5 | クライアントの状態管理 | MEDIUM-HIGH | client- |
| 6 | React 統合 | MEDIUM | react- |
| 7 | TypeScript パターン | MEDIUM | ts- |
| 8 | リアルタイム同期 | LOW-MEDIUM | sync- |
クイックリファレンス
サーバーモジュール API(TypeScript)
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 */ });
クライアント SDK API(TypeScript)
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);
React 統合
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>);
}
ルールカテゴリ
1. モジュール設計(CRITICAL)
module-single-responsibility- ドメイン概念ごとにモジュールを 1 つにするmodule-lifecycle- ライフサイクルフック(init、clientConnected、clientDisconnected)を適切に使用するmodule-error-handling- モジュールのコードでエラーを適切に処理するmodule-type-exports- クライアントで使用する型をエクスポートする
2. テーブルスキーマとインデックス作成(CRITICAL)
table-primary-keys- 適切な主キー戦略を選択するtable-indexing- 頻繁にクエリされる列に.index()を追加するtable-relationships- テーブル間のリレーションシップを正しくモデル化するtable-column-types- 適切な SpacetimeDB 型を使用する
3. Reducer パターン(HIGH)
reducer-atomicity- Reducer をアトミックに保ち、処理を明確にするreducer-validation- Reducer の開始時に入力を検証するreducer-authorization- 機密性の高い操作では呼び出し元の ID を確認するreducer-batch-operations- 関連する変更を 1 つの Reducer でまとめて処理する
4. サブスクリプションの最適化(HIGH)
subscription-selective- 必要なデータのみをサブスクライブするsubscription-filters- サブスクリプションフィルターを使用してデータ転送量を減らすsubscription-cleanup- 不要になったサブスクリプションをクリーンアップするsubscription-batching- クライアント接続時にサブスクリプションの設定をまとめて行う
5. クライアントの状態管理(MEDIUM-HIGH)
client-connection-lifecycle- 接続と再接続を適切に処理するclient-optimistic-updates- UI の応答性を高めるために楽観的更新を使用するclient-error-recovery- Reducer のエラーを適切に処理するclient-identity- ID トークンを安全に管理する
6. React 統合(MEDIUM)
react-use-subscription- サブスクリプションフックを正しく使用するreact-table-hooks- リアクティブデータにはuseTable<DbConnection, Type>()を使用するreact-reducer-hooks- 適切なエラー処理とともにconn.reducers.*を呼び出すreact-connection-status- 接続状態をユーザーに表示する
7. TypeScript パターン(MEDIUM)
ts-generated-types- SpacetimeDB CLI で生成された型を使用するts-strict-mode- 型安全性を高めるために TypeScript の strict モードを有効にするts-discriminated-unions- 状態の表現に判別可能なユニオンを使用するts-type-guards- 実行時検証のために型ガードを実装する
8. リアルタイム同期(LOW-MEDIUM)
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 をご覧ください。
コメント
Markdown 対応