firebase-functions
Firebase Functions Patterns
Directory Structure
functions/
├── src/
│ ├── index.ts # Function exports
│ ├── admin.ts # Firebase Admin SDK init
│ ├── backfill/ # Data migration scripts
│ ├── commentSuggestion/ # AI comment features
│ ├── commentings/ # Comment activity tracking
│ ├── notifications/ # Push notification functions
│ ├── postings/ # Post activity tracking
│ ├── replyings/ # Reply activity tracking
│ └── shared/ # Shared utilities
Function Structure
import { onDocumentCreated } from 'firebase-functions/v2/firestore';
import admin from '../admin';
import { Post } from '../types/Post';
export const createPosting = onDocumentCreated(
'boards/{boardId}/posts/{postId}',
async (event) => {
const postData = event.data?.data() as Post;
const { boardId, postId } = event.params;
if (!postData) {
console.error('No post data found.');
return null;
}
try {
await admin.firestore()
.collection('users')
.doc(postData.authorId)
.collection('postings')
.add(postingData);
console.log(`Created posting for user ${postData.authorId}`);
} catch (error) {
console.error('Error writing posting:', error);
}
return null;
}
);
Error Handling
Don't throw - let function complete gracefully:
try {
await admin.firestore().collection('...').add(data);
console.log(`Successfully created ${resourceType}`);
} catch (error) {
console.error(`Error creating ${resourceType}:`, error);
// Don't throw - function should complete
}
return null;
Build & Test
cd functions && npm install # Install deps
cd functions && npm run build # Compile TypeScript
cd functions && npm test # Run Jest tests
More from bumgeunsong/daily-writing-friends
pr-stacking
PR stacking workflow for breaking large features into smaller, dependent PRs. Use when planning multi-step features, creating dependent branches, or rebasing stacked changes.
29commit
Use when creating git commits in this project
28refactoring
Use when user explicitly asks to refactor code, or when test coverage is requested for untested code with side effects. Enforces Functional Core Imperative Shell pattern extraction before any changes.
28api-layer
Use when creating or modifying API functions in */api/ directories. Enforces Firestore patterns and data fetching conventions.
28code-style
Use when writing or modifying any code. Enforces naming conventions, function design, and code clarity principles.
28react-component
Use when creating or modifying React components (.tsx files). Enforces component structure, import order, and hooks patterns.
28