realtime-database
チャットやリアルタイム同期が必要なアプリ向けに、メッセージ履歴、未読件数、イベントソーシングなどを考慮した、効率的なデータベース設計とクエリ最適化を支援するSkill。
📜 元の英語説明(参考)
When the user needs to design database schemas and queries optimized for real-time applications. Use when the user mentions "chat database," "message storage," "real-time sync," "message history," "unread count," "cursor pagination," "event sourcing," or "live data." Handles schema design for messaging, activity feeds, notifications, and collaborative apps with efficient pagination and sync. For WebSocket transport, see websocket-builder.
🇯🇵 日本人クリエイター向け解説
チャットやリアルタイム同期が必要なアプリ向けに、メッセージ履歴、未読件数、イベントソーシングなどを考慮した、効率的なデータベース設計とクエリ最適化を支援するSkill。
※ jpskill.com 編集部が日本のビジネス現場向けに補足した解説です。Skill本体の挙動とは独立した参考情報です。
下記のコマンドをコピーしてターミナル(Mac/Linux)または PowerShell(Windows)に貼り付けてください。 ダウンロード → 解凍 → 配置まで全自動。
mkdir -p ~/.claude/skills && cd ~/.claude/skills && curl -L -o realtime-database.zip https://jpskill.com/download/15326.zip && unzip -o realtime-database.zip && rm realtime-database.zip
$d = "$env:USERPROFILE\.claude\skills"; ni -Force -ItemType Directory $d | Out-Null; iwr https://jpskill.com/download/15326.zip -OutFile "$d\realtime-database.zip"; Expand-Archive "$d\realtime-database.zip" -DestinationPath $d -Force; ri "$d\realtime-database.zip"
完了後、Claude Code を再起動 → 普通に「動画プロンプト作って」のように話しかけるだけで自動発動します。
💾 手動でダウンロードしたい(コマンドが難しい人向け)
- 1. 下の青いボタンを押して
realtime-database.zipをダウンロード - 2. ZIPファイルをダブルクリックで解凍 →
realtime-databaseフォルダができる - 3. そのフォルダを
C:\Users\あなたの名前\.claude\skills\(Win)または~/.claude/skills/(Mac)へ移動 - 4. Claude Code を再起動
⚠️ ダウンロード・利用は自己責任でお願いします。当サイトは内容・動作・安全性について責任を負いません。
🎯 このSkillでできること
下記の説明文を読むと、このSkillがあなたに何をしてくれるかが分かります。Claudeにこの分野の依頼をすると、自動で発動します。
📦 インストール方法 (3ステップ)
- 1. 上の「ダウンロード」ボタンを押して .skill ファイルを取得
- 2. ファイル名の拡張子を .skill から .zip に変えて展開(macは自動展開可)
- 3. 展開してできたフォルダを、ホームフォルダの
.claude/skills/に置く- · macOS / Linux:
~/.claude/skills/ - · Windows:
%USERPROFILE%\.claude\skills\
- · macOS / Linux:
Claude Code を再起動すれば完了。「このSkillを使って…」と話しかけなくても、関連する依頼で自動的に呼び出されます。
詳しい使い方ガイドを見る →- 最終更新
- 2026-05-18
- 取得日時
- 2026-05-18
- 同梱ファイル
- 1
📖 Skill本文(日本語訳)
※ 原文(英語/中国語)を Gemini で日本語化したものです。Claude 自身は原文を読みます。誤訳がある場合は原文をご確認ください。
リアルタイムデータベース
概要
チャット、アクティビティフィード、通知、共同編集などのリアルタイムアプリケーション向けに最適化されたデータベーススキーマとクエリパターンを設計します。効率的なメッセージストレージ、カーソルベースのページネーション、未読追跡、および再接続時のデータ転送を最小限に抑える同期プロトコルに焦点を当てます。
指示
1. メッセージングのためのスキーマ設計
チャットシステムのコアテーブル:
-- Channels (direct messages + groups)
CREATE TABLE channels (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
type VARCHAR(10) NOT NULL CHECK (type IN ('direct', 'group')),
name VARCHAR(100),
created_at TIMESTAMPTZ DEFAULT now()
);
-- Channel membership with read tracking
CREATE TABLE channel_members (
channel_id UUID REFERENCES channels(id),
user_id UUID NOT NULL,
role VARCHAR(20) DEFAULT 'member',
last_read_message_id BIGINT,
joined_at TIMESTAMPTZ DEFAULT now(),
PRIMARY KEY (channel_id, user_id)
);
-- Messages with sequential IDs for ordering
CREATE TABLE messages (
id BIGSERIAL PRIMARY KEY,
channel_id UUID REFERENCES channels(id),
sender_id UUID NOT NULL,
content TEXT NOT NULL,
reply_to_id BIGINT REFERENCES messages(id),
created_at TIMESTAMPTZ DEFAULT now(),
updated_at TIMESTAMPTZ,
deleted_at TIMESTAMPTZ
);
CREATE INDEX idx_messages_channel_cursor
ON messages(channel_id, id DESC) WHERE deleted_at IS NULL;
メッセージ ID には BIGSERIAL を使用します。これは、シーケンシャルでソート可能であり、カーソルページネーションに最適です。
2. カーソルベースのページネーション
メッセージ履歴に OFFSET を絶対に使用しないでください。これは O(n) であり、新しいメッセージが到着すると結果がシフトします。
-- Load 50 messages before a cursor (scrolling up)
SELECT id, sender_id, content, created_at
FROM messages
WHERE channel_id = $1 AND id < $2 AND deleted_at IS NULL
ORDER BY id DESC
LIMIT 50;
-- Load messages after a cursor (sync on reconnect)
SELECT id, sender_id, content, created_at
FROM messages
WHERE channel_id = $1 AND id > $2 AND deleted_at IS NULL
ORDER BY id ASC;
カーソルメタデータを返します: { messages: [...], nextCursor: 12345, hasMore: true }
3. 未読数の追跡
last_read_message_id アプローチを使用します。これは、ユーザーごとにチャネルごとに1つの整数です。
-- Get unread count for a user across all channels
SELECT cm.channel_id, COUNT(m.id) AS unread_count
FROM channel_members cm
JOIN messages m ON m.channel_id = cm.channel_id
AND m.id > COALESCE(cm.last_read_message_id, 0)
AND m.deleted_at IS NULL
AND m.sender_id != $1
WHERE cm.user_id = $1
GROUP BY cm.channel_id
HAVING COUNT(m.id) > 0;
-- Mark channel as read
UPDATE channel_members
SET last_read_message_id = $2
WHERE channel_id = $1 AND user_id = $3;
4. 再接続時の同期
クライアントが再接続するときは、データ転送を最小限に抑えます。
1. Client sends: { lastMessageIds: { "ch_1": 500, "ch_2": 300 } }
2. Server queries: new messages per channel since those IDs
3. If gap > 200 messages: send summary + latest 50 (client should full-reload)
4. Return: { channels: { "ch_1": { messages: [...], hasMore: false } } }
5. ソフトデリートと編集
メッセージは、スレッドの整合性を維持するためにソフトデリートを使用する必要があります。
deleted_atタイムスタンプ — クエリでフィルタリングし、UI に「メッセージは削除されました」と表示しますupdated_atタイムスタンプ — 編集されたメッセージをマークします- 親がソフトデリートされた後でも、
reply_to_id参照を有効に保ちます
例
例 1: SaaS アプリのチャットスキーマ
プロンプト: 「私のプロジェクト管理ツールでチャット用のデータベースを設計してください。ダイレクトメッセージとプロジェクトチャネル。」
出力: チャネル、メンバー、メッセージテーブルを含む完全なマイグレーション。カーソルページネーションクエリ。未読数クエリ。およびインデックス戦略。推定パフォーマンス: 1000万件以上のメッセージを含むメッセージ履歴で10ミリ秒未満。
例 2: アクティビティフィードスキーマ
プロンプト: 「アクティビティフィードが必要です。 'Alex が Task-42 にコメントしました' のようなユーザーアクション。チームフィードのファンアウトが必要です。」
出力: アクター/動詞/オブジェクトパターンを持つイベントテーブル、ユーザーごとのフィードテーブルへの書き込み時のファンアウト、カーソルページネーション、および90日より古いフィードのクリーンアップジョブ。
ガイドライン
- カーソルページネーションには シーケンシャル ID (
BIGSERIAL) を使用します。UUID は作成順にソートできません - OFFSET を絶対に使用しないでください。カーソルページネーションは O(1) であり、OFFSET は O(n) です
- メッセージごとではなく、チャネルごとに読み取りを追跡します。1つの整数対数百万行
- アクセスパターンに合わせてインデックスを作成します。(
channel_id,id DESC) はチャットクエリの90%をカバーします - メッセージをソフトデリートします。ハードデリートは返信チェーンを壊し、ユーザーを混乱させます
- 1億行を超える場合は、
channel_idまたは時間範囲で 大きなテーブルをパーティション分割します - ホットチャネルを Redis にキャッシュします。最近のメッセージとメンバーリスト
📜 原文 SKILL.md(Claudeが読む英語/中国語)を展開
Real-Time Database
Overview
Designs database schemas and query patterns optimized for real-time applications — chat, activity feeds, notifications, collaborative editing. Focuses on efficient message storage, cursor-based pagination, unread tracking, and sync protocols that minimize data transfer on reconnection.
Instructions
1. Schema Design for Messaging
Core tables for a chat system:
-- Channels (direct messages + groups)
CREATE TABLE channels (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
type VARCHAR(10) NOT NULL CHECK (type IN ('direct', 'group')),
name VARCHAR(100),
created_at TIMESTAMPTZ DEFAULT now()
);
-- Channel membership with read tracking
CREATE TABLE channel_members (
channel_id UUID REFERENCES channels(id),
user_id UUID NOT NULL,
role VARCHAR(20) DEFAULT 'member',
last_read_message_id BIGINT,
joined_at TIMESTAMPTZ DEFAULT now(),
PRIMARY KEY (channel_id, user_id)
);
-- Messages with sequential IDs for ordering
CREATE TABLE messages (
id BIGSERIAL PRIMARY KEY,
channel_id UUID REFERENCES channels(id),
sender_id UUID NOT NULL,
content TEXT NOT NULL,
reply_to_id BIGINT REFERENCES messages(id),
created_at TIMESTAMPTZ DEFAULT now(),
updated_at TIMESTAMPTZ,
deleted_at TIMESTAMPTZ
);
CREATE INDEX idx_messages_channel_cursor
ON messages(channel_id, id DESC) WHERE deleted_at IS NULL;
Use BIGSERIAL for message IDs — sequential, sortable, perfect for cursor pagination.
2. Cursor-Based Pagination
Never use OFFSET for message history — it's O(n) and results shift as new messages arrive.
-- Load 50 messages before a cursor (scrolling up)
SELECT id, sender_id, content, created_at
FROM messages
WHERE channel_id = $1 AND id < $2 AND deleted_at IS NULL
ORDER BY id DESC
LIMIT 50;
-- Load messages after a cursor (sync on reconnect)
SELECT id, sender_id, content, created_at
FROM messages
WHERE channel_id = $1 AND id > $2 AND deleted_at IS NULL
ORDER BY id ASC;
Return cursor metadata: { messages: [...], nextCursor: 12345, hasMore: true }
3. Unread Count Tracking
Use the last_read_message_id approach — one integer per user per channel:
-- Get unread count for a user across all channels
SELECT cm.channel_id, COUNT(m.id) AS unread_count
FROM channel_members cm
JOIN messages m ON m.channel_id = cm.channel_id
AND m.id > COALESCE(cm.last_read_message_id, 0)
AND m.deleted_at IS NULL
AND m.sender_id != $1
WHERE cm.user_id = $1
GROUP BY cm.channel_id
HAVING COUNT(m.id) > 0;
-- Mark channel as read
UPDATE channel_members
SET last_read_message_id = $2
WHERE channel_id = $1 AND user_id = $3;
4. Reconnection Sync
When a client reconnects, minimize data transfer:
1. Client sends: { lastMessageIds: { "ch_1": 500, "ch_2": 300 } }
2. Server queries: new messages per channel since those IDs
3. If gap > 200 messages: send summary + latest 50 (client should full-reload)
4. Return: { channels: { "ch_1": { messages: [...], hasMore: false } } }
5. Soft Deletes and Edits
Messages should use soft deletes to maintain thread integrity:
deleted_attimestamp — filter in queries, show "message deleted" in UIupdated_attimestamp — mark edited messages- Keep
reply_to_idreferences valid even after parent is soft-deleted
Examples
Example 1: Chat Schema for SaaS App
Prompt: "Design the database for chat in my project management tool. Direct messages and project channels."
Output: Complete migration with channels, members, messages tables; cursor pagination queries; unread count query; and index strategy. Estimated performance: sub-10ms for message history with 10M+ messages.
Example 2: Activity Feed Schema
Prompt: "I need an activity feed — user actions like 'Alex commented on Task-42'. Need fan-out for team feeds."
Output: Events table with actor/verb/object pattern, fan-out-on-write to per-user feed tables, cursor pagination, and a cleanup job for feeds older than 90 days.
Guidelines
- Use sequential IDs (BIGSERIAL) for cursor pagination — UUIDs can't be sorted by creation order
- Never use OFFSET — cursor pagination is O(1), OFFSET is O(n)
- Track reads per-channel, not per-message — one integer vs. millions of rows
- Index for your access patterns — (channel_id, id DESC) covers 90% of chat queries
- Soft delete messages — hard deletes break reply chains and confuse users
- Partition large tables by channel_id or time range if exceeding 100M rows
- Cache hot channels in Redis — recent messages and member lists