-
Notifications
You must be signed in to change notification settings - Fork 0
/
firebase.js
84 lines (72 loc) · 2.43 KB
/
firebase.js
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
// Import the functions you need from the SDKs you need
import { initializeApp } from "firebase/app"
import {
getFirestore,
collection,
addDoc,
getDocs,
doc,
setDoc,
getDoc,
query,
where,
} from 'firebase/firestore/lite';
// TODO: Add SDKs for Firebase products that you want to use
// https://firebase.google.com/docs/web/setup#available-libraries
// Your web app's Firebase configuration
// For Firebase JS SDK v7.20.0 and later, measurementId is optional
const firebaseConfig = {
apiKey: "AIzaSyCbAYrjDDMKoPZdOD4ZgczI2-oPB9qxEwA",
authDomain: "nirasdragon.firebaseapp.com",
projectId: "nirasdragon",
storageBucket: "nirasdragon.appspot.com",
messagingSenderId: "1094588203168",
appId: "1:1094588203168:web:e0906edbd42dc0d32749c5",
measurementId: "G-JC2RYNE9W3"
}
// Initialize Firebase
export const app = initializeApp(firebaseConfig)
export const db = getFirestore(app)
// Get a list of cities from your database
export async function getUsers() {
const usersCol = collection(db, 'users')
const usersSnapshot = await getDocs(usersCol)
const userList = citySnapshot.docs.map(doc => doc.data());
return userList;
}
export async function setUser(user) {
return await setDoc(doc(db, "users", 'profiles'), user, { merge: true });
}
export async function setUserWisdomCount(userId, count) {
return await setDoc(doc(db, "users", `${userId}`), { count });
}
export async function getUserWisdomCount(userId) {
const docSnap = await getDoc(doc(db, "users", `${userId}`));
if (docSnap.exists()) {
return docSnap.data()
console.log("Got wisdom count for :", userId, docSnap.data());
} else {
// doc.data() will be undefined in this case
return { count: 0 }
console.log("No such wisdom count for", userId);
}
}
export async function setGrate(grate, userId) {
return await addDoc(collection(db, "grates"), grate);
}
export async function getGrate(userId) {
const q = query(collection(db, "grates"), where("userId", "==", userId));
const qSnap = await getDocs(q)
console.log('qSnap', qSnap);
let docs = [];
qSnap.forEach((doc) => {
// doc.data() is never undefined for query doc snapshots
console.log(doc.id, " => ", doc.data());
docs.push(doc.data());
});
return docs;
}
export async function getGratesCount(userId) {
const grates = await getGrate(userId)
return grates.length
}