-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdbService.ts
More file actions
39 lines (34 loc) · 1 KB
/
dbService.ts
File metadata and controls
39 lines (34 loc) · 1 KB
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
import admin from "firebase-admin";
import type { Group, User } from "./types";
const confFile =
process.env.NODE_ENV === "production"
? "/etc/conf/firebase.json"
: "firebase.json";
export const makeDbService = async () => {
const key = await Bun.file(confFile).json();
const firebaseConfig = {
credential: admin.credential.cert(key),
};
const app = admin.initializeApp(firebaseConfig);
const db = app.firestore();
const userCollection = db.collection("users");
const groupCollection = db.collection("groups");
return {
async addUser(data: User) {
const userRef = userCollection.doc(data.id.toString());
try {
userRef.set(data, { merge: true });
} catch (error) {
console.log("DB error", error);
}
},
async addGroup(data: Group) {
const groupRef = groupCollection.doc(data.id.toString());
try {
groupRef.set(data, { merge: true });
} catch (error) {
console.log("DB error", error);
}
},
};
};