Skip to content

Commit 70125d8

Browse files
committed
feat: rtdb-test uses posts as example
1 parent a95b627 commit 70125d8

File tree

7 files changed

+42
-29
lines changed

7 files changed

+42
-29
lines changed

src/lib/components/DataList.svelte

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
<script lang="ts">
2-
import { listStore } from "../stores/realtimeDatabase.js";
2+
import { listStore } from "../stores/rtdb.js";
33
import { getFirebaseContext } from "../stores/sdk.js";
44
import type { DatabaseReference, Database } from "firebase/database";
55
6-
export let path: string;
6+
export let ref: string;
77
export let startWith: any[] = [];
88
99
const { rtdb } = getFirebaseContext();
10-
let store = listStore(rtdb!, path, startWith);
10+
let store = listStore(rtdb!, ref, startWith);
1111
1212
interface $$Slots {
1313
default: {

src/lib/components/DataRef.svelte

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
<script lang="ts">
2-
import { refStore } from "../stores/realtimeDatabase.js";
2+
import { refStore } from "../stores/rtdb.js";
33
import { getFirebaseContext } from "../stores/sdk.js";
44
import type { DatabaseReference, Database } from "firebase/database";
55
6-
export let path: string;
6+
export let ref: string;
77
export let startWith: any = undefined;
88
99
const { rtdb } = getFirebaseContext();
10-
let store = refStore(rtdb!, path, startWith);
10+
let store = refStore(rtdb!, ref, startWith);
1111
1212
interface $$Slots {
1313
default: { data: any; ref: DatabaseReference | null; rtdb?: Database };

src/lib/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import SignedIn from "./components/SignedIn.svelte";
99
import SignedOut from "./components/SignedOut.svelte";
1010
import { userStore } from "./stores/auth.js";
1111
import { docStore, collectionStore } from "./stores/firestore.js";
12-
import { refStore, listStore } from './stores/realtimeDatabase.js';
12+
import { refStore, listStore } from './stores/rtdb.js';
1313
import { getFirebaseContext } from "./stores/sdk.js";
1414

1515
export {

src/lib/stores/realtimeDatabase.ts renamed to src/lib/stores/rtdb.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ import type { Database } from "firebase/database";
44

55
/**
66
* @param {Database} rtdb - Firebase Realtime Database instance.
7-
* @param {string} path - Path to the database reference.
7+
* @param {string} ref - Database path or reference.
88
* @param {T | undefined} startWith - Optional default data.
99
* @returns a store with realtime updates on individual database reference data.
1010
*/
11-
export function refStore<T = any>(rtdb: Database, path: string, startWith?: T) {
12-
const dataRef = dbRef(rtdb, path);
11+
export function refStore<T = any>(rtdb: Database, ref: string, startWith?: T) {
12+
const dataRef = dbRef(rtdb, ref);
1313

1414
const { subscribe } = writable<T | null>(startWith, (set) => {
1515
const unsubscribe = onValue(dataRef, (snapshot) => {
@@ -27,25 +27,25 @@ export function refStore<T = any>(rtdb: Database, path: string, startWith?: T) {
2727

2828
/**
2929
* @param {Database} rtdb - Firebase Realtime Database instance.
30-
* @param {string} path - Path to the database reference.
30+
* @param {string} ref - Database path or reference.
3131
* @param {T[]} startWith - Optional default data.
3232
* @returns a store with realtime updates on lists of data.
3333
*/
3434
export function listStore<T = any>(
3535
rtdb: Database,
36-
path: string,
36+
ref: string,
3737
startWith: T[] = []
3838
) {
39-
const listRef = dbRef(rtdb, path);
39+
const listRef = dbRef(rtdb, ref);
4040

4141
const { subscribe } = writable<T[]>(startWith, (set) => {
4242
const unsubscribe = onValue(listRef, (snapshot) => {
4343
const dataArr: T[] = [];
4444
snapshot.forEach((childSnapshot) => {
4545
dataArr.push({
46-
key: childSnapshot.ref.key,
47-
...(childSnapshot.val() as T),
48-
});
46+
ref: childSnapshot.ref,
47+
...childSnapshot.val(),
48+
} as T);
4949
});
5050
set(dataArr);
5151
});

src/routes/+layout.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33
import { db as firestore, auth, rtdb } from "./firebase.js";
44
</script>
55

6-
<FirebaseApp {auth} {firestore} {rtdb}>
6+
<FirebaseApp {auth} {firestore} {rtdb} >
77
<slot />
88
</FirebaseApp>

src/routes/firebase.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,12 @@ import {
55
getFirestore,
66
setDoc,
77
} from "firebase/firestore";
8-
import { connectDatabaseEmulator, getDatabase, ref, set } from "firebase/database";
8+
import {
9+
connectDatabaseEmulator,
10+
getDatabase,
11+
ref,
12+
set,
13+
} from "firebase/database";
914
import { connectAuthEmulator, getAuth } from "firebase/auth";
1015
import { dev } from "$app/environment";
1116

@@ -37,5 +42,8 @@ if (dev) {
3742
});
3843

3944
// Seed Realtime Database
40-
set(ref(rtdb, "test"), { key: "value" });
45+
set(ref(rtdb, "posts/test"), {
46+
title: "Hi Mom",
47+
content: "this is a test",
48+
});
4149
}

src/routes/rtdb-test/+page.svelte

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
1010
const rtdb = getFirebaseContext().rtdb!;
1111
12-
async function addData(uid: string) {
13-
const dataRef = ref(rtdb, `users/${uid}/data`);
14-
await push(dataRef, {
12+
async function addPost(uid: string) {
13+
const postsRef = ref(rtdb, `users/${uid}/posts`);
14+
await push(postsRef, {
1515
content: "RTDB item " + (Math.random() + 1).toString(36).substring(7),
1616
created: Date.now(),
1717
});
@@ -22,8 +22,8 @@
2222

2323
<h2>Single Data Reference</h2>
2424

25-
<DataRef path="test" let:data>
26-
<p data-testid="ref-data">{data.key}</p>
25+
<DataRef ref="posts/test" let:data={post}>
26+
<p data-testid="ref-data">{post?.title}</p>
2727
<div slot="loading">
2828
<p data-testid="loading">Loading...</p>
2929
</div>
@@ -38,15 +38,20 @@
3838

3939
<SignedIn let:user>
4040
<h2>Data List</h2>
41-
<DataList path={`users/${user.uid}/data`} startWith={[]} let:data let:count>
42-
<p data-testid="count">You've {count} items</p>
41+
<DataList
42+
ref={`users/${user.uid}/posts`}
43+
startWith={[]}
44+
let:data={posts}
45+
let:count
46+
>
47+
<p data-testid="count">You've made {count} posts</p>
4348

4449
<ul>
45-
{#each data as item (item.key)}
46-
<li>{item.content} ... {item.key}</li>
50+
{#each posts as post (post.ref.key)}
51+
<li>{post?.content} ... {post.ref.key}</li>
4752
{/each}
4853
</ul>
4954

50-
<button on:click={() => addData(user.uid)}>Add Data</button>
55+
<button on:click={() => addPost(user.uid)}>Add Data</button>
5156
</DataList>
5257
</SignedIn>

0 commit comments

Comments
 (0)