ulduldp's picture
Create storage/db.js
210f2be verified
raw
history blame contribute delete
494 Bytes
const fs = require("fs");
const path = require("path");
const base = path.join(__dirname, "../data");
function read(file) {
const filePath = path.join(base, file);
if (!fs.existsSync(filePath)) return [];
return JSON.parse(fs.readFileSync(filePath));
}
function write(file, data) {
const filePath = path.join(base, file);
const temp = filePath + ".tmp";
fs.writeFileSync(temp, JSON.stringify(data, null, 2));
fs.renameSync(temp, filePath);
}
module.exports = { read, write };