-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.js
39 lines (35 loc) · 1.12 KB
/
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
const loki = require('lokijs')
const lfsa = require('lokijs/src/loki-fs-structured-adapter')
const fs = require('fs')
const config = require('./config')
let db
let msgs
module.exports = {
init() {
if (fs.existsSync(config.databaseDirectory)) {
const stats = fs.statSync(config.databaseDirectory)
if (!stats.isDirectory()) {
console.error(`${config.databaseDirectory} is not a folder!`)
process.exit(-1)
}
} else {
fs.mkdirSync(config.databaseDirectory)
}
db = new loki(config.databaseDirectory + '/data', {
adapter: new lfsa(),
autoload: true,
autoloadCallback: () => {
msgs = db.getCollection('messages')
if (msgs == null) {
msgs = db.addCollection('messages', {
unique: ['id'],
indices: ['author']
})
}
},
autosave: false,
})
setInterval(() => db.saveDatabase(), 30000)
},
messages: () => { return msgs }
}