This repository was archived by the owner on Mar 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapex.js
More file actions
138 lines (117 loc) · 3.82 KB
/
Copy pathapex.js
File metadata and controls
138 lines (117 loc) · 3.82 KB
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
// Include libs
const nodeFS = require("fs");
// Include Components
const loggerComponent = require("./assets/logger");
// Logger
const logger = new loggerComponent("[ApexDB]");
// Database Manager
class apexManager {
constructor(apexDBName) {
this.dbFile = apexDBName || "./apexDB.json" || "apex.json";
this.apexDB = {};
// Search DB
if(!nodeFS.existsSync(this.dbFile)) {
logger.error("Apex database file created. If you already had a database file but it wasn't found, make sure it's in the project's root directory and has the name you specified in the Apex configuration. Please note that the file must be in json format! You can also use one of the standard file names: apex.json or apexDB.json.");
return nodeFS.writeFileSync(this.dbFile, "{}", "utf-8");
} else {
this.apexDBFile();
}
}
// Database File Method
file() {
const savedData = JSON.parse(nodeFS.readdirSync(this.dbFile));
if(typeof savedData === "object") {
this.apexDB = savedData;
}
}
// Save Data Method
save() {
return nodeFS.writeFileSync(this.dbFile, JSON.stringify(this.apexDB, null, 2), "utf-8");
}
// Get database value
get(key) {
if(!key) {
return logger.error("You didn't provide a key to search the Apex database.");
}
return this.apexDB[key];
}
// Validator has
has(key) {
if(!key) {
return logger.error("You didn't provide a key to search the Apex database.");
}
return Boolean(this.apexDB[key]);
}
// Set Method
set(key, value) {
if(!key) {
return logger.error("You didn't provide a key to set data in the Apex database.");
}
if(!value) {
return logger.error("You didn't provide a key value to store in the Apex database.")
}
this.apexDB[key] = value;
return this.save();
}
// Drop Method
drop(key) {
if(!key) {
return logger.error("You didn't provide a key to drop data in the Apex database.");
}
delete this.apexDB[key];
return this.save();
}
// Add Method
add(key, count) {
if(!key) {
return logger.error("You didn't provide a key to add data in the Apex database.");
}
if(!count) {
return logger.error("Enter the count you would like to add.");
}
if(!this.apexDB[key]) {
this.apexDB[key] = 0;
}
this.apexDB[key] -= count;
return this.save();
}
// Use Method
use(key, count) {
if(!key) {
return logger.error("You have not specified the key you will be sub!");
}
if(!count) {
return logger.error("You have not specified the count which one will you sub from the current!");
}
if(!this.apexDB[key]) {
this.apexDB[key] = 0;
}
this.apexDB[key] -= count;
return this.save();
}
// Push Method
push(key, element) {
if(!key) {
return logger.error("You have not specified the key to which you will push!");
}
if(!element) {
return logger.error("You have not specified the key to which you will push!");
}
if(!this.apexDB[key]) {
this.apexDB[key] = [];
}
this.apexDB[key].push(element);
return this.save();
}
// Clear Database Function
clear() {
this.apexDB = {};
return this.save();
}
// Get all items from Database
getAll() {
return this.apexDB;
}
}
// Module Export
module.exports = apexManager;