Skip to content

Commit 4ac7b3b

Browse files
committed
fix: add firebaseSDK to store args
1 parent eb7ae9e commit 4ac7b3b

26 files changed

+116
-357
lines changed

README.md

+25-30
Original file line numberDiff line numberDiff line change
@@ -38,28 +38,21 @@ Svelte makes it possible to dramatically simplify the way developers work with F
3838

3939
## Quick Start
4040

41-
1. Install Firebase `npm i firebase` v9+ and initialize it in a layout `+layout.svelte`:
41+
1. Install Firebase npm i firebase v9+ and initialize it in a file like lib/firebase.js:
4242

4343
```
4444
npm i sveltefire firebase
4545
```
4646

47-
```svelte
48-
<script lang="ts">
49-
import { FirebaseApp } from 'sveltefire';
50-
import { initializeApp } from 'firebase/app';
51-
import { getFirestore } from 'firebase/firestore';
52-
import { getAuth } from 'firebase/auth';
53-
54-
// Initialize Firebase
55-
const app = initializeApp(/* your firebase config */);
56-
const db = getFirestore(app);
57-
const auth = getAuth(app);
58-
</script>
59-
60-
<FirebaseApp {auth} {firestore}>
61-
<slot />
62-
</FirebaseApp>
47+
```ts
48+
import { initializeApp } from 'firebase/app';
49+
import { getFirestore } from 'firebase/firestore';
50+
import { getAuth } from 'firebase/auth';
51+
52+
// Initialize Firebase
53+
const app = initializeApp(/* your firebase config */);
54+
export const db = getFirestore(app);
55+
export const auth = getAuth(app);
6356
```
6457

6558
2. Get the Current user
@@ -68,7 +61,7 @@ npm i sveltefire firebase
6861
<script>
6962
import { auth } from '$lib/firebase';
7063
import { userStore } from 'sveltefire';
71-
const user = userStore();
64+
const user = userStore(auth);
7265
</script>
7366
7467
Hello {$user?.uid}
@@ -83,7 +76,7 @@ Use the `$` as much as you want - it will only result in one Firebase read reque
8376
import { firestore } from '$lib/firebase';
8477
import { docStore } from 'sveltefire';
8578
86-
const post = docStore('posts/test');
79+
const post = docStore(firestore, 'posts/test');
8780
</script>
8881
8982
{$post?.content}
@@ -105,7 +98,7 @@ Listen to the current user. Render UI conditionally based on the auth state:
10598
<script>
10699
import { userStore } from 'sveltefire';
107100
108-
const user = userStore();
101+
const user = userStore(auth);
109102
</script>
110103
111104
{#if $user}
@@ -123,11 +116,11 @@ Subscribe to realtime data. The store will unsubscribe automatically to avoid un
123116
<script>
124117
import { docStore, collectionStore } from 'sveltefire';
125118
126-
const post = docStore('posts/test');
119+
const post = docStore(firestore, 'posts/test');
127120
128121
// OR
129122
130-
const posts = collectionStore('posts');
123+
const posts = collectionStore(firestore, 'posts');
131124
</script>
132125
133126
{$post?.content}
@@ -145,8 +138,8 @@ interface Post {
145138
title: string;
146139
content: string;
147140
}
148-
const post = docStore<Post>('posts/test');
149-
const posts = collectionStore<Post>('posts');
141+
const post = docStore<Post>(firestore, 'posts/test');
142+
const posts = collectionStore<Post>(firestore, 'posts');
150143
```
151144

152145
## SSR
@@ -174,7 +167,7 @@ Second, pass the server data as the `startWith` value to a store. This will bypa
174167
export let data: PageData;
175168

