Skip to content

Commit f0d5b91

Browse files
committed
feat: rename components to Node & NodeList for RTDB
1 parent 70125d8 commit f0d5b91

File tree

5 files changed

+40
-35
lines changed

5 files changed

+40
-35
lines changed

src/lib/components/DataRef.svelte renamed to src/lib/components/Node.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/rtdb.js";
2+
import { nodeStore } from "../stores/rtdb.js";
33
import { getFirebaseContext } from "../stores/sdk.js";
44
import type { DatabaseReference, Database } from "firebase/database";
55
6-
export let ref: string;
6+
export let path: string;
77
export let startWith: any = undefined;
88
99
const { rtdb } = getFirebaseContext();
10-
let store = refStore(rtdb!, ref, startWith);
10+
let store = nodeStore(rtdb!, path, startWith);
1111
1212
interface $$Slots {
1313
default: { data: any; ref: DatabaseReference | null; rtdb?: Database };

src/lib/components/DataList.svelte renamed to src/lib/components/NodeList.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/rtdb.js";
2+
import { nodeListStore } from "../stores/rtdb.js";
33
import { getFirebaseContext } from "../stores/sdk.js";
44
import type { DatabaseReference, Database } from "firebase/database";
55
6-
export let ref: string;
6+
export let path: string;
77
export let startWith: any[] = [];
88
99
const { rtdb } = getFirebaseContext();
10-
let store = listStore(rtdb!, ref, startWith);
10+
let store = nodeListStore(rtdb!, path, startWith);
1111
1212
interface $$Slots {
1313
default: {

src/lib/index.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,30 @@
11
// Reexport your entry components here
2+
import FirebaseApp from "./components/FirebaseApp.svelte";
23
import User from "./components/User.svelte";
34
import Collection from "./components/Collection.svelte";
45
import Doc from "./components/Doc.svelte";
5-
import DataRef from './components/DataRef.svelte';
6-
import DataList from './components/DataList.svelte';
7-
import FirebaseApp from "./components/FirebaseApp.svelte";
6+
import NodeList from "./components/NodeList.svelte";
7+
import Node from "./components/Node.svelte";
88
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/rtdb.js';
12+
import { nodeStore, nodeListStore } from './stores/rtdb.js';
1313
import { getFirebaseContext } from "./stores/sdk.js";
1414

1515
export {
1616
Doc,
1717
User,
1818
Collection,
19-
DataRef,
20-
DataList,
19+
Node,
20+
NodeList,
2121
FirebaseApp,
2222
SignedOut,
2323
SignedIn,
2424
docStore,
2525
collectionStore,
26-
refStore,
27-
listStore,
26+
nodeStore as refStore,
27+
nodeListStore as listStore,
2828
userStore,
2929
getFirebaseContext,
3030
};

src/lib/stores/rtdb.ts

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

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

1418
const { subscribe } = writable<T | null>(startWith, (set) => {
1519
const unsubscribe = onValue(dataRef, (snapshot) => {
@@ -27,24 +31,25 @@ export function refStore<T = any>(rtdb: Database, ref: string, startWith?: T) {
2731

2832
/**
2933
* @param {Database} rtdb - Firebase Realtime Database instance.
30-
* @param {string} ref - Database path or reference.
34+
* @param {string} path - Path to the list of nodes.
3135
* @param {T[]} startWith - Optional default data.
32-
* @returns a store with realtime updates on lists of data.
36+
* @returns a store with realtime updates on lists of nodes.
3337
*/
34-
export function listStore<T = any>(
38+
export function nodeListStore<T = any>(
3539
rtdb: Database,
36-
ref: string,
40+
path: string,
3741
startWith: T[] = []
3842
) {
39-
const listRef = dbRef(rtdb, ref);
43+
const listRef = dbRef(rtdb, path);
4044

4145
const { subscribe } = writable<T[]>(startWith, (set) => {
4246
const unsubscribe = onValue(listRef, (snapshot) => {
4347
const dataArr: T[] = [];
4448
snapshot.forEach((childSnapshot) => {
49+
const childData = childSnapshot.val();
4550
dataArr.push({
46-
ref: childSnapshot.ref,
47-
...childSnapshot.val(),
51+
nodeKey: childSnapshot.ref.key,
52+
...(typeof childData === "object" ? childData : {}),
4853
} as T);
4954
});
5055
set(dataArr);

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<script lang="ts">
2-
import DataRef from "$lib/components/DataRef.svelte";
3-
import DataList from "$lib/components/DataList.svelte";
4-
import SignedOut from "$lib/components/SignedOut.svelte";
2+
import Node from "$lib/components/Node.svelte";
3+
import NodeList from "$lib/components/NodeList.svelte";
54
import SignedIn from "$lib/components/SignedIn.svelte";
5+
import SignedOut from "$lib/components/SignedOut.svelte";
66
import { signInAnonymously } from "firebase/auth";
77
import { push, ref } from "firebase/database";
88
import { getFirebaseContext } from "$lib/stores/sdk.js";
@@ -22,12 +22,12 @@
2222

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

25-
<DataRef ref="posts/test" let:data={post}>
25+
<Node path="posts/test" let:data={post}>
2626
<p data-testid="ref-data">{post?.title}</p>
2727
<div slot="loading">
2828
<p data-testid="loading">Loading...</p>
2929
</div>
30-
</DataRef>
30+
</Node>
3131

3232
<h2>User Owned Data</h2>
3333

@@ -38,20 +38,20 @@
3838

3939
<SignedIn let:user>
4040
<h2>Data List</h2>
41-
<DataList
42-
ref={`users/${user.uid}/posts`}
41+
<NodeList
42+
path={`users/${user.uid}/posts`}
4343
startWith={[]}
4444
let:data={posts}
4545
let:count
4646
>
4747
<p data-testid="count">You've made {count} posts</p>
4848

4949
<ul>
50-
{#each posts as post (post.ref.key)}
51-
<li>{post?.content} ... {post.ref.key}</li>
50+
{#each posts as post (post.nodeKey)}
51+
<li>{post?.content} ... {post.nodeKey}</li>
5252
{/each}
5353
</ul>
5454

5555
<button on:click={() => addPost(user.uid)}>Add Data</button>
56-
</DataList>
56+
</NodeList>
5757
</SignedIn>

0 commit comments

Comments
 (0)