-
Notifications
You must be signed in to change notification settings - Fork 0
/
mongodb.js
188 lines (166 loc) · 4.95 KB
/
mongodb.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
const MongoClient = require("mongodb").MongoClient;
// Connection URL
const URL = "mongodb://localhost:27017";
// Database Name
const dbName = "MusiQDB";
// Function to initialize and create the database.
async function createDB(URL, dbName) {
const client = await MongoClient.connect(URL, {
useNewUrlParser: true,
useUnifiedTopology: true,
}).catch((err) => {
console.log(err);
});
try {
console.log("Successfully Connect...");
const db = client.db(dbName);
console.log(`Creating database: ${dbName} ...`);
insertSong([song1, song2, song3, song4, song5, song6]);
// Seed and insert sample user and songs
user1 = { username: "admin", password: "admin" };
user2 = { username: "feroz", password: "feroz123" };
insertUser(user1);
insertUser(user2);
console.log("objects added");
console.log(`Database running at: ${URL}`);
} catch (exception) {
console.log(`Exception: ${exception}`);
}
}
// Inserts a user in the database collection
async function insertUser(userObj) {
(async () => {
const client = await MongoClient.connect(URL, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
let db = client.db(dbName);
try {
const res = await db.collection("user").insertOne(userObj);
// console.log(`Inserted: ${JSON.stringify(res["ops"])}`);
client.close();
} catch (err) {
console.log(err);
} finally {
if (!client) client.close();
}
})().catch((err) => console.error(err));
}
async function insertSong(songObj) {
(async () => {
const client = await MongoClient.connect(URL, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
let db = client.db(dbName);
try {
const res = await db.collection("song").insertMany(songObj);
// console.log(`Inserted: ${JSON.stringify(res["ops"])}`);
client.close();
} catch (err) {
console.log(err);
} finally {
if (!client) client.close();
}
})().catch((err) => console.error(err));
}
// Given a user obj, finds and retrieves the user matching the query
async function findUser(userObj) {
console.log("finding User", userObj);
(async () => {
const client = await MongoClient.connect(URL, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
const db = client.db(dbName);
try {
const res = await db.collection("user").findOne(userObj);
console.log(`Found: ${JSON.stringify(res)}`);
client.close();
} catch (err) {
console.log(err);
} finally {
if (!client) client.close();
}
})().catch((err) => console.error(err));
}
function getSongs() {
var data = [];
return (async () => {
const client = await MongoClient.connect(URL, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
const db = client.db(dbName);
try {
await db
.collection("song")
.find({})
.toArray()
.then((results) => {
data = results;
return results;
})
.catch((error) => console.error(error));
return data;
} catch (err) {
console.log(err);
} finally {
if (!client) client.close();
}
})().catch((err) => console.error(err));
// return data;
}
// Creates the database when the file is loaded
function createSchema() {
try {
createDB(URL, dbName);
console.log(`${dbName} creation complete`);
} catch (err) {
console.log("damn");
}
}
song1 = {
artistName: "Will Carpenter",
trackName: "Weird Singing Noise",
albumCover:
"https://s3.ca-central-1.amazonaws.com/audio.musiq.com/album-1.jpeg",
URL: "https://s3.ca-central-1.amazonaws.com/audio.musiq.com/audio-1.mp3",
};
song2 = {
artistName: "Erik Conta",
trackName: "All Time Low",
albumCover:
"https://s3.ca-central-1.amazonaws.com/audio.musiq.com/album-2.jpeg",
URL: "https://s3.ca-central-1.amazonaws.com/audio.musiq.com/audio-2.mp3",
};
song3 = {
artistName: "Tiny Wayne",
trackName: "I Can't Sing",
albumCover:
"https://s3.ca-central-1.amazonaws.com/audio.musiq.com/album-3.jpeg",
URL: "https://s3.ca-central-1.amazonaws.com/audio.musiq.com/audio-3.mp3",
};
song4 = {
artistName: "Big Daddy",
trackName: "Tiny Son Noises",
albumCover:
"https://s3.ca-central-1.amazonaws.com/audio.musiq.com/album-4.jpeg",
URL: "https://s3.ca-central-1.amazonaws.com/audio.musiq.com/audio-4.mp3",
};
song5 = {
artistName: "Wanye Kest",
trackName: "Confused Man",
albumCover:
"https://s3.ca-central-1.amazonaws.com/audio.musiq.com/album-5.jpeg",
URL: "https://s3.ca-central-1.amazonaws.com/audio.musiq.com/audio-5.mp3",
};
song6 = {
artistName: "Alabama Man",
trackName: "Weird Country Noises",
albumCover:
"https://s3.ca-central-1.amazonaws.com/audio.musiq.com/album-6.jpeg",
URL: "https://s3.ca-central-1.amazonaws.com/audio.musiq.com/audio-6.mp3",
};
// Exports modules and functions to be used by the api...
module.exports = { insertUser, findUser, createDB, getSongs, createSchema };