@edgedev/firebase 1.0.8 → 1.0.10

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.
Files changed (2) hide show
  1. package/index.ts +45 -13
  2. package/package.json +1 -1
package/index.ts CHANGED
@@ -1,9 +1,12 @@
1
1
  import { initializeApp } from "firebase/app";
2
- import { reactive } from "vue";
2
+ import { reactive, ref } from "vue";
3
3
 
4
4
  import {
5
5
  getFirestore,
6
6
  collection,
7
+ addDoc,
8
+ updateDoc,
9
+ doc,
7
10
  query,
8
11
  onSnapshot
9
12
  } from "firebase/firestore";
@@ -23,29 +26,58 @@ const app = initializeApp(firebaseConfig);
23
26
  const db = getFirestore(app);
24
27
 
25
28
  // Simple Store Items (add matching key per firebase collection)
26
- export const fireStoreData = reactive({});
27
- export const fireStoreUnsubscibe = reactive({});
29
+ export const data = reactive({});
30
+ export const unsubscibe = reactive({});
31
+ export const user = ref(null);
28
32
 
29
33
  // Composable to start snapshot listener and set unsubscribe function
30
- export const initSnapshot = (collectionName) => {
34
+ export const startSnapshot = (collectionPath: string): void => {
31
35
  // This first "if" is to prevent multiple listeners on the same collection
32
- if (!(fireStoreUnsubscibe[collectionName] instanceof Function)) {
33
- const q = query(collection(db, collectionName));
36
+ if (!(data[collectionPath] instanceof Function)) {
37
+ const q = query(collection(db, collectionPath));
34
38
  const unsubscribe = onSnapshot(q, (querySnapshot) => {
35
39
  const items = {};
36
40
  querySnapshot.forEach((doc) => {
37
- items[doc.id] = doc.data();
41
+ const item = doc.data();
42
+ item.docId = doc.id;
43
+ items[doc.id] = item;
38
44
  });
39
- fireStoreData[collectionName] = items;
45
+ data[collectionPath] = items;
40
46
  });
41
- fireStoreUnsubscibe[collectionName] = unsubscribe;
47
+ unsubscibe[collectionPath] = unsubscribe;
48
+ }
49
+ };
50
+
51
+ export const storeDoc = (collectionPath: string, item: object): void => {
52
+ const cloneItem = JSON.parse(JSON.stringify(item));
53
+ const current_time = new Date().getTime();
54
+ cloneItem.last_updated = current_time;
55
+ if (user !== null) {
56
+ cloneItem.uid = user.uid;
57
+ }
58
+ if (!Object.prototype.hasOwnProperty.call(cloneItem, "doc_created_at")) {
59
+ cloneItem.doc_created_at = current_time;
60
+ }
61
+ if (Object.prototype.hasOwnProperty.call(cloneItem, "docId")) {
62
+ const docId = cloneItem.docId;
63
+ if (Object.prototype.hasOwnProperty.call(data, collectionPath)) {
64
+ data[collectionPath][docId] = cloneItem;
65
+ }
66
+ delete cloneItem.docId;
67
+ const docRef = doc(db, collectionPath, docId);
68
+ updateDoc(docRef, cloneItem);
69
+ } else {
70
+ if (Object.prototype.hasOwnProperty.call(data, collectionPath)) {
71
+ data[collectionPath][current_time] = cloneItem;
72
+ }
73
+ addDoc(collection(db, collectionPath), cloneItem);
42
74
  }
43
75
  };
44
76
 
45
77
  // Composable to stop snapshot listener
46
- export const stopSnapshot = (collectionName) => {
47
- if (fireStoreUnsubscibe[collectionName] instanceof Function) {
48
- fireStoreUnsubscibe[collectionName]();
49
- fireStoreUnsubscibe[collectionName] = null;
78
+ export const stopSnapshot = (collectionPath: string): void => {
79
+ if (unsubscibe[collectionPath] instanceof Function) {
80
+ unsubscibe[collectionPath]();
81
+ unsubscibe[collectionPath] = null;
50
82
  }
51
83
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@edgedev/firebase",
3
- "version": "1.0.8",
3
+ "version": "1.0.10",
4
4
  "description": "Composables and stores for firebase",
5
5
  "main": "index.ts",
6
6
  "author": "Seth Fischer",