sveltedfire 0.0.1 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -44,7 +44,16 @@ fetchDocs('pages')('public', '==', true) // fetch all public pages
44
44
  fetchDocs('pages')(and(where('published_at', '>=', '2025-01-01'), where('published_at', '<=', '2025-01-31'))) // fetch all pages published in the month of January
45
45
  ```
46
46
 
47
- In the case of both `fetchDoc` and `fetchDocs`, the documents are returned as bare objects with the ID field injected into both the `id` and `_id` fields. If your data already defines an `id` field, it will be returned as-is; the `_id` field will always reference the actual ID of the object.
47
+ To listen for the snapshots of an object, use `listenDoc`. This takes the same parameters as `fetchDoc`, but returns a store. This should be used with rune syntax as below:
48
+ ```typescript
49
+ const value = listenDoc('pages', 'tester')
50
+
51
+ {#if $value}
52
+ {$value.name}
53
+ {/if}
54
+ ```
55
+
56
+ In the case of both `fetchDoc`, `fetchDocs`, and `listenDoc`, the documents are returned as bare objects with the ID field injected into both the `id` and `_id` fields. If your data already defines an `id` field, it will be returned as-is; the `_id` field will always reference the actual ID of the object.
48
57
 
49
58
 
50
59
  --------------
package/dist/index.d.ts CHANGED
@@ -3,6 +3,7 @@ export { fetchDoc } from './sveltedfire/utilities/fetchDoc.js';
3
3
  export { fetchDocs } from './sveltedfire/utilities/fetchDocs.js';
4
4
  export { signInWithGoogle } from './sveltedfire/auth/signInWithGoogle.js';
5
5
  export { signOut } from './sveltedfire/auth/signOut.js';
6
+ export { listenDoc } from './sveltedfire/utilities/listenDoc.js';
6
7
  import SignedIn from './sveltedfire/components/SignedIn.svelte';
7
8
  import SignedOut from './sveltedfire/components/SignedOut.svelte';
8
9
  export { SignedIn, SignedOut };
package/dist/index.js CHANGED
@@ -4,6 +4,7 @@ export { fetchDoc } from './sveltedfire/utilities/fetchDoc.js';
4
4
  export { fetchDocs } from './sveltedfire/utilities/fetchDocs.js';
5
5
  export { signInWithGoogle } from './sveltedfire/auth/signInWithGoogle.js';
6
6
  export { signOut } from './sveltedfire/auth/signOut.js';
7
+ export { listenDoc } from './sveltedfire/utilities/listenDoc.js';
7
8
  import SignedIn from './sveltedfire/components/SignedIn.svelte';
8
9
  import SignedOut from './sveltedfire/components/SignedOut.svelte';
9
10
  export { SignedIn, SignedOut };
@@ -3,10 +3,10 @@ export const fetchDoc = async (...docPath) => {
3
3
  const db = getFirestore();
4
4
  let theRef;
5
5
  if (docPath.length > 2) {
6
- theRef = doc(collection(db, docPath[0], ...docPath.slice(1, -1)), docPath[-1]);
6
+ theRef = doc(collection(db, docPath[0], ...docPath.slice(1, -1)), docPath.slice(-1)[0]);
7
7
  }
8
8
  else {
9
- theRef = doc(collection(db, docPath[0]), docPath[-1]);
9
+ theRef = doc(collection(db, docPath[0]), docPath.slice(-1)[0]);
10
10
  }
11
11
  const theDoc = await getDoc(theRef);
12
12
  if (!theDoc.exists()) {
@@ -0,0 +1,2 @@
1
+ import { type DocumentData } from "firebase/firestore";
2
+ export declare const listenDoc: (...docPath: Array<string>) => import("svelte/store").Readable<DocumentData | null>;
@@ -0,0 +1,25 @@
1
+ import { getFirestore, collection, doc, onSnapshot } from "firebase/firestore";
2
+ import { readable } from 'svelte/store';
3
+ export const listenDoc = (...docPath) => {
4
+ const db = getFirestore();
5
+ let theRef;
6
+ if (docPath.length > 2) {
7
+ theRef = doc(collection(db, docPath[0], ...docPath.slice(1, -1)), docPath.slice(-1)[0]);
8
+ }
9
+ else {
10
+ theRef = doc(collection(db, docPath[0]), docPath.slice(-1)[0]);
11
+ }
12
+ console.log(theRef);
13
+ const theDoc = readable(null, (set) => {
14
+ const unsubscribe = onSnapshot(theRef, (val) => {
15
+ if (!val.exists()) {
16
+ set(null);
17
+ }
18
+ else {
19
+ set({ id: val.id, ...val.data(), _id: val.id });
20
+ }
21
+ });
22
+ return () => unsubscribe();
23
+ });
24
+ return theDoc;
25
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "sveltedfire",
3
- "version": "0.0.1",
3
+ "version": "0.0.3",
4
4
  "scripts": {
5
5
  "dev": "vite dev",
6
6
  "build": "vite build && npm run prepack",
@@ -21,6 +21,10 @@
21
21
  "!dist/**/*.test.*",
22
22
  "!dist/**/*.spec.*"
23
23
  ],
24
+ "repository": {
25
+ "type": "git",
26
+ "url": "git+https://github.com/cadetstar/sveltedfire.git"
27
+ },
24
28
  "sideEffects": [
25
29
  "**/*.css"
26
30
  ],