Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
scheng123321
GitHub Repository: scheng123321/1v1-lol
Path: blob/master/fireStore.js
446 views
1
var db;
2
3
function initializeFirestore()
4
{
5
db = firebase.firestore();
6
}
7
8
//TODO: add subcollections support
9
10
function addDocument(collectionName, data, isJson){
11
if(!db || !collectionName)
12
return;
13
14
if(isJson){
15
try {
16
data = JSON.parse(data);
17
} catch(error){
18
console.log("Couldnt insert data to db, invalid json");
19
return;
20
}
21
}
22
23
return db.collection(collectionName).add(data);
24
}
25
26
function setDocument(collectionName, documentName, data, isJson, isMerge){
27
if(!db || !collectionName || !documentName)
28
return;
29
30
if(isJson){
31
try {
32
data = JSON.parse(data);
33
} catch(error){
34
console.log("Couldnt insert data to db, invalid json");
35
return;
36
}
37
}
38
39
return db.collection(collectionName).doc(documentName).set(data, { merge: isMerge });
40
}
41
42
function updateDocument(collectionName, documentName, data, isJson){
43
// TODO: add support for arrays
44
if(!db || !collectionName || !documentName)
45
return;
46
47
if(isJson){
48
try {
49
data = JSON.parse(data);
50
} catch(error){
51
console.log("Couldnt insert data to db, invalid json");
52
return;
53
}
54
}
55
56
return db.collection(collectionName).doc(documentName).update(data);
57
}
58
59
function deleteDocument(collectionName, documentName){
60
if(!db || !collectionName || !documentName)
61
return;
62
63
return db.collection(collectionName).doc(documentName).delete();
64
}
65
66
function getDocument(collectionName, documentName){
67
if(!db || !collectionName || !documentName)
68
return;
69
70
return db.collection(collectionName).doc(documentName).get();
71
}
72
73
74