-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfirebase.ts
90 lines (83 loc) · 2.31 KB
/
firebase.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
89
90
import { db } from "@/lib/firebaseConfig";
import { arrayRemove, arrayUnion, collection, deleteDoc, doc, getDoc, updateDoc, addDoc } from "firebase/firestore";
// 조건부 파라미터를 추가하고 addDoc을 바꾸고 둘다 해야한다.
export const postFirebase = async <T extends TableTypes>(table: T, id: string, body: TalbeField<T>) => {
try {
await addDoc(collection(db, table), { ...body, id });
} catch (e) {
console.error("Invalid Data");
}
};
export const getFirebase = async <T extends TableTypes>(table: T, id: string) => {
try {
const firebaseRef = doc(db, table, id);
const docSnap = await getDoc(firebaseRef);
if (docSnap.exists()) {
return docSnap.data() as TalbeField<T>;
} else {
console.error("No such document!");
}
} catch (e) {
console.error("Error getting document: ", e);
}
};
export const deleteFirebase = async <T extends TableTypes>(table: T, id: string) => {
try {
await deleteDoc(doc(db, table, id));
} catch (e) {
console.error("Error adding document: ", e);
}
};
export const updateFirebase = async <T extends TableTypes>(table: T, id: string, body: TalbeField<T>) => {
try {
const firebaseRef = doc(db, table, id);
await updateDoc(firebaseRef, body);
} catch (e) {
console.error("Error adding document: ", e);
}
};
export const pushFirebaseArray = async <T extends TableTypes>(
table: T,
id: string,
field: keyof TalbeField<T>,
value: string
) => {
try {
const firebaseRef = doc(db, table, id);
await updateDoc(firebaseRef, {
[field]: arrayUnion(value),
});
} catch (e) {
console.error("Error adding document: ", e);
}
};
export const popFirebaseArray = async <T extends TableTypes>(
table: T,
id: string,
field: keyof TalbeField<T>,
value: string
) => {
try {
const firebaseRef = doc(db, table, id);
await updateDoc(firebaseRef, {
[field]: arrayRemove(value),
});
} catch (e) {
console.error("Error adding document: ", e);
}
};
export const updateFirebaseField = async <T extends TableTypes>(
table: T,
id: string,
field: keyof TalbeField<T>,
value: string
) => {
try {
const firebaseRef = doc(db, table, id);
await updateDoc(firebaseRef, {
[field]: value,
});
} catch (e) {
console.error("Error adding document: ", e);
}
};