forked from brainfoolong/web-ftp-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.js
75 lines (67 loc) · 1.5 KB
/
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
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
'use strict'
const path = require('path')
const low = require('lowdb')
const hash = require(path.join(__dirname, 'hash'))
/**
* LowDB helper
*/
const db = {}
/**
* The db defaults
* @type {object<string, *>}
* @private
*/
db._defaults = {
'id': {'id': 0},
'servers': {},
'settings': {'salt': hash.random(64)},
'users': {},
'logs': {},
'splitboxtabs': {'tabs': {}},
'queue': {'entries': {}, 'settings': {'mode': 'replace-always'}}
}
/**
* Which dbs are in memory
* @type {Array}
* @private
*/
db._inMemory = ['queue', 'logs']
/**
* The instances
* @type {object<string, low>}
* @private
*/
db._instances = []
/**
* Get next id
* @returns {number}
*/
db.getNextId = function () {
let id = db.get('id').value()
id.id++
db.get('id').set('id', id.id).write()
return id.id
}
/**
* Get lowdb instance
* @param {string} file
* @param {string=} folder
* @returns {low}
*/
db.get = function (file, folder) {
let relativePath = folder ? folder + '/' + file : file
let filepath = path.join(__dirname, '../db')
if (folder) filepath = path.join(filepath, folder)
filepath = path.join(filepath, file + '.json')
if (typeof db._instances[relativePath] !== 'undefined') {
return db._instances[relativePath]
}
const inst = low(db._inMemory.indexOf(relativePath) > -1 ? undefined : filepath)
// set defaults
if (typeof db._defaults[file] !== 'undefined') {
inst.defaults(db._defaults[file]).write()
}
db._instances[relativePath] = inst
return inst
}
module.exports = db