-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.js
79 lines (67 loc) · 1.76 KB
/
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
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
const admin = require('firebase-admin');
var db;
module.exports.addItem = function(table, id, object) {
if (!db) {
db = connectToDb();
}
db.collection(table).doc(id).set(object);
}
// addItem('config', 'test', {value: 'test'});
module.exports.fetchItem = async function(table, id) {
if (!db) {
db = connectToDb();
}
var data;
await db.collection(table).get()
.then((snapshot) => {
snapshot.forEach((doc) => {
if (doc.id === id) {
data = doc.data();
}
});
})
.catch((err) => {
console.log('Error getting documents', err);
})
return data;
}
// fetchItem('config', 'test').then(thing => {
// console.log(thing);
// });
module.exports.deleteItem = function(table, id) {
if (!db) {
db = connectToDb();
}
db.collection(table).doc(id).delete();
}
// deleteItem('config', 'test');
module.exports.updateItem = function(table, id, newValue) {
if (!db) {
db = connectToDb();
}
let docRef = db.collection(table).doc(id);
let updateSingle = docRef.update(newValue);
}
module.exports.fetchAllItems = async function(table) {
if (!db) {
db = connectToDb();
}
var data = [];
await db.collection(table).get()
.then((snapshot) => {
snapshot.forEach((doc) => {
data.push(doc);
});
})
.catch((err) => {
console.log('Error getting documents', err);
})
return data;
}
function connectToDb() {
admin.initializeApp({
credential: admin.credential.cert(JSON.parse(process.env.DB_KEY))
});
return admin.firestore();
}
//updateItem('config', 'test', {value: 'test2'});