Context Helpers
After <FirebaseApp> mounts, it sets raw Firebase service instances in Svelte context. Use these helpers to access them in any descendant component.
Available helpers
Section titled “Available helpers”import { getFirebaseAppContext, getAuthContext, getFirestoreContext, getStorageContext, getRTDBContext, getFunctionsContext, getAppCheckContext, getAnalyticsContext, getMessagingContext,} from 'svelte-firekit';| Helper | Returns | Firebase SDK type |
|---|---|---|
getFirebaseAppContext() | FirebaseApp | The initialized Firebase app instance |
getAuthContext() | Auth | Firebase Auth instance |
getFirestoreContext() | Firestore | Firestore instance |
getStorageContext() | FirebaseStorage | Storage instance |
getRTDBContext() | Database | Realtime Database instance |
getFunctionsContext() | Functions | Cloud Functions instance |
getAppCheckContext() | AppCheck | null | App Check instance (or null if not enabled) |
getAnalyticsContext() | Analytics | null | Analytics instance (or null if not initialized) |
getMessagingContext() | Messaging | null | FCM instance (or null if not initialized) |
Example
Section titled “Example”Access Firestore directly to run a custom query not covered by firekitCollection:
<script lang="ts"> import { getFirestoreContext } from 'svelte-firekit'; import { collection, query, where, getDocs } from 'firebase/firestore';
const db = getFirestoreContext();
async function customQuery() { const q = query( collection(db, 'posts'), where('authorId', '==', userId), where('tags', 'array-contains', 'svelte') ); const snap = await getDocs(q); return snap.docs.map(d => ({ id: d.id, ...d.data() })); }</script>Use with Transactions
Section titled “Use with Transactions”<script lang="ts"> import { getFirestoreContext, firekitMutations } from 'svelte-firekit'; import { doc } from 'firebase/firestore';
const db = getFirestoreContext();
async function incrementCounter() { const ref = doc(db, 'counters/clicks'); await firekitMutations.transaction(async (tx) => { const snap = await tx.get(ref); const count = snap.data()?.count ?? 0; tx.update(ref, { count: count + 1 }); }); }</script>Use with Storage
Section titled “Use with Storage”<script lang="ts"> import { getStorageContext } from 'svelte-firekit'; import { ref, listAll } from 'firebase/storage';
const storage = getStorageContext();
async function listUserFiles(uid: string) { const dirRef = ref(storage, `users/${uid}/uploads`); const result = await listAll(dirRef); return result.items; }</script>