@@ -8,32 +8,32 @@ A minimal, yet powerful library that puts realtime Firebase data into Svelte sto
8
8
<!-- 1. 🔥 Firebase App -->
9
9
<FirebaseApp {auth} {firestore} {rtdb}>
10
10
11
- <!-- 2. 👤 Get the current user -->
12
- <SignedIn let:user>
11
+ <!-- 2. 👤 Get the current user -->
12
+ <SignedIn let:user>
13
13
14
- <p>Howdy, {user.uid}</p>
14
+ <p>Howdy, {user.uid}</p>
15
15
16
- <!-- 3 (a). 📜 Get a Firestore document owned by a user -->
17
- <Doc ref={`posts/${user.uid}`} let:data={post} let:ref={postRef}>
16
+ <!-- 3 (a). 📜 Get a Firestore document owned by a user -->
17
+ <Doc ref={`posts/${user.uid}`} let:data={post} let:ref={postRef}>
18
18
19
- <h2>{post.title}</h2>
19
+ <h2>{post.title}</h2>
20
20
21
- <!-- 4 (a). 💬 Get all the comments in its subcollection -->
22
- <Collection ref={postRef.path + '/comments'} let:data={comments}>
23
- {#each comments as comment}
21
+ <!-- 4 (a). 💬 Get all the comments in its subcollection -->
22
+ <Collection ref={postRef.path + '/comments'} let:data={comments}>
23
+ {#each comments as comment}
24
24
25
- {/each}
25
+ {/each}
26
26
27
- <!-- 3 (b). 📜 Get a Realtime Database node owned by a user -->
28
- <Node path={`posts/${user.uid}`} let:data={post} let:ref={postRef}>
27
+ <!-- 3 (b). 📜 Get a Realtime Database node owned by a user -->
28
+ <Node path={`posts/${user.uid}`} let:data={post} let:ref={postRef}>
29
29
30
- <h2>{post.title}</h2>
30
+ <h2>{post.title}</h2>
31
31
32
- <!-- 4 (b). 💬 Get all the comments in its subnodes -->
33
- <NodeList path={postRef.path + '/comments'} let:data={comments}>
34
- {#each comments as comment}
32
+ <!-- 4 (b). 💬 Get all the comments in its subnodes -->
33
+ <NodeList path={postRef.path + '/comments'} let:data={comments}>
34
+ {#each comments as comment}
35
35
36
- {/each}
36
+ {/each}
37
37
...
38
38
```
39
39
@@ -114,9 +114,9 @@ Listen to the current user. Render UI conditionally based on the auth state:
114
114
</script>
115
115
116
116
{#if $user}
117
- <p>Hi {$user.uid}</p>
117
+ <p>Hi {$user.uid}</p>
118
118
{:else}
119
- <p>Sign in...</p>
119
+ <p>Sign in...</p>
120
120
{/if}
121
121
```
122
122
@@ -198,11 +198,11 @@ First, fetch data from a load function like so:
198
198
import { doc , getDoc } from ' firebase/firestore' ;
199
199
200
200
export const load = (async () => {
201
- const ref = doc (firestore , ' posts' , ' first-post' );
202
- const snapshot = await getDoc (ref );
203
- return {
204
- post: snapshot .data ();
205
- };
201
+ const ref = doc (firestore , ' posts' , ' first-post' );
202
+ const snapshot = await getDoc (ref );
203
+ return {
204
+ post: snapshot .data ();
205
+ };
206
206
});
207
207
```
208
208
@@ -235,8 +235,8 @@ The `FirebaseApp` component puts the FirebaseSDK into Svelte context. This avoid
235
235
236
236
<FirebaseApp {auth} {firestore} {rtdb} >
237
237
238
- <User let:user></User>
239
- <!-- other sveltefire components here -->
238
+ <User let:user></User>
239
+ <!-- other sveltefire components here -->
240
240
241
241
</FirebaseApp>
242
242
```
@@ -256,11 +256,11 @@ Get the current user.
256
256
257
257
``` svelte
258
258
<SignedIn let:user>
259
- Hello {user.uid}
259
+ Hello {user.uid}
260
260
</SignedIn>
261
261
262
262
<SignedOut>
263
- You need to sign in!
263
+ You need to sign in!
264
264
</SignedOut>
265
265
```
266
266
@@ -270,26 +270,26 @@ Fetch a single document and listen to data in realtime. The `data` slot prop pro
270
270
271
271
``` svelte
272
272
<Doc ref="posts/test" let:data let:ref>
273
- {data.content}
274
- {ref.path}
273
+ {data.content}
274
+ {ref.path}
275
275
</Doc>
276
276
```
277
277
278
278
Slot props can be renamed:
279
279
280
280
``` svelte
281
281
<Doc ref="posts/test" let:data={post} let:ref={postRef}>
282
- {post.content}
283
- {postRef.path}
282
+ {post.content}
283
+ {postRef.path}
284
284
</Doc>
285
285
```
286
286
287
287
Firestore components can also handle loading states:
288
288
289
289
``` svelte
290
290
<Doc path="posts/test">
291
- <!-- data renders here in the default slot -->
292
- <div slot="loading">Loading.... This will disappear when data is defined</div>
291
+ <!-- data renders here in the default slot -->
292
+ <div slot="loading">Loading.... This will disappear when data is defined</div>
293
293
</Doc>
294
294
```
295
295
@@ -318,40 +318,39 @@ Collections can also take a Firestore Query instead of a path:
318
318
319
319
``` svelte
320
320
<script>
321
- const myQuery = query(collection(firestore, 'posts'), where('test', '==', 'test'));
321
+ const myQuery = query(collection(firestore, 'posts'), where('test', '==', 'test'));
322
322
</script>
323
323
324
324
<Collection ref={myQuery} let:data>
325
325
</Collection>
326
326
```
327
327
328
- <<<<<<< HEAD
329
328
### Node
330
329
331
330
Fetch a single node from the Realtime Database and listen to its data in realtime. The ` data ` slot prop gives you access to the fetched data, and the ` ref ` provides the Realtime Database reference.
332
331
333
332
``` svelte
334
333
<Node path="posts/test" let:data let:ref>
335
- {data.content}
336
- {ref.key}
334
+ {data.content}
335
+ {ref.key}
337
336
</Node>
338
337
```
339
338
340
339
Slot props can be renamed:
341
340
342
341
``` svelte
343
342
<Node path="posts/test" let:data={post} let:ref={postRef}>
344
- {post.content}
345
- {postRef.key}
343
+ {post.content}
344
+ {postRef.key}
346
345
</Node>
347
346
```
348
347
349
348
Realtime Database components can also handle loading states:
350
349
351
350
``` svelte
352
351
<Node path="posts/test">
353
- <!-- data renders here in the default slot -->
354
- <div slot="loading">Loading.... This will disappear when data is defined</div>
352
+ <!-- data renders here in the default slot -->
353
+ <div slot="loading">Loading.... This will disappear when data is defined</div>
355
354
</Node>
356
355
```
357
356
@@ -374,15 +373,15 @@ Fetch lists of nodes from the Realtime Database and listen to their data in real
374
373
{/each}
375
374
</NodeList>
376
375
```
377
- =======
378
- ### DownloadLink
379
376
380
- DownloadLink provides a ` link ` to download a file from Firebase Storage and its ` reference ` .
377
+ ### DownloadURL
378
+
379
+ DownloadURL provides a ` link ` to download a file from Firebase Storage and its ` reference ` .
381
380
382
381
``` svelte
383
- <DownloadLink ref={item} let:link let:ref>
384
- <a href=" {link}" download>Download {ref?.name}</a>
385
- </DownloadLink >
382
+ <DownloadURL ref={item} let:link let:ref>
383
+ <a href={link} download>Download {ref?.name}</a>
384
+ </DownloadURL >
386
385
```
387
386
388
387
### StorageList
@@ -391,31 +390,46 @@ StorageList provides a list of `items` and `prefixes` corresponding to the list
391
390
392
391
``` svelte
393
392
<StorageList ref="/" let:list>
394
- <ul>
395
- {#if list === null}
396
- <li>Loading...</li>
397
- {:else if list.prefixes.length === 0 && list.items.length === 0}
398
- <li>Empty</li>
399
- {:else}
400
- <!-- Listing the prefixes -->
401
- {#each list.prefixes as prefix}
402
- <li>
403
- {prefix.name}
404
- </li>
405
- {/each}
406
- <!-- Listing the objects in the given folder -->
407
- {#each list.items as item}
408
- <li>
409
- {item.name}
410
- </li>
411
- {/each}
412
- {/if}
413
- </ul>
393
+ <ul>
394
+ {#if list === null}
395
+ <li>Loading...</li>
396
+ {:else if list.prefixes.length === 0 && list.items.length === 0}
397
+ <li>Empty</li>
398
+ {:else}
399
+ <!-- Listing the prefixes -->
400
+ {#each list.prefixes as prefix}
401
+ <li>
402
+ {prefix.name}
403
+ </li>
404
+ {/each}
405
+ <!-- Listing the objects in the given folder -->
406
+ {#each list.items as item}
407
+ <li>
408
+ {item.name}
409
+ </li>
410
+ {/each}
411
+ {/if}
412
+ </ul>
414
413
</StorageList>
415
414
```
416
415
417
- You can combine
418
- >>>>>>> upstream/master
416
+ ### UploadTask
417
+
418
+ Upload a file with progress tracking
419
+
420
+ ``` svelte
421
+ <UploadTask ref="filename.txt" data={someBlob} let:progress let:snapshot>
422
+ {#if snapshot?.state === "running"}
423
+ {progress}% uploaded
424
+ {/if}
425
+
426
+ {#if snapshot?.state === "success"}
427
+ <DownloadURL ref={snapshot?.ref} let:link>
428
+ <a href={link} download>Download</a>
429
+ </DownloadURL>
430
+ {/if}
431
+ </UploadTask>
432
+ ```
419
433
420
434
### Using Components Together
421
435
@@ -424,54 +438,54 @@ These components can be combined to build complex realtime apps. It's especially
424
438
``` svelte
425
439
<FirebaseApp {auth} {firestore}>
426
440
<SignedIn let:user>
427
- <p>UID: {user.uid}</p>
441
+ <p>UID: {user.uid}</p>
428
442
429
- <h3>Profile</h3>
443
+ <h3>Profile</h3>
430
444
431
- <!-- 📜 Fetch data using Firestore -->
432
- <Doc ref={`posts/${user.uid}`} let:data={profile} let:ref={profileRef}>
445
+ <!-- 📜 Fetch data using Firestore -->
446
+ <Doc ref={`posts/${user.uid}`} let:data={profile} let:ref={profileRef}>
433
447
434
- {profile.content}
448
+ {profile.content}
435
449
436
- <h4>Comments</h4>
437
- <Collection ref={profileRef.path + '/comments'} let:data={comments}>
438
- {#each comments as comment}
439
- <strong>{comment.content}</strong>
440
- {/each}
450
+ <h4>Comments</h4>
451
+ <Collection ref={profileRef.path + '/comments'} let:data={comments}>
452
+ {#each comments as comment}
453
+ <strong>{comment.content}</strong>
454
+ {/each}
441
455
442
- <div slot="loading">Loading Comments...</div>
443
- </Collection>
456
+ <div slot="loading">Loading Comments...</div>
457
+ </Collection>
444
458
445
- <div slot="loading">Loading Profile...</div>
446
- </Doc>
459
+ <div slot="loading">Loading Profile...</div>
460
+ </Doc>
447
461
448
- <!-- 📜 Fetch data using Realtime Database -->
449
- <Node path={`posts/${user.uid}`} let:data={profile} let:ref={profileRef}>
462
+ <!-- 📜 Fetch data using Realtime Database -->
463
+ <Node path={`posts/${user.uid}`} let:data={profile} let:ref={profileRef}>
450
464
451
- {profile.content}
465
+ {profile.content}
452
466
453
- <h4>Comments</h4>
454
- <NodeList path={profileRef.path + '/comments'} let:data={comments}>
455
- {#each comments as comment}
456
- <strong>{comment.content}</strong>
457
- {/each}
467
+ <h4>Comments</h4>
468
+ <NodeList path={profileRef.path + '/comments'} let:data={comments}>
469
+ {#each comments as comment}
470
+ <strong>{comment.content}</strong>
471
+ {/each}
458
472
459
- <div slot="loading">Loading Comments...</div>
460
- </NodeList>
473
+ <div slot="loading">Loading Comments...</div>
474
+ </NodeList>
461
475
462
- <div slot="loading">Loading Profile...</div>
463
- </Node>
476
+ <div slot="loading">Loading Profile...</div>
477
+ </Node>
464
478
</SignedIn>
465
479
466
480
<SignedOut>
467
- <p>Sign in to see your profile</p>
481
+ <p>Sign in to see your profile</p>
468
482
</SignedOut>
469
483
</FirebaseApp>
470
484
```
471
485
472
486
## Roadmap
473
487
474
- - Add support for Firebase Storage
488
+ - ~~ Add support for Firebase Storage~~ (Added in latest release!)
475
489
- ~~ Add support for Firebase RTDB~~ (Added in latest release!)
476
490
- Add support for Firebase Analytics in SvelteKit
477
491
- Find a way to make TS generics with with Doc/Collection components
0 commit comments