@onurege3467/zerohelper 3.2.1 → 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.
- package/database/csvdb/index.js +90 -0
- package/database/index.js +4 -0
- package/database/migrate/index.js +6 -1
- package/database/yamldatabase/index.js +76 -0
- package/package.json +3 -1
- package/readme.md +51 -1
|
@@ -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
|
@@ -8,6 +8,8 @@ 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
|
|
12
|
+
const CSVDatabase = require("./csvdatabase/index"); // Assuming you've saved the CSV class in this path
|
|
11
13
|
var MigrateDatabase = require("./migrate/index");
|
|
12
14
|
|
|
13
15
|
module.exports = {
|
|
@@ -46,6 +48,8 @@ module.exports = {
|
|
|
46
48
|
* @type {PostgreSQL}
|
|
47
49
|
*/
|
|
48
50
|
PostgreSQL,
|
|
51
|
+
YamlDatabase,
|
|
52
|
+
CSVDatabase,
|
|
49
53
|
/**
|
|
50
54
|
* Migration utility for databases.
|
|
51
55
|
* @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,10 @@ 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);
|
|
39
|
+
case "csv":
|
|
40
|
+
return new YamlDatabase(config.options.filePath);
|
|
36
41
|
case "mongodb":
|
|
37
42
|
const mongoClient = await MongoDB.createData(
|
|
38
43
|
config.options.database,
|
|
@@ -60,4 +65,4 @@ async function initializeDatabase(config) {
|
|
|
60
65
|
}
|
|
61
66
|
|
|
62
67
|
// Export the migrateData function
|
|
63
|
-
module.exports = migrateData;
|
|
68
|
+
module.exports = migrateData;
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
const fs = require("fs");
|
|
2
|
+
const path = require("path");
|
|
3
|
+
const yaml = require("js-yaml");
|
|
4
|
+
|
|
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({}));
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
_readYAML() {
|
|
14
|
+
const data = fs.readFileSync(this.filePath, "utf8");
|
|
15
|
+
return yaml.load(data) || {};
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
_writeYAML(data) {
|
|
19
|
+
fs.writeFileSync(this.filePath, yaml.dump(data));
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
async set(key, value) {
|
|
23
|
+
const data = this._readYAML();
|
|
24
|
+
data[key] = value;
|
|
25
|
+
this._writeYAML(data);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
async get(key) {
|
|
29
|
+
const data = this._readYAML();
|
|
30
|
+
return data[key] || null;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
async delete(key) {
|
|
34
|
+
const data = this._readYAML();
|
|
35
|
+
delete data[key];
|
|
36
|
+
this._writeYAML(data);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
async has(key) {
|
|
40
|
+
const data = this._readYAML();
|
|
41
|
+
return key in data;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
async push(key, value) {
|
|
45
|
+
const data = this._readYAML();
|
|
46
|
+
if (!Array.isArray(data[key])) {
|
|
47
|
+
data[key] = [];
|
|
48
|
+
}
|
|
49
|
+
data[key].push(value);
|
|
50
|
+
this._writeYAML(data);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
async add(key, value) {
|
|
54
|
+
const data = this._readYAML();
|
|
55
|
+
if (typeof data[key] !== "number") {
|
|
56
|
+
data[key] = 0;
|
|
57
|
+
}
|
|
58
|
+
data[key] += value;
|
|
59
|
+
this._writeYAML(data);
|
|
60
|
+
}
|
|
61
|
+
|
|
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);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
async getAllData() {
|
|
72
|
+
return this._readYAML();
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
module.exports = YAMLDatabase;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@onurege3467/zerohelper",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "4.0.0",
|
|
4
4
|
"main": "index.js",
|
|
5
5
|
"scripts": {
|
|
6
6
|
"test": "node test.js"
|
|
@@ -27,8 +27,10 @@
|
|
|
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",
|
|
33
|
+
"js-yaml": "^4.1.0",
|
|
32
34
|
"jsonwebtoken": "^9.0.2",
|
|
33
35
|
"lodash": "^4.17.21",
|
|
34
36
|
"mongodb": "^6.12.0",
|
package/readme.md
CHANGED
|
@@ -16,6 +16,8 @@ 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-️)
|
|
20
|
+
- [🎋 CSV Database](#csv-database-)
|
|
19
21
|
- [🗄️ MongoDB](#mongodb-️)
|
|
20
22
|
- [🐬 MySQL](#mysql-)
|
|
21
23
|
- [📱 SQLiteDB](#sqlitedb-)
|
|
@@ -182,7 +184,40 @@ ZeroHelper provides multiple database utilities for seamless integration with va
|
|
|
182
184
|
await console.log(db.has("foo"));
|
|
183
185
|
})();
|
|
184
186
|
```
|
|
187
|
+
# YamlDatabase 🗃️
|
|
188
|
+
```js
|
|
189
|
+
(async function () {
|
|
190
|
+
const YamlDatabase = require("@onurege3467/zerohelper/database/yamldatabase");
|
|
191
|
+
const db = new YamlDatabase();
|
|
192
|
+
|
|
193
|
+
await db.set("foo", "bar");
|
|
194
|
+
await db.push("array", "x");
|
|
195
|
+
await db.delete("foo");
|
|
196
|
+
|
|
197
|
+
await db.add("number", 1);
|
|
198
|
+
await db.sub("number", 1);
|
|
199
|
+
|
|
200
|
+
await console.log(db.get("foo"));
|
|
201
|
+
await console.log(db.has("foo"));
|
|
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");
|
|
185
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
|
+
```
|
|
186
221
|
# MongoDB 🗄️
|
|
187
222
|
|
|
188
223
|
```js
|
|
@@ -410,7 +445,22 @@ const targetConfig = {
|
|
|
410
445
|
}
|
|
411
446
|
}
|
|
412
447
|
```
|
|
413
|
-
|
|
448
|
+
```json
|
|
449
|
+
{
|
|
450
|
+
"type": "yaml",
|
|
451
|
+
"options": {
|
|
452
|
+
"filePath": "data.yaml"
|
|
453
|
+
}
|
|
454
|
+
}
|
|
455
|
+
```
|
|
456
|
+
```json
|
|
457
|
+
{
|
|
458
|
+
"type": "csv",
|
|
459
|
+
"options": {
|
|
460
|
+
"filePath": "data.csv"
|
|
461
|
+
}
|
|
462
|
+
}
|
|
463
|
+
```
|
|
414
464
|
```json
|
|
415
465
|
{
|
|
416
466
|
"type": "mongodb",
|