-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJDB.js
170 lines (148 loc) · 4 KB
/
JDB.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
const path = require("path");
const fs = require("fs/promises");
class JDB {
constructor(baseDir, indent = 4) {
this.baseDir = baseDir;
this.indent = indent;
this.queue = Promise.resolve();
}
_getDirectoryPath(dirname) {
return path.join(this.baseDir, dirname);
}
_getFilePath(dirname) {
return path.join(this._getDirectoryPath(dirname), "jdb.json");
}
async _checkDirectoryExists(dirname) {
try {
await fs.access(this._getDirectoryPath(dirname));
return true;
} catch {
return false;
}
}
async _createDirectory(dirname) {
if (!(await this._checkDirectoryExists(dirname))) {
await fs.mkdir(this._getDirectoryPath(dirname), {
recursive: true,
});
}
}
setIndent(indent) {
this.indent = indent;
}
async create(input, dirname = "") {
try {
await this._createDirectory(dirname);
await fs.writeFile(
this._getFilePath(dirname),
JSON.stringify(input, null, this.indent)
);
} catch (error) {
throw new Error("Failed to create: " + error.message);
}
}
async read(dirname = "") {
if (await this._checkDirectoryExists(dirname)) {
const data = await fs.readFile(this._getFilePath(dirname), "utf-8");
try {
return JSON.parse(data);
} catch (e) {
return null;
}
} else {
throw new Error("Database not found");
}
}
async update(input, dirname = "") {
if (await this._checkDirectoryExists(dirname)) {
await fs.writeFile(
this._getFilePath(dirname),
JSON.stringify(input, null, this.indent)
);
} else {
throw new Error("Database not found");
}
}
async patch(input, dirname = "") {
this.queue = this.queue.then(async () => {
if (await this._checkDirectoryExists(dirname)) {
const data = await this.read(dirname);
if (data) {
const updatedData = { ...data, ...input };
await fs.writeFile(
this._getFilePath(dirname),
JSON.stringify(updatedData, null, this.indent)
);
}
} else {
throw new Error("Database not found");
}
});
return this.queue;
}
async delete(dirname) {
if (await this._checkDirectoryExists(dirname)) {
await fs.rm(this._getDirectoryPath(dirname), { recursive: true });
} else {
throw new Error("Database not found");
}
}
async print(dirname = "") {
if (await this._checkDirectoryExists(dirname)) {
console.log(await this.read(dirname));
} else {
throw new Error("Database not found");
}
}
// Shortened CRUD operations
async c(key, value, dirname = "") {
await this.create({ [key]: value }, dirname);
}
async r(key, dirname = "") {
const data = await this.read(dirname);
if (data && data[key]) {
return data[key];
} else {
throw new Error("Key not found");
}
}
async u(key, value, dirname = "") {
await this.update({ [key]: value }, dirname);
}
async d(key, dirname = "") {
const data = await this.read(dirname);
if (data && data[key]) {
delete data[key];
await this.create(data, dirname);
} else {
throw new Error("Key not found");
}
}
async p(key, value, dirname = "") {
await this.patch({ [key]: value }, dirname);
}
// Special operations
async prt(key, dirname = "") {
const data = await this.read(dirname);
if (data && data[key]) {
console.log({ [key]: data[key] });
} else {
throw new Error("Key not found");
}
}
async i(value, dirname = "") {
const data = await this.read(dirname);
const highestKey = Math.max(...Object.keys(data).map(Number), -1);
await this.p(highestKey + 1, value, dirname);
}
async l(desc, value = "", dirname = "") {
await this.i(`${desc} ${value}`, dirname);
}
async f(key, value, dirname = "") {
const data = await this.read(dirname);
if (!data || !data[key]) {
await this.u(key, value, dirname);
}
}
}
module.exports = JDB;