@onurege3467/zerohelper 3.2.1 → 3.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/database/index.js CHANGED
@@ -8,6 +8,7 @@ var MySQLDatabase = require("./mysql/index");
8
8
  var SQLiteDatabase = require("./sqldb/index");
9
9
  var RedisDatabase = require("./redis/index");
10
10
  var PostgreSQL = require("./postgresql/index");
11
+ var YamlDatabase = require("./yamldatabase/index"); // Assuming you've saved the YAML class in this path
11
12
  var MigrateDatabase = require("./migrate/index");
12
13
 
13
14
  module.exports = {
@@ -46,6 +47,7 @@ module.exports = {
46
47
  * @type {PostgreSQL}
47
48
  */
48
49
  PostgreSQL,
50
+ YamlDatabase,
49
51
  /**
50
52
  * Migration utility for databases.
51
53
  * @type {MigrateDatabase}
@@ -1,4 +1,5 @@
1
1
  const JsonDatabase = require("../jsondatabase/index");
2
+ const YamlDatabase = require("../yamldatabase/index"); // Assuming you've saved the YAML class in this path
2
3
  const MongoDB = require("../mongodb/index");
3
4
  const MySQLDatabase = require("../mysql/index");
4
5
  const SQLiteDatabase = require("../sqldb/index");
@@ -33,6 +34,8 @@ async function initializeDatabase(config) {
33
34
  switch (config.type) {
34
35
  case "json":
35
36
  return new JsonDatabase(config.options.filePath);
37
+ case "yaml":
38
+ return new YamlDatabase(config.options.filePath);
36
39
  case "mongodb":
37
40
  const mongoClient = await MongoDB.createData(
38
41
  config.options.database,
@@ -60,4 +63,4 @@ async function initializeDatabase(config) {
60
63
  }
61
64
 
62
65
  // Export the migrateData function
63
- module.exports = migrateData;
66
+ module.exports = migrateData;
@@ -0,0 +1,147 @@
1
+ const fs = require("fs");
2
+ const path = require("path");
3
+ const yaml = require("js-yaml"); // You'll need to install this package: npm install js-yaml
4
+
5
+ class database {
6
+ constructor(name = "database", loc2) {
7
+ var location = "database";
8
+ var filePath = `databases/${name}.yaml`;
9
+ if (name.endsWith(".yaml") || name.endsWith(".yml")) {
10
+ filePath = `databases/${name}`;
11
+ }
12
+ if (location == "database" && !fs.existsSync(`databases`)) {
13
+ fs.mkdirSync(`databases`, { recursive: true });
14
+ } else if (!fs.existsSync(`${location}`)) {
15
+ fs.mkdirSync(`databases`, { recursive: true });
16
+ }
17
+ if (loc2) {
18
+ if (!fs.existsSync(`${loc2}`)) {
19
+ fs.mkdirSync(`databases/${loc2}`, { recursive: true });
20
+ }
21
+ filePath = `databases/${loc2}/${name}.yaml`;
22
+ if (name.endsWith(".yaml") || name.endsWith(".yml")) {
23
+ filePath = `databases/${loc2}/${name}`;
24
+ }
25
+ }
26
+
27
+ if (!fs.existsSync(filePath)) fs.closeSync(fs.openSync(filePath, "w"));
28
+ this.FilePath = filePath;
29
+ this.Location = location;
30
+ }
31
+
32
+ add(path, value) {
33
+ let data = this.get(path);
34
+ if (typeof data == "number") data += Number(value);
35
+ else data = Number(value);
36
+ this.set(path, data);
37
+ return data;
38
+ }
39
+
40
+ getAllData() {
41
+ let data = this.read();
42
+ if (!data) data = {};
43
+ return data;
44
+ }
45
+
46
+ get(path) {
47
+ let data = this.read(),
48
+ result = undefined;
49
+ if (!data) data = {};
50
+ result = _get(path, data);
51
+ return result ? result : undefined;
52
+ }
53
+
54
+ has(path) {
55
+ let data = this.read(),
56
+ result = undefined;
57
+ result = _get(path, data);
58
+ if (!result) return false;
59
+ else {
60
+ return true;
61
+ }
62
+ }
63
+
64
+ set(path, value) {
65
+ let data = this.read();
66
+ if (!data) data = {};
67
+ data = _set(path, value, data);
68
+ fs.truncateSync(this.FilePath);
69
+ fs.writeFileSync(this.FilePath, yaml.dump(data), {
70
+ encoding: "utf-8",
71
+ });
72
+ return data;
73
+ }
74
+
75
+ delete(path) {
76
+ let data = this.read();
77
+ if (!data) data = {};
78
+ data = _set(path, undefined, data);
79
+ fs.truncateSync(this.FilePath);
80
+ fs.writeFileSync(this.FilePath, yaml.dump(data), {
81
+ encoding: "utf-8",
82
+ });
83
+ return data;
84
+ }
85
+
86
+ push(path, value) {
87
+ let data = this.read();
88
+ if (!data) data = {};
89
+ if (_get(path, data) && Array.isArray(_get(path, data))) {
90
+ _get(path, data).push(value);
91
+ } else if (!_get(path, data)) {
92
+ _set(path, [value], data);
93
+ }
94
+ fs.truncateSync(this.FilePath);
95
+ fs.writeFileSync(this.FilePath, yaml.dump(data), {
96
+ encoding: "utf-8",
97
+ });
98
+ return data;
99
+ }
100
+
101
+ sub(path, value) {
102
+ let data = this.get(path);
103
+ if (typeof data == "number") data -= Number(value);
104
+ else data = Number(value);
105
+ this.set(path, data);
106
+ return data;
107
+ }
108
+
109
+ read() {
110
+ let data = fs.readFileSync(this.FilePath, { encoding: "utf-8" });
111
+ if (!data || (data && data == null)) return {};
112
+ try {
113
+ let obj = yaml.load(data);
114
+ return obj || {};
115
+ } catch (e) {
116
+ return {};
117
+ }
118
+ }
119
+ }
120
+
121
+ // The _set and _get helper functions remain the same as they work with objects
122
+ function _set(path, value, obj = undefined) {
123
+ if (obj == undefined) return undefined;
124
+ let locations = path.split("."),
125
+ output = {};
126
+ output = obj;
127
+ let ref = output;
128
+ for (let index = 0; index < locations.length - 1; index++) {
129
+ if (!ref[locations[index]]) ref = ref[locations[index]] = {};
130
+ else ref = ref[locations[index]];
131
+ }
132
+ ref[locations[locations.length - 1]] = value;
133
+ return output;
134
+ }
135
+
136
+ function _get(path, obj = {}) {
137
+ let locations = path.split("."),
138
+ ref = obj;
139
+ for (let index = 0; index < locations.length - 1; index++) {
140
+ ref = ref[locations[index]] ? ref[locations[index]] : undefined;
141
+ if (!ref) return undefined;
142
+ }
143
+ let output = ref[locations[locations.length - 1]];
144
+ return output;
145
+ }
146
+
147
+ module.exports = database;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@onurege3467/zerohelper",
3
- "version": "3.2.1",
3
+ "version": "3.3.0",
4
4
  "main": "index.js",
5
5
  "scripts": {
6
6
  "test": "node test.js"
@@ -29,6 +29,7 @@
29
29
  "crypto": "^1.0.1",
30
30
  "dotenv": "^16.4.7",
31
31
  "fs": "^0.0.1-security",
32
+ "js-yaml": "^4.1.0",
32
33
  "jsonwebtoken": "^9.0.2",
33
34
  "lodash": "^4.17.21",
34
35
  "mongodb": "^6.12.0",
package/readme.md CHANGED
@@ -16,6 +16,7 @@ ZeroHelper is a versatile JavaScript package providing helper functions and data
16
16
  - [➗ Math Functions](#math-functions-)
17
17
  3. [💾 Database Utilities](#-database-utilities)
18
18
  - [🗃️ JsonDatabase](#jsondatabase-️)
19
+ - [♦️ YamlDatabase](#yamldatabase-️)
19
20
  - [🗄️ MongoDB](#mongodb-️)
20
21
  - [🐬 MySQL](#mysql-)
21
22
  - [📱 SQLiteDB](#sqlitedb-)
@@ -182,6 +183,23 @@ ZeroHelper provides multiple database utilities for seamless integration with va
182
183
  await console.log(db.has("foo"));
183
184
  })();
184
185
  ```
186
+ # YamlDatabase 🗃️
187
+ ```js
188
+ (async function () {
189
+ const YamlDatabase = require("@onurege3467/zerohelper/database/yamldatabase");
190
+ const db = new YamlDatabase();
191
+
192
+ await db.set("foo", "bar");
193
+ await db.push("array", "x");
194
+ await db.delete("foo");
195
+
196
+ await db.add("number", 1);
197
+ await db.sub("number", 1);
198
+
199
+ await console.log(db.get("foo"));
200
+ await console.log(db.has("foo"));
201
+ })();
202
+ ```
185
203
 
186
204
  # MongoDB 🗄️
187
205
 
@@ -410,7 +428,14 @@ const targetConfig = {
410
428
  }
411
429
  }
412
430
  ```
413
-
431
+ ```json
432
+ {
433
+ "type": "yaml",
434
+ "options": {
435
+ "filePath": "data.yaml"
436
+ }
437
+ }
438
+ ```
414
439
  ```json
415
440
  {
416
441
  "type": "mongodb",