-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.js
31 lines (25 loc) · 951 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
27
28
29
30
31
const { uuid } = require('uuidv4');
const low = require('lowdb');
const FileSync = require('lowdb/adapters/FileSync');
const adapter = new FileSync('.data/db.json');
const db = low(adapter);
if(process.env.ADMIN_KEY.length > 0 && !process.env.DB_INITIALIZED){
process.env.DB_INITIALIZED = true;
// Set some defaults (required if your JSON file is empty)
db.defaults({ users: [{ id:uuid(), name: 'admin', pw: process.env.ADMIN_KEY, isPlaying:false }]})
.write();
db.get('users').filter({isPlaying:true}).map(u=>{
u.isPlaying = false;
return u
}).write();
db.get('users').filter({name:'admin'}).map(u=>{
if(u.pw != process.env.ADMIN_KEY)u.pw = process.env.ADMIN_KEY;
return u
}).write();
db.get('users').value().forEach(user=>{
console.log(user.name, user.pw);
});
} else {
//console.log('Set the value of ADMIN_KEY in .env, for example ADMIN_KEY="34crrg344"');
}
module.exports = db;