-
Notifications
You must be signed in to change notification settings - Fork 1
/
firebase.js
75 lines (65 loc) · 1.79 KB
/
firebase.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
const { initializeApp, cert } = require('firebase-admin/app')
const { getFirestore } = require('firebase-admin/firestore')
const dotenv = require('dotenv')
dotenv.config()
const serviceAccount = JSON.parse(process.env.FIREBASE_CREDENTIALS)
initializeApp({
credential: cert(serviceAccount),
})
const db = getFirestore()
const playlists = db.collection('playlists')
const generateRandomString = () => {
var text = ''
var possible =
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
for (var i = 0; i < 16; i++) {
text += possible.charAt(Math.floor(Math.random() * possible.length))
}
return text
}
const addNewPlaylist = async (playlistData) => {
const res = await db
.collection('playlists')
.doc(generateRandomString())
.set(playlistData)
return res
}
const getPlaylists = async () => {
let playlistArr = []
await playlists.get().then((QuerySnapshot) => {
QuerySnapshot.forEach((doc) => {
console.log(doc.id)
playlistArr.push({
id: doc.id,
data: doc.data(),
})
})
})
return playlistArr
}
const deletePlaylist = async (playlistId) => {
console.log("HERE")
console.log("FOO: " , typeof playlistId)
const res = await db.collection('playlists').doc(playlistId).delete()
return res
}
// const queryPlaylist = async () => {
// const queryRef = await playlists
// .where("track_data.total_tracks_played", "<", 50)
// .get();
// if (queryRef.empty) {
// console.log("NOPE");
// return;
// } else {
// queryRef.forEach((doc) => {
// console.log(doc.data())
// console.log("YEP");
// });
// }
// };
// console.log(queryPlaylist())
module.exports = {
addNewPlaylist: addNewPlaylist,
getPlaylists: getPlaylists,
deletePlaylist: deletePlaylist,
}