This repository was archived by the owner on Dec 4, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb.js
More file actions
46 lines (42 loc) · 1.8 KB
/
db.js
File metadata and controls
46 lines (42 loc) · 1.8 KB
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
// --- Required librairies ---
const fs = require('fs');
// --- Configs ---
const config = JSON.parse(fs.readFileSync('./config.json')).json_db;
// -- JSON parse error resolve --
function json_resolve(str) {
str = str.replace(/\\n/g, "\\n")
.replace(/\\'/g, "\\'")
.replace(/\\"/g, '\\"')
.replace(/\\&/g, "\\&")
.replace(/\\r/g, "\\r")
.replace(/\\t/g, "\\t")
.replace(/\\b/g, "\\b")
.replace(/\\f/g, "\\f");
// remove non-printable and other non-valid JSON chars
str = str.replace(/[\u0000-\u001F]+/g, "");
return str;
}
module.exports = {
getTable: function (table_name) {
if (!fs.existsSync('./db/')) fs.mkdirSync('./db/', { recursive: true });
if (!fs.existsSync('./db/' + table_name + '.json')) fs.writeFileSync('./db/' + table_name + '.json', "{}");
let table = JSON.parse(fs.readFileSync('./db/' + table_name + '.json'));
return table;
},
get: function (table_name, row, column) {
if (!fs.existsSync('./db/')) fs.mkdirSync('./db/', { recursive: true });
if (!fs.existsSync('./db/' + table_name + '.json')) fs.writeFileSync('./db/' + table_name + '.json', "{}");
let table = JSON.parse(fs.readFileSync('./db/' + table_name + '.json'));
if (!table[row]) return false;
if (!table[row][column]) return false;
return table[row][column];
},
set: function (table_name, row, column, data) {
if (!fs.existsSync('./db/')) fs.mkdirSync('./db/', { recursive: true });
let table = JSON.parse(fs.readFileSync('./db/' + table_name + '.json'));
if (!table[row]) table[row] = {};
table[row][column] = data;
fs.writeFileSync('./db/' + table_name + '.json', JSON.stringify(table));
},
resolve: json_resolve
};