[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"skill:01M6EG0ENVH83VYPR8N0V9R07H":3,"skill-same:01M6EG0ENVH83VYPR8N0V9R07H":48},{"skillNo":4,"name":5,"nameZh":6,"description":6,"descriptionZh":6,"repositoryFullName":7,"ownerLogin":8,"ownerAvatarUrl":9,"ownerAccountNo":10,"ownerDisplayName":11,"repositoryNo":12,"starsCount":13,"licenseSpdxId":14,"licenseFileUrl":6,"sourceHtmlUrl":15,"categories":16,"keywords":17,"publishedAt":21,"linkOnly":22,"markdownContent":23,"markdownBody":23,"frontMatter":24,"refName":25,"commitSha":26,"skillDirectory":27,"forksCount":28,"repositoryHtmlUrl":29,"repositoryHomepageUrl":30,"ownerHtmlUrl":31,"ownerBlogUrl":6,"ownerDescription":32,"updatedAt":33,"install":34,"resources":47},"01M6EG0ENVH83VYPR8N0V9R07H","realtime-database",null,"ComeOnOliver\u002Fskillshub","ComeOnOliver","https:\u002F\u002Favatars.githubusercontent.com\u002Fu\u002F43215937?v=4","01M6E5JNFECJAAVFE0W7SMWWD2","Haoran Shu","01M6E5JNNDQS7AJZDYFQ8TPZCK",67,"MIT","https:\u002F\u002Fgithub.com\u002FComeOnOliver\u002Fskillshub\u002Fblob\u002Fmain\u002Fskills\u002FTerminalSkills\u002Fskills\u002Frealtime-database\u002FSKILL.md",[],[18,19,20],"realtime","database","realtime database","2026-11-10 10:12:30",false,"# Real-Time Database\n\n## Overview\n\nDesigns 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.\n\n## Instructions\n\n### 1. Schema Design for Messaging\n\nCore tables for a chat system:\n\n```sql\n-- Channels (direct messages + groups)\nCREATE TABLE channels (\n  id UUID PRIMARY KEY DEFAULT gen_random_uuid(),\n  type VARCHAR(10) NOT NULL CHECK (type IN ('direct', 'group')),\n  name VARCHAR(100),\n  created_at TIMESTAMPTZ DEFAULT now()\n);\n\n-- Channel membership with read tracking\nCREATE TABLE channel_members (\n  channel_id UUID REFERENCES channels(id),\n  user_id UUID NOT NULL,\n  role VARCHAR(20) DEFAULT 'member',\n  last_read_message_id BIGINT,\n  joined_at TIMESTAMPTZ DEFAULT now(),\n  PRIMARY KEY (channel_id, user_id)\n);\n\n-- Messages with sequential IDs for ordering\nCREATE TABLE messages (\n  id BIGSERIAL PRIMARY KEY,\n  channel_id UUID REFERENCES channels(id),\n  sender_id UUID NOT NULL,\n  content TEXT NOT NULL,\n  reply_to_id BIGINT REFERENCES messages(id),\n  created_at TIMESTAMPTZ DEFAULT now(),\n  updated_at TIMESTAMPTZ,\n  deleted_at TIMESTAMPTZ\n);\n\nCREATE INDEX idx_messages_channel_cursor\n  ON messages(channel_id, id DESC) WHERE deleted_at IS NULL;\n```\n\nUse BIGSERIAL for message IDs — sequential, sortable, perfect for cursor pagination.\n\n### 2. Cursor-Based Pagination\n\nNever use OFFSET for message history — it's O(n) and results shift as new messages arrive.\n\n```sql\n-- Load 50 messages before a cursor (scrolling up)\nSELECT id, sender_id, content, created_at\nFROM messages\nWHERE channel_id = $1 AND id \u003C $2 AND deleted_at IS NULL\nORDER BY id DESC\nLIMIT 50;\n\n-- Load messages after a cursor (sync on reconnect)\nSELECT id, sender_id, content, created_at\nFROM messages\nWHERE channel_id = $1 AND id > $2 AND deleted_at IS NULL\nORDER BY id ASC;\n```\n\nReturn cursor metadata: `{ messages: [...], nextCursor: 12345, hasMore: true }`\n\n### 3. Unread Count Tracking\n\nUse the `last_read_message_id` approach — one integer per user per channel:\n\n```sql\n-- Get unread count for a user across all channels\nSELECT cm.channel_id, COUNT(m.id) AS unread_count\nFROM channel_members cm\nJOIN messages m ON m.channel_id = cm.channel_id\n  AND m.id > COALESCE(cm.last_read_message_id, 0)\n  AND m.deleted_at IS NULL\n  AND m.sender_id != $1\nWHERE cm.user_id = $1\nGROUP BY cm.channel_id\nHAVING COUNT(m.id) > 0;\n\n-- Mark channel as read\nUPDATE channel_members\nSET last_read_message_id = $2\nWHERE channel_id = $1 AND user_id = $3;\n```\n\n### 4. Reconnection Sync\n\nWhen a client reconnects, minimize data transfer:\n\n```\n1. Client sends: { lastMessageIds: { \"ch_1\": 500, \"ch_2\": 300 } }\n2. Server queries: new messages per channel since those IDs\n3. If gap > 200 messages: send summary + latest 50 (client should full-reload)\n4. Return: { channels: { \"ch_1\": { messages: [...], hasMore: false } } }\n```\n\n### 5. Soft Deletes and Edits\n\nMessages should use soft deletes to maintain thread integrity:\n\n- `deleted_at` timestamp — filter in queries, show \"message deleted\" in UI\n- `updated_at` timestamp — mark edited messages\n- Keep `reply_to_id` references valid even after parent is soft-deleted\n\n## Examples\n\n### Example 1: Chat Schema for SaaS App\n\n**Prompt**: \"Design the database for chat in my project management tool. Direct messages and project channels.\"\n\n**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.\n\n### Example 2: Activity Feed Schema\n\n**Prompt**: \"I need an activity feed — user actions like 'Alex commented on Task-42'. Need fan-out for team feeds.\"\n\n**Output**: Events table with actor\u002Fverb\u002Fobject pattern, fan-out-on-write to per-user feed tables, cursor pagination, and a cleanup job for feeds older than 90 days.\n\n## Guidelines\n\n- **Use sequential IDs** (BIGSERIAL) for cursor pagination — UUIDs can't be sorted by creation order\n- **Never use OFFSET** — cursor pagination is O(1), OFFSET is O(n)\n- **Track reads per-channel, not per-message** — one integer vs. millions of rows\n- **Index for your access patterns** — (channel_id, id DESC) covers 90% of chat queries\n- **Soft delete messages** — hard deletes break reply chains and confuse users\n- **Partition large tables** by channel_id or time range if exceeding 100M rows\n- **Cache hot channels** in Redis — recent messages and member lists\n",{},"main","def8531e65114c0fca8fb8551c1871ee0eed705c","skills\u002FTerminalSkills\u002Fskills\u002Frealtime-database",24,"https:\u002F\u002Fgithub.com\u002FComeOnOliver\u002Fskillshub","https:\u002F\u002Fskillshub.wtf","https:\u002F\u002Fgithub.com\u002FComeOnOliver","AI & Blockchain Engineer\r\n","2026-06-24 22:07:19",{"source":7,"skillName":5,"commands":35,"prompt":45,"zipAvailable":46},[36,39,42],{"tool":37,"command":38},"npx","npx skills add ComeOnOliver\u002Fskillshub --skill realtime-database",{"tool":40,"command":41},"bunx","bunx skills add ComeOnOliver\u002Fskillshub --skill realtime-database",{"tool":43,"command":44},"pnpm","pnpm dlx skills add ComeOnOliver\u002Fskillshub --skill realtime-database","安装技能，技能地址：https:\u002F\u002Fgithub.com\u002FComeOnOliver\u002Fskillshub\u002Fblob\u002Fmain\u002Fskills\u002FTerminalSkills\u002Fskills\u002Frealtime-database\u002FSKILL.md",true,[],[49,58,67,76,82,88],{"skillNo":50,"name":51,"nameZh":6,"description":6,"descriptionZh":6,"repositoryFullName":7,"ownerLogin":8,"ownerAvatarUrl":9,"ownerAccountNo":10,"ownerDisplayName":11,"repositoryNo":12,"starsCount":13,"licenseSpdxId":14,"licenseFileUrl":6,"sourceHtmlUrl":52,"categories":53,"keywords":54,"publishedAt":21},"01M6FKJN13GMG4KY57J3QD8GY5","model-hierarchy-skill","https:\u002F\u002Fgithub.com\u002FComeOnOliver\u002Fskillshub\u002Fblob\u002Fmain\u002Fskills\u002Fzscole\u002Fmodel-hierarchy-skill\u002Fmodel-hierarchy-skill\u002FSKILL.md",[],[55,56,57],"model","model hierarchy","model hierarchy skill",{"skillNo":59,"name":60,"nameZh":6,"description":6,"descriptionZh":6,"repositoryFullName":7,"ownerLogin":8,"ownerAvatarUrl":9,"ownerAccountNo":10,"ownerDisplayName":11,"repositoryNo":12,"starsCount":13,"licenseSpdxId":14,"licenseFileUrl":6,"sourceHtmlUrl":61,"categories":62,"keywords":63,"publishedAt":21},"01M6FKJJJ6GCAMR4PPPRD12M5X","zoom-mcp","https:\u002F\u002Fgithub.com\u002FComeOnOliver\u002Fskillshub\u002Fblob\u002Fmain\u002Fskills\u002Fzoom\u002Fskills\u002Fzoom-mcp\u002FSKILL.md",[],[64,65,66],"MCP","zoom","zoom mcp",{"skillNo":68,"name":69,"nameZh":6,"description":6,"descriptionZh":6,"repositoryFullName":7,"ownerLogin":8,"ownerAvatarUrl":9,"ownerAccountNo":10,"ownerDisplayName":11,"repositoryNo":12,"starsCount":13,"licenseSpdxId":14,"licenseFileUrl":6,"sourceHtmlUrl":70,"categories":71,"keywords":72,"publishedAt":21},"01M6FKJE3KYT3KPEZMEEHYKA6K","zoom-apps-sdk","https:\u002F\u002Fgithub.com\u002FComeOnOliver\u002Fskillshub\u002Fblob\u002Fmain\u002Fskills\u002Fzoom\u002Fskills\u002Fzoom-apps-sdk\u002FSKILL.md",[],[73,74,75,65],"sdk","zoom apps sdk","apps",{"skillNo":77,"name":78,"nameZh":6,"description":6,"descriptionZh":6,"repositoryFullName":7,"ownerLogin":8,"ownerAvatarUrl":9,"ownerAccountNo":10,"ownerDisplayName":11,"repositoryNo":12,"starsCount":13,"licenseSpdxId":14,"licenseFileUrl":6,"sourceHtmlUrl":79,"categories":80,"keywords":81,"publishedAt":21},"01M6FKJ9EDH9Q5VWDF8HAJRSP0","windows","https:\u002F\u002Fgithub.com\u002FComeOnOliver\u002Fskillshub\u002Fblob\u002Fmain\u002Fskills\u002Fzoom\u002Fskills\u002Fwindows\u002FSKILL.md",[],[78],{"skillNo":83,"name":84,"nameZh":6,"description":6,"descriptionZh":6,"repositoryFullName":7,"ownerLogin":8,"ownerAvatarUrl":9,"ownerAccountNo":10,"ownerDisplayName":11,"repositoryNo":12,"starsCount":13,"licenseSpdxId":14,"licenseFileUrl":6,"sourceHtmlUrl":85,"categories":86,"keywords":87,"publishedAt":21},"01M6FKJ666BEEFC3AE9GJ72Q93","whiteboard","https:\u002F\u002Fgithub.com\u002FComeOnOliver\u002Fskillshub\u002Fblob\u002Fmain\u002Fskills\u002Fzoom\u002Fskills\u002Fwhiteboard\u002FSKILL.md",[],[84],{"skillNo":89,"name":90,"nameZh":6,"description":6,"descriptionZh":6,"repositoryFullName":7,"ownerLogin":8,"ownerAvatarUrl":9,"ownerAccountNo":10,"ownerDisplayName":11,"repositoryNo":12,"starsCount":13,"licenseSpdxId":14,"licenseFileUrl":6,"sourceHtmlUrl":91,"categories":92,"keywords":93,"publishedAt":21},"01M6FKJ21WFCDGTJQ2Q91T9DVW","websockets","https:\u002F\u002Fgithub.com\u002FComeOnOliver\u002Fskillshub\u002Fblob\u002Fmain\u002Fskills\u002Fzoom\u002Fskills\u002Fwebsockets\u002FSKILL.md",[],[90]]