
关于
Firebase 让你在几分钟内拥有完整后端——认证、数据库、存储、托管等一站式服务。
name: firebase description: Firebase 让你在几分钟内获得完整后端——认证、数据库、存储、函数、托管。但简单的设置背后隐藏着真正的复杂性。 risk: unknown source: vibeship-spawner-skills (Apache 2.0) date_added: 2026-02-27
Firebase
Firebase 让你在几分钟内获得完整后端——认证、数据库、存储、函数、托管。但简单的设置背后隐藏着真正的复杂性。安全规则是你的最后一道防线,而它们经常是错误的。Firestore 查询是有限的,你在设计完数据模型之后才会发现这一点。
本技能涵盖 Firebase Authentication、Firestore、Realtime Database、Cloud Functions、Cloud Storage 和 Firebase Hosting。关键洞察:Firebase 针对读密集型、反规范化数据进行了优化。如果你在用关系型思维思考,那就错了。
2025 年教训:Firestore 定价可能让你大吃一惊。读取很便宜,直到它们不再便宜。一个设计不良的监听器可能比专用数据库花费更多。根据查询模式而非数据关系来规划数据模型。
原则
- 为查询设计数据,而非为关系
- 安全规则是强制性的,不是可选的
- 积极反规范化——重复是廉价的,连接是昂贵的
- 使用批量写入和事务保证一致性
- 明智使用离线持久化——它不是免费的
- Cloud Functions 用于客户端不应该做的事情
- 基于环境的配置,永远不要在客户端硬编码密钥
能力
- firebase-auth
- firestore
- firebase-realtime-database
- firebase-cloud-functions
- firebase-storage
- firebase-hosting
- firebase-security-rules
- firebase-admin-sdk
- firebase-emulators
范围
- general-backend-architecture -> backend
- payment-processing -> stripe
- email-sending -> email
- advanced-auth-flows -> authentication-oauth
- kubernetes-deployment -> devops
工具
核心
- firebase - 何时使用:客户端 SDK 注意:模块化 SDK - 可 tree-shake
- firebase-admin - 何时使用:服务端 / Cloud Functions 注意:完全访问,绕过安全规则
- firebase-functions - 何时使用:Cloud Functions v2 注意:推荐使用 v2 函数
测试
- @firebase/rules-unit-testing - 何时使用:测试安全规则 注意:必需——规则 bug 就是安全 bug
- firebase-tools - 何时使用:模拟器套件 注意:本地开发无需访问生产环境
框架
- reactfire - 何时使用:React + Firebase 注意:基于 Hooks,处理订阅
- vuefire - 何时使用:Vue + Firebase 注意:Vue 特定绑定
- angularfire - 何时使用:Angular + Firebase 注意:官方 Angular 绑定
模式
模块化 SDK 导入
只导入你需要的内容以获得更小的包
何时使用:客户端 Firebase 使用
模块化导入:
""" Firebase v9+ 使用模块化 SDK。只导入你需要的内容。 这启用了 tree-shaking 和更小的包。 """
// WRONG: v8-compat style (larger bundle)
import firebase from 'firebase/compat/app';
import 'firebase/compat/firestore';
const db = firebase.firestore();
// RIGHT: v9+ modular (tree-shakeable)
import { initializeApp } from 'firebase/app';
import { getFirestore, collection, doc, getDoc } from 'firebase/firestore';
const app = initializeApp(firebaseConfig);
const db = getFirestore(app);
// Get a document
const docRef = doc(db, 'users', 'userId');
const docSnap = await getDoc(docRef);
if (docSnap.exists()) {
console.log(docSnap.data());
}
// Query with constraints
import { query, where, orderBy, limit } from 'firebase/firestore';
const q = query(
collection(db, 'posts'),
where('published', '==', true),
orderBy('createdAt', 'desc'),
limit(10)
);
安全规则设计
从第一天起就用适当的规则保护你的数据
何时使用:任何 Firestore 数据库
FIRESTORE 安全规则:
""" 规则是你的最后一道防线。每次读写都要经过它们。 搞错了,你的数据就暴露了。 """
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
// Helper functions
function isSignedIn() {
return request.auth != null;
}
function isOwner(userId) {
return request.auth.uid == userId;
}
function isAdmin() {
return request.auth.token.admin == true;
}
// Users collection
match /users/{userId} {
allow read: if true;
allow write: if isOwner(userId);
match /private/{document=**} {
allow read, write: if isOwner(userId);
}
}
// Posts collection
match /posts/{postId} {
allow read: if resource.data.published == true
|| isOwner(resource.data.authorId);
allow create: if isSignedIn()
&& request.resource.data.authorId == request.auth.uid;
allow update, delete: if isOwner(resource.data.authorId);
}
}
}
限制
- 仅在任务明确匹配上述描述范围时使用此技能。
- 不要将输出视为环境特定验证、测试或专家审查的替代品。
- 如果缺少所需输入、权限、安全边界或成功标准,请停下来要求澄清。
兼容工具
Claude CodeCursor
标签
运维部署

