1
|
|
|
/** |
2
|
|
|
* Database |
3
|
|
|
*/ |
4
|
|
|
'use strict' |
5
|
|
|
|
6
|
1 |
|
const fs = require('fs') |
7
|
1 |
|
const _ = require('lodash') |
8
|
|
|
|
9
|
1 |
|
const __ = { |
10
|
|
|
getDatabase: database => { |
11
|
10 |
|
if (!fs.existsSync(database) || !fs.readFileSync(database, 'utf8')) { |
12
|
3 |
|
return false |
13
|
|
|
} |
14
|
|
|
|
15
|
7 |
|
return JSON.parse(fs.readFileSync(database, 'utf8')) |
16
|
|
|
} |
17
|
|
|
} |
18
|
|
|
|
19
|
1 |
|
const Database = () => { |
20
|
1 |
|
const proto = { |
21
|
|
|
data: {}, |
22
|
|
|
database: '', |
23
|
|
|
|
24
|
|
|
databaseExists: function () { |
25
|
1 |
|
return fs.existsSync(this.database) |
26
|
|
|
}, |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Set file database. |
30
|
|
|
* |
31
|
|
|
* @param {string} pathname |
32
|
|
|
*/ |
33
|
|
|
setFile: function (pathname) { |
34
|
6 |
|
let regex = new RegExp(/\.json$/) |
35
|
|
|
|
36
|
6 |
|
if (!regex.test(pathname)) { |
37
|
1 |
|
throw new Error('The database file must be a json file') |
38
|
|
|
} |
39
|
|
|
|
40
|
5 |
|
this.database = pathname |
41
|
|
|
|
42
|
5 |
|
return this |
43
|
|
|
}, |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Add a key and value for database. |
47
|
|
|
* |
48
|
|
|
* @param {string} key |
49
|
|
|
* @param {string} value |
50
|
|
|
*/ |
51
|
|
|
add: function (key, value) { |
52
|
5 |
|
this.data = __.getDatabase(this.database) || this.data |
53
|
5 |
|
this.data[key] = value |
54
|
|
|
|
55
|
5 |
|
return this |
56
|
|
|
}, |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Add values to env vars from node. |
60
|
|
|
* |
61
|
|
|
* @param {object} data |
62
|
|
|
*/ |
63
|
|
|
addEnv: function (data) { |
64
|
2 |
|
if (typeof data !== 'object') { |
65
|
1 |
|
throw new Error('data must be an object') |
66
|
|
|
} |
67
|
|
|
|
68
|
1 |
|
_.map(data, (value, key) => { |
69
|
1 |
|
process.env[key] = value |
70
|
|
|
}) |
71
|
|
|
}, |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Add multiples keys to database. |
75
|
|
|
* |
76
|
|
|
* @param {object} data |
77
|
|
|
*/ |
78
|
|
|
massive: function (data) { |
79
|
2 |
|
if (typeof data !== 'object') { |
80
|
1 |
|
throw new Error('data must be an object') |
81
|
|
|
} |
82
|
|
|
|
83
|
2 |
|
_.map(data, (value, key) => this.add(key, value)) |
84
|
|
|
|
85
|
1 |
|
return this |
86
|
|
|
}, |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Return an key from database or all data. |
90
|
|
|
* |
91
|
|
|
* @param {string} item |
92
|
|
|
*/ |
93
|
|
|
get: function (item) { |
94
|
5 |
|
let db = __.getDatabase(this.database) || this.data |
95
|
|
|
|
96
|
5 |
|
if (item !== undefined && !(item in db)) { |
97
|
1 |
|
throw new Error(`${item} is undefined`) |
98
|
|
|
} |
99
|
|
|
|
100
|
4 |
|
return db[item] || db |
101
|
|
|
}, |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Store data to db.json. |
105
|
|
|
*/ |
106
|
|
|
store: function () { |
107
|
2 |
|
fs.writeFile(this.database, JSON.stringify(this.data), err => { |
108
|
2 |
|
if (err) { |
109
|
1 |
|
throw new Error(err) |
110
|
|
|
} |
111
|
|
|
|
112
|
1 |
|
return this |
113
|
|
|
}) |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
117
|
1 |
|
return Object.create(proto) |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
module.exports = Database() |
121
|
|
|
|