Create storage/db.js
Browse files- storage/db.js +20 -0
storage/db.js
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
const fs = require("fs");
|
| 2 |
+
const path = require("path");
|
| 3 |
+
|
| 4 |
+
const base = path.join(__dirname, "../data");
|
| 5 |
+
|
| 6 |
+
function read(file) {
|
| 7 |
+
const filePath = path.join(base, file);
|
| 8 |
+
if (!fs.existsSync(filePath)) return [];
|
| 9 |
+
return JSON.parse(fs.readFileSync(filePath));
|
| 10 |
+
}
|
| 11 |
+
|
| 12 |
+
function write(file, data) {
|
| 13 |
+
const filePath = path.join(base, file);
|
| 14 |
+
const temp = filePath + ".tmp";
|
| 15 |
+
|
| 16 |
+
fs.writeFileSync(temp, JSON.stringify(data, null, 2));
|
| 17 |
+
fs.renameSync(temp, filePath);
|
| 18 |
+
}
|
| 19 |
+
|
| 20 |
+
module.exports = { read, write };
|