-
Notifications
You must be signed in to change notification settings - Fork 7
/
db.js
34 lines (28 loc) · 866 Bytes
/
db.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
import admin from "firebase-admin";
import serviceAccount from "./serviceAccountKey.json";
if (!admin.apps.length) {
try {
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
});
} catch (error) {
console.log("Firebase admin initialization error", error.stack);
}
}
const db = admin.firestore();
export default db;
export const getIds = async () => {
const querySnapshot = await db.collection("boards").select().get();
return querySnapshot.docs.map((doc) => doc.id);
};
export const getBoard = async (id) => {
const snapshot = await db.collection("boards").doc(id).get();
return snapshot.data();
};
export const getLikes = async () => {
const querySnapshot = await db.collection("likes").get();
return querySnapshot.docs.reduce((acc, doc) => {
acc[doc.id] = doc.data();
return acc;
}, {});
};