-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.js
61 lines (52 loc) · 1.37 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
const admin = require("firebase-admin");
const serviceAccount = require('./key')
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: 'https://aluraflix-backend.firebaseio.com',
});
const db = admin.firestore()
const getVideos = async (limit=10) => {
var videos = [];
const snapshot = await db.collection('videos').limit(limit).get();
snapshot.forEach(doc => {
const obj = {
id: doc.id,
...doc.data()
}
videos.push(obj);
});
return videos;
}
const getVideo = async(id) => {
const doc = await db.collection('videos').doc(id).get();
return {exists: doc.exists, ...doc.data()};
}
const createVideo = async(titulo, url) => {
const obj = {
titulo,
url,
}
const newdoc = await db.collection('videos').add(obj);
const doc = await getVideo(newdoc.id);
return doc;
}
const deleteVideo = async(id) => {
const res = await db.collection('videos').doc(id).delete();
return res;
}
const hasVideo = async(id) => {
const res = await db.collection('videos').doc(id).get()
return res.exists;
}
const updateVideo = async(id, newdata) => {
const res = await db.collection('videos').doc(id).update(newdata);
return res;
}
module.exports = {
getVideos,
getVideo,
createVideo,
deleteVideo,
updateVideo,
hasVideo
}