@edgedev/firebase 1.0.17 → 1.0.18
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/index.ts +34 -23
- package/package.json +1 -1
package/index.ts
CHANGED
|
@@ -11,6 +11,7 @@ import {
|
|
|
11
11
|
onSnapshot,
|
|
12
12
|
WhereFilterOp,
|
|
13
13
|
QueryConstraint,
|
|
14
|
+
Unsubscribe,
|
|
14
15
|
where
|
|
15
16
|
} from "firebase/firestore";
|
|
16
17
|
|
|
@@ -29,42 +30,39 @@ const app = initializeApp(firebaseConfig);
|
|
|
29
30
|
const db = getFirestore(app);
|
|
30
31
|
|
|
31
32
|
// Simple Store Items (add matching key per firebase collection)
|
|
32
|
-
export const data:
|
|
33
|
-
export const unsubscibe:
|
|
34
|
-
export const user:
|
|
33
|
+
export const data: CollectionDataObject = reactive({});
|
|
34
|
+
export const unsubscibe: CollectionUnsubscribeObject = reactive({});
|
|
35
|
+
export const user: UserDataObject = reactive({ uid: null, email: "" });
|
|
35
36
|
|
|
36
37
|
// Composable to start snapshot listener and set unsubscribe function
|
|
37
38
|
export const startSnapshot = (
|
|
38
39
|
collectionPath: string,
|
|
39
40
|
queryList: FirestoreQuery[] = []
|
|
40
41
|
): void => {
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
const queryConditions: QueryConstraint[] = queryList.map((condition) =>
|
|
44
|
-
where(condition.field, condition.operator, condition.value)
|
|
45
|
-
);
|
|
46
|
-
const q = query(collection(db, collectionPath), ...queryConditions);
|
|
47
|
-
const unsubscribe = onSnapshot(q, (querySnapshot) => {
|
|
48
|
-
const items = {};
|
|
49
|
-
querySnapshot.forEach((doc) => {
|
|
50
|
-
const item = doc.data();
|
|
51
|
-
item.docId = doc.id;
|
|
52
|
-
items[doc.id] = item;
|
|
53
|
-
});
|
|
54
|
-
data[collectionPath] = items;
|
|
55
|
-
});
|
|
56
|
-
unsubscibe[collectionPath] = unsubscribe;
|
|
42
|
+
if (data[collectionPath] instanceof Function) {
|
|
43
|
+
stopSnapshot(collectionPath);
|
|
57
44
|
}
|
|
45
|
+
const queryConditions: QueryConstraint[] = queryList.map((condition) =>
|
|
46
|
+
where(condition.field, condition.operator, condition.value)
|
|
47
|
+
);
|
|
48
|
+
const q = query(collection(db, collectionPath), ...queryConditions);
|
|
49
|
+
const unsubscribe = onSnapshot(q, (querySnapshot) => {
|
|
50
|
+
const items = {};
|
|
51
|
+
querySnapshot.forEach((doc) => {
|
|
52
|
+
const item = doc.data();
|
|
53
|
+
item.docId = doc.id;
|
|
54
|
+
items[doc.id] = item;
|
|
55
|
+
});
|
|
56
|
+
data[collectionPath] = items;
|
|
57
|
+
});
|
|
58
|
+
unsubscibe[collectionPath] = unsubscribe;
|
|
58
59
|
};
|
|
59
60
|
|
|
60
61
|
export const storeDoc = (collectionPath: string, item: object): void => {
|
|
61
62
|
const cloneItem = JSON.parse(JSON.stringify(item));
|
|
62
63
|
const current_time = new Date().getTime();
|
|
63
64
|
cloneItem.last_updated = current_time;
|
|
64
|
-
cloneItem.uid =
|
|
65
|
-
if (Object.prototype.hasOwnProperty.call(user, "uid")) {
|
|
66
|
-
cloneItem.uid = user["uid"];
|
|
67
|
-
}
|
|
65
|
+
cloneItem.uid = user.uid;
|
|
68
66
|
if (!Object.prototype.hasOwnProperty.call(cloneItem, "doc_created_at")) {
|
|
69
67
|
cloneItem.doc_created_at = current_time;
|
|
70
68
|
}
|
|
@@ -97,3 +95,16 @@ interface FirestoreQuery {
|
|
|
97
95
|
operator: WhereFilterOp; // '==' | '<' | '<=' | '>' | '>=' | 'array-contains' | 'in' | 'array-contains-any';
|
|
98
96
|
value: unknown;
|
|
99
97
|
}
|
|
98
|
+
|
|
99
|
+
interface CollectionUnsubscribeObject {
|
|
100
|
+
[key: string]: Unsubscribe;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
interface CollectionDataObject {
|
|
104
|
+
[key: string]: object;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
interface UserDataObject {
|
|
108
|
+
uid: string | null;
|
|
109
|
+
email: string;
|
|
110
|
+
}
|