-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.js
26 lines (21 loc) · 815 Bytes
/
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
require('dotenv').config();
const { MongoClient } = require('mongodb');
let db;
async function connectToDb() {
const url = process.env.DB_URL;
const client = new MongoClient(url, { useNewUrlParser: true, useUnifiedTopology: true });
await client.connect();
console.log(`${new Date()}: Connected to MongoDB at ${url}`);
db = client.db();
}
//calcalate the next playlist's id
async function getNextPlaylistID(email) {
const result = await db.collection('user').findOne({ email }) ;
//console.log("array length" + result.playlists.length+1);
return result.playlists.length ? result.playlists[result.playlists.length-1].id +1 : 1 ;
// return result.playlists.length ? result.playlists.length+ 1 : 1;
}
function getDb() {
return db;
}
module.exports = { connectToDb, getDb, getNextPlaylistID };