-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.js
46 lines (36 loc) · 1013 Bytes
/
database.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
import Datastore from 'nedb';
const db = new Datastore({ filename: './players.db', autoload: true });
// Player handling
const getPlayer = (req, res) => {
db.findOne({ playername: req.params.playername }, (err, doc) => {
if (err) {
res.status(500).end();
return;
}
res.json(doc);
});
};
const savePlayer = (req, res) => {
const playerObject = req.body;
db.insert(playerObject, (err) => {
if (err) {
console.log(`Err: ${err}`);
}
});
res.status(200).end();
};
const updatePlayer = (req, res) => {
const playerObject = req.body;
db.update({ playername: req.params.playername }, playerObject, {}, (err, numReplaced) => {
if (err) {
console.log(`${playerObject.playername} could not be updated. ${numReplaced} docs updated.`);
res.status(500).end();
}
});
res.status(200).end();
};
export default {
getPlayer,
savePlayer,
updatePlayer,
};