
Cloud
Learning Level
// Multi-region configuration
const regions = {'
us: {'
database: 'my-app-us.firebaseio.com',
projectId: 'my-app-us',
region: 'us-central1'
},
eu: {'
database: 'my-app-eu.firebaseio.com',
projectId: 'my-app-eu',
region: 'europe-west1'
},
asia: {'
database: 'my-app-asia.firebaseio.com',
projectId: 'my-app-asia',
region: 'asia-east1'
}
};
// Route users to nearest region
function getUserRegion(userLocation) {
const [latitude, longitude] = userLocation;
return determineOptimalRegion(latitude, longitude);
}import { getPerformance, trace } from "firebase/performance";
const perf = getPerformance();
async function optimizedDataFetch(query) {
const myTrace = trace(perf, 'data-fetch');
myTrace.start();
const result = await query.get();
myTrace.putAttribute('doc_count', result.docs.length);
myTrace.putMetric('fetch_time_ms', Date.now() - startTime);
myTrace.stop();
return result;
}import admin from 'firebase-admin';
async function createCustomToken(userId, claims) {
const token = await admin.auth().createCustomToken(userId, {
role: claims.role,
team: claims.team,
issuedAt: admin.firestore.FieldValue.serverTimestamp()
});
return token;
}exports.auditDataChanges = functions.firestore
.document('sensitive-data/{docId}')
.onWrite(async (change, context) => {
const auditEntry = {
timestamp: admin.firestore.FieldValue.serverTimestamp(),
userId: context.auth.uid,
action: change.before.exists ? 'update' : 'create',
before: change.before.data(),
after: change.after.data(),
documentId: context.params.docId
};
await admin.firestore().collection('audit_logs').add(auditEntry);
});// Implement caching with TTL
const cache = new Map();
const CACHE_TTL = 5 * 60 * 1000;
async function getCachedData(key, fetchFn) {
const cached = cache.get(key);
if (cached && Date.now() - cached.timestamp < CACHE_TTL) {
return cached.data;
}
const data = await fetchFn();
cache.set(key, { data, timestamp: Date.now() });
return data;
}Implement advanced messaging patterns with Cloud Functions, or design real-time collaborative systems.
Resources
Ojasa Mirai
Master AI-powered development skills through structured learning, real projects, and verified credentials. Whether you're upskilling your team or launching your career, we deliver the skills companies actually need.
Learn Deep โข Build Real โข Verify Skills โข Launch Forward