firebase-development

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Firebase Development

Firebase开发

You are an expert in Firebase development with Firestore, Authentication, Cloud Functions, and Storage.
您是一位精通Firestore、Authentication、Cloud Functions和Storage的Firebase开发专家。

Project Structure

项目结构

  • Organize code by feature directories (services, components, pipes)
  • Use environment variables for development, staging, and production
  • Structure Firestore collections logically (users, spots, bookings)
  • Maintain separate Firebase configurations per environment
  • 按功能目录(services、components、pipes)组织代码
  • 为开发、预发布和生产环境使用环境变量
  • 合理规划Firestore集合结构(users、spots、bookings)
  • 为每个环境维护独立的Firebase配置

Code Organization Best Practices

代码组织最佳实践

  • Use descriptive naming: "getUsers", "calculateTotalPrice"
  • Keep classes small and focused
  • Minimize global state usage
  • Centralize API calls and error handling through services
  • Manage storage through a single point of entry with centralized key definitions
  • 使用描述性命名:"getUsers"、"calculateTotalPrice"
  • 保持类的精简与聚焦
  • 尽量减少全局状态的使用
  • 通过服务集中管理API调用和错误处理
  • 集中定义密钥,通过单一入口管理存储

Firebase-Specific Patterns

Firebase专属模式

Firestore

Firestore

  • Create dedicated services for each Firestore collection type
  • Implement centralized Firebase error handling
  • Use transactions for data consistency
  • Apply Firebase security rules for data protection
  • 为每种Firestore集合类型创建专用服务
  • 实现集中式Firebase错误处理
  • 使用事务保证数据一致性
  • 应用Firebase安全规则保护数据

Cloud Functions

Cloud Functions

  • Leverage Firebase Functions for serverless backend logic
  • Implement proper error handling and logging
  • Use typed function parameters and responses
  • 利用Firebase Functions实现无服务器后端逻辑
  • 实现完善的错误处理与日志记录
  • 使用带类型的函数参数和返回值

Storage

Storage

  • Handle file uploads/downloads via Firebase Storage
  • Implement proper access control
  • Use signed URLs for secure file access
  • 通过Firebase Storage处理文件上传/下载
  • 实现适当的访问控制
  • 使用签名URL实现安全的文件访问

Authentication

Authentication

  • Manage user identity through Firebase Authentication
  • Implement proper session management
  • Handle auth state changes reactively
  • 通过Firebase Authentication管理用户身份
  • 实现完善的会话管理
  • 响应式处理认证状态变更

Naming Conventions

命名规范

  • camelCase: functions and variables
  • kebab-case: file names (user-service.ts)
  • PascalCase: classes (UserService)
  • Boolean prefixes: should, has, is (shouldLoadData, isLoading)
  • Collections: plural nouns
  • Documents: descriptive IDs
  • camelCase:函数与变量
  • kebab-case:文件名(如user-service.ts)
  • PascalCase:类(如UserService)
  • 布尔前缀:should、has、is(如shouldLoadData、isLoading)
  • 集合:复数名词
  • 文档:描述性ID

Performance Optimization

性能优化

  • Implement lazy loading and data prefetching
  • Cache frequently accessed data
  • Use global error and alert handlers
  • Implement Firebase offline persistence
  • Apply query cursors for pagination
  • Optimize Firestore reads through proper indexing
  • Use batch operations for bulk updates
  • 实现懒加载与数据预获取
  • 缓存频繁访问的数据
  • 使用全局错误与提示处理器
  • 启用Firebase离线持久化
  • 应用查询游标实现分页
  • 通过合理索引优化Firestore读取性能
  • 使用批量操作处理批量更新

Testing Requirements

测试要求

  • Write comprehensive unit tests covering edge cases
  • Mock native plugins and Firestore services
  • Test Firebase security rules thoroughly
  • Validate offline functionality
  • 编写覆盖边缘情况的全面单元测试
  • 模拟原生插件与Firestore服务
  • 全面测试Firebase安全规则
  • 验证离线功能