176169
// Just give the store a startWith value
177-
const post = docStore('posts/test', data.post);
170+
const post = docStore(firestore, 'posts/test', data.post);
178171
```
179172

180173
## Realtime Components
@@ -183,9 +176,10 @@ In addition to stores, SvelteFire provides a set of components that can build co
183176

184177
### FirebaseApp
185178

186-
The `FirebaseApp` component puts the FirebaseSDK into Svelte context. This avoids the need to pass `auth` and `firestore` down to every component/store. It is typically placed in root layout.
179+
The `FirebaseApp` component puts the FirebaseSDK into Svelte context. This avoids the need to pass `auth` and `firestore` down to every component. It is typically placed in root layout.
187180

188181
```svelte
182+
<!-- +layout.svelte -->
189183
<script>
190184
// Initialize Firebase...
191185
const db = getFirestore(app);
@@ -200,7 +194,7 @@ The `FirebaseApp` component puts the FirebaseSDK into Svelte context. This avoid
200194
</FirebaseApp>
201195
```
202196

203-
You can easily access the Firebase SDK in any component via context. This is useful when using the Firebase SDK directly, which requires the SDK as an argument.
197+
You can use Svelte's context API to access the Firebase SDK in any component.
204198

205199
```svelte
206200
<script>
@@ -295,7 +289,6 @@ These components can be combined to build complex realtime apps. It's especially
295289
<SignedIn let:user>
296290
<p>UID: {user.uid}</p>
297291
298-
299292
<h3>Profile</h3>
300293
<Doc ref={`posts/${user.uid}`} let:data={profile} let:ref={profileRef}>
301294
@@ -313,9 +306,11 @@ These components can be combined to build complex realtime apps. It's especially
313306
314307
<div slot="loading">Loading Profile...</div>
315308
</Doc>
316-
317-
<div slot="signedOut">Signed out</div>
318309
</SignedIn>
310+
311+
<SignedOut>
312+
<p>Sign in to see your profile</p>
313+
</SignedOut>
319314
</FirebaseApp>
320315
```
321316

dist/components/Collection.svelte

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
<script>import { collectionStore } from "../stores/firestore.js";
2+
import { getFirebaseContext } from "../stores/sdk.js";
23
export let ref;
34
export let startWith = void 0;
4-
let store = collectionStore(ref, startWith);
5+
const { firestore } = getFirebaseContext();
6+
let store = collectionStore(firestore, ref, startWith);
57
</script>
68

79
{#if $store !== undefined}
8-
<slot data={$store} ref={store.ref} count={$store?.length ?? 0} />
10+
<slot data={$store} ref={store.ref} count={$store?.length ?? 0} {firestore} />
911
{:else}
1012
<slot name="loading" />
1113
{/if}

dist/components/Collection.svelte.d.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { SvelteComponent } from "svelte";
2-
import type { CollectionReference, Query } from 'firebase/firestore';
2+
import type { CollectionReference, Firestore, Query } from 'firebase/firestore';
33
declare const __propDef: {
44
props: {
55
ref: string | CollectionReference | Query;
@@ -13,6 +13,7 @@ declare const __propDef: {
1313
data: any[];
1414
ref: CollectionReference | Query | null;
1515
count: number;
16+
firestore?: Firestore | undefined;
1617
};
1718
loading: {};
1819
};

dist/components/Doc.svelte

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
<script>import { docStore } from "../stores/firestore.js";
2+
import { getFirebaseContext } from "../stores/sdk.js";
23
export let ref;
34
export let startWith = void 0;
4-
let store = docStore(ref, startWith);
5+
const { firestore } = getFirebaseContext();
6+
let store = docStore(firestore, ref, startWith);
57
</script>
68

79
{#if $store !== undefined}
8-
<slot data={$store} ref={store.ref} />
10+
<slot data={$store} ref={store.ref} {firestore} />
911
{:else}
1012
<slot name="loading" />
1113
{/if}

dist/components/Doc.svelte.d.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { SvelteComponent } from "svelte";
2-
import type { DocumentReference } from 'firebase/firestore';
2+
import type { DocumentReference, Firestore } from 'firebase/firestore';
33
declare const __propDef: {
44
props: {
55
ref: string | DocumentReference;
@@ -12,6 +12,7 @@ declare const __propDef: {
1212
default: {
1313
data: any;
1414
ref: DocumentReference | null;
15+
firestore?: Firestore | undefined;
1516
};
1617
loading: {};
1718
};

dist/components/FirebaseApp.svelte

-1
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,4 @@ export let auth;
44
setFirebaseContext({ firestore, auth });
55
</script>
66

7-
87
<slot />

dist/components/FirebaseApp.svelte.d.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { SvelteComponent } from "svelte";
2-
import type { Auth } from 'firebase/auth';
3-
import type { Firestore } from 'firebase/firestore';
2+
import type { Auth } from "firebase/auth";
3+
import type { Firestore } from "firebase/firestore";
44
declare const __propDef: {
55
props: {
66
firestore: Firestore;

dist/components/SignedIn.svelte

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import { getFirebaseContext } from "../stores/sdk.js";
33
import { signOut } from "firebase/auth";
44
const auth = getFirebaseContext().auth;
5-
const user = userStore();
5+
const user = userStore(auth);
66
</script>
77

88
{#if $user}

dist/components/SignedOut.svelte

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
<script>import { getFirebaseContext } from "../stores/sdk.js";
22
import { userStore } from "../stores/auth.js";
3-
getFirebaseContext;
43
const auth = getFirebaseContext().auth;
5-
const user = userStore();
4+
const user = userStore(auth);
65
</script>
76

87
{#if !$user}

dist/components/User.svelte

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<script>import { userStore } from "../stores/auth.js";
2-
const user = userStore();
2+
import { getFirebaseContext } from "../index.js";
3+
const auth = getFirebaseContext().auth;
4+
const user = userStore(auth);
35
</script>
46

57
{#if $user}

dist/stores/auth.d.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
/// <reference types="svelte" />
2+
import { type Auth } from "firebase/auth";
23
/**
4+
* @param {Auth} auth firebase auth instance
35
* @param {any} startWith optional default data. Useful for server-side cookie-based auth
46
* @returns a store with the current firebase user
57
*/
6-
export declare function userStore(startWith?: null): {
8+
export declare function userStore(auth: Auth, startWith?: null): {
79
subscribe: (this: void, run: import("svelte/store").Subscriber<import("@firebase/auth").User | null>, invalidate?: import("svelte/store").Invalidator<import("@firebase/auth").User | null> | undefined) => import("svelte/store").Unsubscriber;
810
};

dist/stores/auth.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ import { writable } from "svelte/store";
22
import { getFirebaseContext } from "./sdk.js";
33
import { onAuthStateChanged } from "firebase/auth";
44
/**
5+
* @param {Auth} auth firebase auth instance
56
* @param {any} startWith optional default data. Useful for server-side cookie-based auth
67
* @returns a store with the current firebase user
78
*/
8-
export function userStore(startWith = null) {
9+
export function userStore(auth, startWith = null) {
910
let unsubscribe;
10-
const { auth } = getFirebaseContext();
1111
// Fallback for SSR
1212
if (!globalThis.window) {
1313
const { subscribe } = writable(startWith);

dist/stores/firestore.d.ts

+6-9
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,25 @@
1-
import type { Query, CollectionReference, DocumentReference, DocumentData } from "firebase/firestore";
2-
import type { Readable } from "svelte/motion";
1+
import type { Query, CollectionReference, DocumentReference, Firestore } from "firebase/firestore";
32
interface DocStore<T> {
43
subscribe: (cb: (value: T | null) => void) => void | (() => void);
5-
ref: DocumentReference | null;
4+
ref: DocumentReference<T> | null;
65
id: string;
76
}
87
/**
8+
* @param {Firestore} firestore firebase firestore instance
99
* @param {string|DocumentReference} ref document path or reference
1010
* @param {T} startWith optional default data
1111
* @returns a store with realtime updates on document data
1212
*/
13-
export declare function docStore<T>(ref: string | DocumentReference, startWith?: T): DocStore<T>;
13+
export declare function docStore<T = any>(firestore: Firestore, ref: string | DocumentReference<T>, startWith?: T): DocStore<T>;
1414
interface CollectionStore<T> {
1515
subscribe: (cb: (value: T | []) => void) => void | (() => void);
1616
ref: CollectionReference | Query | null;
1717
}
1818
/**
19+
* @param {Firestore} firestore firebase firestore instance
1920
* @param {string|Query|CollectionReference} ref collection path, reference, or query
2021
* @param {[]} startWith optional default data
2122
* @returns a store with realtime updates on collection data
2223
*/
23-
export declare function collectionStore<T>(ref: string | Query | CollectionReference, startWith?: T[]): CollectionStore<T[]>;
24-
/**
25-
* experimental, fetch document based on curret user
26-
*/
27-
export declare function userDataStore<T = DocumentData>(collectionPath?: string): Readable<T | null>;
24+
export declare function collectionStore<T>(firestore: Firestore, ref: string | Query | CollectionReference, startWith?: T[]): CollectionStore<T[]>;
2825
export {};

dist/stores/firestore.js

+7-22
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
1-
import { derived, writable } from "svelte/store";
2-
import { doc, collection, onSnapshot, query, where } from "firebase/firestore";
3-
import { onAuthStateChanged } from "firebase/auth";
4-
import { getFirebaseContext } from "./sdk.js";
5-
import { userStore } from "./auth.js";
1+
import { writable } from "svelte/store";
2+
import { doc, collection, onSnapshot } from "firebase/firestore";
63
/**
4+
* @param {Firestore} firestore firebase firestore instance
75
* @param {string|DocumentReference} ref document path or reference
86
* @param {T} startWith optional default data
97
* @returns a store with realtime updates on document data
108
*/
11-
export function docStore(ref, startWith) {
12-
const { firestore } = getFirebaseContext();
9+
export function docStore(firestore, ref, startWith) {
1310
let unsubscribe;
1411
// Fallback for SSR
1512
if (!globalThis.window) {
@@ -30,7 +27,7 @@ export function docStore(ref, startWith) {
3027
id: "",
3128
};
3229
}
33-
const docRef = typeof ref === "string" ? doc(firestore, ref) : ref;
30+
const docRef = typeof ref === 'string' ? doc(firestore, ref) : ref;
3431
const { subscribe } = writable(startWith, (set) => {
3532
unsubscribe = onSnapshot(docRef, (snapshot) => {
3633
set(snapshot.data() ?? null);
@@ -44,12 +41,12 @@ export function docStore(ref, startWith) {
4441
};
4542
}
4643
/**
44+
* @param {Firestore} firestore firebase firestore instance
4745
* @param {string|Query|CollectionReference} ref collection path, reference, or query
4846
* @param {[]} startWith optional default data
4947
* @returns a store with realtime updates on collection data
5048
*/
51-
export function collectionStore(ref, startWith = []) {
52-
const { firestore } = getFirebaseContext();
49+
export function collectionStore(firestore, ref, startWith = []) {
5350
let unsubscribe;
5451
// Fallback for SSR
5552
if (!globalThis.window) {
@@ -83,15 +80,3 @@ export function collectionStore(ref, startWith = []) {
8380
ref: colRef,
8481
};
8582
}
86-
/**
87-
* experimental, fetch document based on curret user
88-
*/
89-
export function userDataStore(collectionPath = "users") {
90-
const user = userStore();
91-
return derived(user, ($user, set) => {
92-
if (!$user)
93-
return set(null);
94-
const fullPath = `${collectionPath}/${$user.uid}`;
95-
return docStore(fullPath).subscribe(set);
96-
});
97-
}

dist/stores/stores.d.ts

-1
This file was deleted.

0 commit comments

Comments
 (0)