/
firebase-service.ts
88 lines (78 loc) · 1.95 KB
/
firebase-service.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
//
// firebaseservice - a composable for managing the interaction
// with firebase
//
// REFERENCE
// @see https://v3.vuejs.org/guide/composition-api-introduction.html#why-composition-api
//
import { onMounted, ref } from "vue";
// FIREBASE
import firebase from "firebase/app";
import "firebase/firestore";
const db = firebase
.initializeApp({ projectId: process.env.VUE_APP_FIREBASE_PROJECT_ID })
.firestore();
const userCollection = db.collection("users");
const users = ref<any>([]);
const error = ref<any>(null);
const loading = ref<any>(false);
export default () => {
/**
*
* @param collection returns data for specified collections
*/
const getCollectionData = async (collection: any) => {
loading.value = true;
try {
const querySnapshot = await collection.get();
const results = querySnapshot.docs.map((doc: any) => {
return { ...doc.data(), id: doc.id };
});
loading.value = false;
return results;
} catch (e) {
error.value = e;
loading.value = false;
}
};
/**
* get document from firebase and not local users
*
* @param collection
* @param id
*/
const getCollectionDoc = async (collection: any, id: string) => {
loading.value = true;
try {
const doc = await collection.doc(id).get();
loading.value = false;
return { ...doc.data(), id: doc.id };
} catch (e) {
error.value = e;
loading.value = false;
}
};
/**
* get the user from the local array
* @param userId
*/
const getLocalUser = (userId: string) => {
return users.value.find((u: any) => u.id === userId);
};
const loadUsers = async () => {
users.value = await getCollectionData(userCollection);
};
onMounted(async () => {
await loadUsers();
});
return {
// functions
loadUsers,
getLocalUser,
getUser : (id: string) => getCollectionDoc(userCollection,id),
//properties
users,
loading,
error,
};
};