-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.js
More file actions
63 lines (53 loc) · 1.62 KB
/
database.js
File metadata and controls
63 lines (53 loc) · 1.62 KB
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
const { MongoClient } = require('mongodb');
const bcrypt = require('bcrypt');
const uuid = require('uuid');
const config = require('./dbConfig.json');
const url = `mongodb+srv://${config.userName}:${config.password}@${config.hostname}`;
const client = new MongoClient(url);
const db = client.db('simon');
const userCollection = db.collection('user');
const scoreCollection = db.collection('score');
// This will asynchronously test the connection and exit the process if it fails
(async function testConnection() {
await client.connect();
await db.command({ ping: 1 });
})().catch((ex) => {
console.log(`Unable to connect to database with ${url} because ${ex.message}`);
process.exit(1);
});
function getUser(email) {
return userCollection.findOne({ email: email.toString() });
}
function getUserByToken(token) {
return userCollection.findOne({ token: token.toString() });
}
async function createUser(email, password) {
// Hash the password before we insert it into the database
const passwordHash = await bcrypt.hash(password.toString(), 10);
const user = {
email: email.toString(),
password: passwordHash.toString(),
token: uuid.v4().toString(),
};
await userCollection.insertOne(user);
return user;
}
function addScore(score) {
scoreCollection.insertOne(Number.isInteger(score)?score:-1);
}
function getHighScores() {
const query = { score: { $gt: 0, $lt: 1000 } };
const options = {
sort: { score: -1 },
limit: 10,
};
const cursor = scoreCollection.find(query, options);
return cursor.toArray();
}
module.exports = {
getUser,
getUserByToken,
createUser,
addScore,
getHighScores,
};