@onurege3467/zerohelper 3.3.0 → 4.0.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.
@@ -0,0 +1,90 @@
1
+ const fs = require("fs");
2
+ const path = require("path");
3
+ const { parse, stringify } = require("csv");
4
+
5
+ class CSVDatabase {
6
+ constructor(filePath) {
7
+ this.filePath = filePath || path.join(__dirname, "database.csv");
8
+ if (!fs.existsSync(this.filePath)) {
9
+ fs.writeFileSync(this.filePath, "key,value\n"); // Başlık satırı
10
+ }
11
+ }
12
+
13
+ async _readCSV() {
14
+ const data = fs.readFileSync(this.filePath, "utf8");
15
+ return new Promise((resolve, reject) => {
16
+ parse(data, { columns: true }, (err, records) => {
17
+ if (err) reject(err);
18
+ else resolve(records);
19
+ });
20
+ });
21
+ }
22
+
23
+ async _writeCSV(records) {
24
+ const data = await new Promise((resolve, reject) => {
25
+ stringify(records, { header: true }, (err, output) => {
26
+ if (err) reject(err);
27
+ else resolve(output);
28
+ });
29
+ });
30
+ fs.writeFileSync(this.filePath, data);
31
+ }
32
+
33
+ async set(key, value) {
34
+ const records = await this._readCSV();
35
+ const index = records.findIndex((record) => record.key === key);
36
+ if (index !== -1) {
37
+ records[index].value = JSON.stringify(value);
38
+ } else {
39
+ records.push({ key, value: JSON.stringify(value) });
40
+ }
41
+ await this._writeCSV(records);
42
+ }
43
+
44
+ async get(key) {
45
+ const records = await this._readCSV();
46
+ const record = records.find((record) => record.key === key);
47
+ return record ? JSON.parse(record.value) : null;
48
+ }
49
+
50
+ async delete(key) {
51
+ const records = await this._readCSV();
52
+ const filteredRecords = records.filter((record) => record.key !== key);
53
+ await this._writeCSV(filteredRecords);
54
+ }
55
+
56
+ async has(key) {
57
+ const value = await this.get(key);
58
+ return value !== null;
59
+ }
60
+
61
+ async push(key, value) {
62
+ const currentValue = (await this.get(key)) || [];
63
+ if (!Array.isArray(currentValue)) throw new Error("Value is not an array");
64
+ currentValue.push(value);
65
+ await this.set(key, currentValue);
66
+ }
67
+
68
+ async add(key, value) {
69
+ const currentValue = (await this.get(key)) || 0;
70
+ if (typeof currentValue !== "number") throw new Error("Value is not a number");
71
+ await this.set(key, currentValue + value);
72
+ }
73
+
74
+ async sub(key, value) {
75
+ const currentValue = (await this.get(key)) || 0;
76
+ if (typeof currentValue !== "number") throw new Error("Value is not a number");
77
+ await this.set(key, currentValue - value);
78
+ }
79
+
80
+ async getAllData() {
81
+ const records = await this._readCSV();
82
+ const result = {};
83
+ records.forEach((record) => {
84
+ result[record.key] = JSON.parse(record.value);
85
+ });
86
+ return result;
87
+ }
88
+ }
89
+
90
+ module.exports = CSVDatabase;
package/database/index.js CHANGED
@@ -9,6 +9,7 @@ var SQLiteDatabase = require("./sqldb/index");
9
9
  var RedisDatabase = require("./redis/index");
10
10
  var PostgreSQL = require("./postgresql/index");
11
11
  var YamlDatabase = require("./yamldatabase/index"); // Assuming you've saved the YAML class in this path
12
+ const CSVDatabase = require("./csvdatabase/index"); // Assuming you've saved the CSV class in this path
12
13
  var MigrateDatabase = require("./migrate/index");
13
14
 
