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

Lines changed: 25 additions & 30 deletions
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

Lines changed: 4 additions & 2 deletions
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

Lines changed: 2 additions & 1 deletion
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

Lines changed: 4 additions & 2 deletions
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

Lines changed: 2 additions & 1 deletion
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

Lines changed: 0 additions & 1 deletion
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 2 deletions
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

Lines changed: 3 additions & 1 deletion
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}

0 commit comments

Comments
 (0)