-
Notifications
You must be signed in to change notification settings - Fork 2
/
data.js
67 lines (59 loc) · 1.32 KB
/
data.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
import fs from "fs";
const parsedData = readData("meanings");
var myData = parsedData.data;
const queryData = readData("queries");
var queries = queryData.data;
export var adminDetails = readData("admin").data;
function readData(fName) {
let db = fs.readFileSync(`./processed/${fName}.json`);
return JSON.parse(db);
}
function pushData(path, obj) {
fs.writeFile(
`./processed/${path}.json`,
JSON.stringify({ data: obj }),
(err) => {
if (err) console.log("err occurred while writing data");
else {
console.log(`wrote new data to ${path}.json file successfully`);
}
}
);
}
function removeQuery(id) {
queries = queries.filter((obj) => obj.id !== id);
pushData("queries", queries);
}
function addWord(obj) {
let { WORD } = obj;
if (Object.keys(myData).includes(WORD)) {
return;
}
myData[WORD] = obj;
pushData("meanings", myData);
}
function removeWord(obj) {
let { WORD } = obj;
delete myData[WORD];
pushData("meanings", myData);
}
function editWord(obj) {
let { WORD } = obj;
myData[WORD] = obj;
pushData("meanings", myData);
}
function reWriteAdminData(obj) {
adminDetails = obj;
console.log(adminDetails);
pushData("admin", obj);
}
export {
pushData,
queries,
removeQuery,
addWord,
removeWord,
editWord,
reWriteAdminData,
};
export default myData;