14
15
  module.exports = {
@@ -48,6 +49,7 @@ module.exports = {
48
49
  */
49
50
  PostgreSQL,
50
51
  YamlDatabase,
52
+ CSVDatabase,
51
53
  /**
52
54
  * Migration utility for databases.
53
55
  * @type {MigrateDatabase}
@@ -36,6 +36,8 @@ async function initializeDatabase(config) {
36
36
  return new JsonDatabase(config.options.filePath);
37
37
  case "yaml":
38
38
  return new YamlDatabase(config.options.filePath);
39
+ case "csv":
40
+ return new YamlDatabase(config.options.filePath);
39
41
  case "mongodb":
40
42
  const mongoClient = await MongoDB.createData(
41
43
  config.options.database,
@@ -1,147 +1,76 @@
1
1
  const fs = require("fs");
2
2
  const path = require("path");
3
- const yaml = require("js-yaml"); // You'll need to install this package: npm install js-yaml
3
+ const yaml = require("js-yaml");
4
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}`;
5
+ class YAMLDatabase {
6
+ constructor(filePath) {
7
+ this.filePath = filePath || path.join(__dirname, "database.yaml");
8
+ if (!fs.existsSync(this.filePath)) {
9
+ fs.writeFileSync(this.filePath, yaml.dump({}));
11
10
  }
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
11
  }
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;
12
+
13
+ _readYAML() {
14
+ const data = fs.readFileSync(this.filePath, "utf8");
15
+ return yaml.load(data) || {};
38
16
  }
39
-
40
- getAllData() {
41
- let data = this.read();
42
- if (!data) data = {};
43
- return data;
17
+
18
+ _writeYAML(data) {
19
+ fs.writeFileSync(this.filePath, yaml.dump(data));
44
20
  }
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;
21
+
22
+ async set(key, value) {
23
+ const data = this._readYAML();
24
+ data[key] = value;
25
+ this._writeYAML(data);
52
26
  }
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
- }
27
+
28
+ async get(key) {
29
+ const data = this._readYAML();
30
+ return data[key] || null;
62
31
  }
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;
32
+
33
+ async delete(key) {
34
+ const data = this._readYAML();
35
+ delete data[key];
36
+ this._writeYAML(data);
73
37
  }
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;
38
+
39
+ async has(key) {
40
+ const data = this._readYAML();
41
+ return key in data;
84
42
  }
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);
43
+
44
+ async push(key, value) {
45
+ const data = this._readYAML();
46
+ if (!Array.isArray(data[key])) {
47
+ data[key] = [];
93
48
  }
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;
49
+ data[key].push(value);
50
+ this._writeYAML(data);
107
51
  }
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 {};
52
+
53
+ async add(key, value) {
54
+ const data = this._readYAML();
55
+ if (typeof data[key] !== "number") {
56
+ data[key] = 0;
117
57
  }
58
+ data[key] += value;
59
+ this._writeYAML(data);
118
60
  }
119
- }
120
61
 
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]];
62
+ async sub(key, value) {
63
+ const data = this._readYAML();
64
+ if (typeof data[key] !== "number") {
65
+ data[key] = 0;
66
+ }
67
+ data[key] -= value;
68
+ this._writeYAML(data);
131
69
  }
132
- ref[locations[locations.length - 1]] = value;
133
- return output;
134
- }
135
70
 
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;
71
+ async getAllData() {
72
+ return this._readYAML();
142
73
  }
143
- let output = ref[locations[locations.length - 1]];
144
- return output;
145
74
  }
146
75
 
147
- module.exports = database;
76
+ module.exports = YAMLDatabase;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@onurege3467/zerohelper",
3
- "version": "3.3.0",
3
+ "version": "4.0.0",
4
4
  "main": "index.js",
5
5
  "scripts": {
6
6
  "test": "node test.js"
@@ -27,6 +27,7 @@
27
27
  "dependencies": {
28
28
  "bcrypt": "^5.1.1",
29
29
  "crypto": "^1.0.1",
30
+ "csv": "^6.3.11",
30
31
  "dotenv": "^16.4.7",
31
32
  "fs": "^0.0.1-security",
32
33
  "js-yaml": "^4.1.0",
package/readme.md CHANGED
@@ -17,6 +17,7 @@ ZeroHelper is a versatile JavaScript package providing helper functions and data
17
17
  3. [💾 Database Utilities](#-database-utilities)
18
18
  - [🗃️ JsonDatabase](#jsondatabase-️)
19
19
  - [♦️ YamlDatabase](#yamldatabase-️)
20
+ - [🎋 CSV Database](#csv-database-)
20
21
  - [🗄️ MongoDB](#mongodb-️)
21
22
  - [🐬 MySQL](#mysql-)
22
23
  - [📱 SQLiteDB](#sqlitedb-)
@@ -200,7 +201,23 @@ ZeroHelper provides multiple database utilities for seamless integration with va
200
201
  await console.log(db.has("foo"));
201
202
  })();
202
203
  ```
204
+ # CSV Database 🎋
205
+ ```js
206
+ (async function () {
207
+ const csvdb = require("@onurege3467/zerohelper/database/csvdb");
208
+ const db = new csvdb();
209
+
210
+ await db.set("foo", "bar");
211
+ await db.push("array", "x");
212
+ await db.delete("foo");
203
213
 
214
+ await db.add("number", 1);
215
+ await db.sub("number", 1);
216
+
217
+ await console.log(db.get("foo"));
218
+ await console.log(db.has("foo"));
219
+ })();
220
+ ```
204
221
  # MongoDB 🗄️
205
222
 
206
223
  ```js
@@ -437,6 +454,14 @@ const targetConfig = {
437
454
  }
438
455
  ```
439
456
  ```json
457
+ {
458
+ "type": "csv",
459
+ "options": {
460
+ "filePath": "data.csv"
461
+ }
462
+ }
463
+ ```
464
+ ```json
440
465
  {
441
466
  "type": "mongodb",
442
467
  "options": {