First commit

This commit is contained in:
Charles Sharma 2022-07-16 14:22:26 -06:00
commit 696a772451
4 changed files with 33 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
test.json

17
index.js Normal file
View File

@ -0,0 +1,17 @@
let fs = require("fs");
function loadDB(databaseFile) {
let _db, db;
if (fs.existsSync(databaseFile)) {
_db = JSON.parse(fs.readFileSync(databaseFile, "utf8"));
} else {
fs.writeFileSync(databaseFile, JSON.stringify({}));
_db = {};
}
return new Proxy(_db, {
set: function (target, key, value) {
target[key] = value;
fs.writeFileSync(databaseFile, JSON.stringify(target));
},
});
}
exports.loadDB = loadDB;

12
package.json Normal file
View File

@ -0,0 +1,12 @@
{
"name": "jsondb",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC"
}

3
test.js Normal file
View File

@ -0,0 +1,3 @@
let { loadDB } = require("./index.js");
let db = loadDB("test.json");
console.log(db.test);