@onurege3467/zerohelper 2.0.3 → 2.1.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 +23 -0
- package/database/sqldb/index.js +60 -34
- package/database/test.js +0 -1
- package/package.json +1 -1
package/database/index.js
CHANGED
|
@@ -1,11 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module Database
|
|
3
|
+
*/
|
|
4
|
+
|
|
1
5
|
var JsonDatabase = require("./jsondatabase/index");
|
|
2
6
|
var MongoDB = require("./mongodb/index");
|
|
3
7
|
var MySQLDatabase = require("./mysql/index");
|
|
4
8
|
var SQLiteDatabase = require("./sqldb/index");
|
|
5
9
|
|
|
6
10
|
module.exports = {
|
|
11
|
+
/**
|
|
12
|
+
* JSON-based database.
|
|
13
|
+
* @type {JsonDatabase}
|
|
14
|
+
*/
|
|
7
15
|
JsonDatabase,
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* MongoDB-based database.
|
|
19
|
+
* @type {MongoDB}
|
|
20
|
+
*/
|
|
8
21
|
MongoDB,
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* MySQL-based database.
|
|
25
|
+
* @type {MySQLDatabase}
|
|
26
|
+
*/
|
|
9
27
|
MySQLDatabase,
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* SQLite-based database.
|
|
31
|
+
* @type {SQLiteDatabase}
|
|
32
|
+
*/
|
|
10
33
|
SQLiteDatabase,
|
|
11
34
|
};
|
package/database/sqldb/index.js
CHANGED
|
@@ -6,30 +6,45 @@ class Database {
|
|
|
6
6
|
constructor(dbFilePath) {
|
|
7
7
|
this.dbFilePath = dbFilePath || path.join(__dirname, "database.sqlite");
|
|
8
8
|
this.db = null;
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
initialize() {
|
|
12
|
-
if (!fs.existsSync(this.dbFilePath)) {
|
|
13
|
-
console.log("Creating database file...");
|
|
14
|
-
fs.writeFileSync(this.dbFilePath, "");
|
|
15
|
-
}
|
|
16
9
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
this.runQuery(
|
|
23
|
-
`CREATE TABLE IF NOT EXISTS key_value_store (key TEXT PRIMARY KEY, value TEXT)`
|
|
24
|
-
)
|
|
25
|
-
.then(() => {
|
|
26
|
-
console.log("Table initialized");
|
|
27
|
-
})
|
|
28
|
-
.catch((err) => console.error(err));
|
|
10
|
+
// Veritabanı hazır olana kadar işlemleri bekletmek için bir Promise
|
|
11
|
+
this.ready = new Promise((resolve, reject) => {
|
|
12
|
+
if (!fs.existsSync(this.dbFilePath)) {
|
|
13
|
+
console.log("Database file does not exist. Creating the file...");
|
|
14
|
+
fs.writeFileSync(this.dbFilePath, ""); // Boş bir dosya oluştur
|
|
29
15
|
}
|
|
16
|
+
|
|
17
|
+
// Veritabanını başlat
|
|
18
|
+
this.db = new sqlite3.Database(this.dbFilePath, (err) => {
|
|
19
|
+
if (err) {
|
|
20
|
+
console.error("Error opening database:", err.message);
|
|
21
|
+
reject(err);
|
|
22
|
+
} else {
|
|
23
|
+
console.log("Connected to the SQLite database.");
|
|
24
|
+
this.runQuery(
|
|
25
|
+
`CREATE TABLE IF NOT EXISTS key_value_store (key TEXT PRIMARY KEY, value TEXT)`
|
|
26
|
+
)
|
|
27
|
+
.then(() => {
|
|
28
|
+
console.log("Table initialized");
|
|
29
|
+
resolve(); // Veritabanı hazır
|
|
30
|
+
})
|
|
31
|
+
.catch((err) => {
|
|
32
|
+
console.error(err);
|
|
33
|
+
reject(err);
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
});
|
|
30
37
|
});
|
|
31
38
|
}
|
|
32
39
|
|
|
40
|
+
_ensureDatabaseInitialized() {
|
|
41
|
+
if (!this.db) {
|
|
42
|
+
throw new Error(
|
|
43
|
+
"Database is not initialized. Ensure the database file exists."
|
|
44
|
+
);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
33
48
|
_parseNestedKey(key) {
|
|
34
49
|
return key.includes(".") ? key.split(".") : [key];
|
|
35
50
|
}
|
|
@@ -50,7 +65,9 @@ class Database {
|
|
|
50
65
|
return { ...target, ...source };
|
|
51
66
|
}
|
|
52
67
|
|
|
53
|
-
set(key, value) {
|
|
68
|
+
async set(key, value) {
|
|
69
|
+
await this.ready; // Veritabanı hazır olana kadar bekle
|
|
70
|
+
this._ensureDatabaseInitialized();
|
|
54
71
|
const keys = this._parseNestedKey(key);
|
|
55
72
|
if (keys.length > 1) {
|
|
56
73
|
return this.get(keys[0]).then((currentValue) => {
|
|
@@ -69,7 +86,9 @@ class Database {
|
|
|
69
86
|
);
|
|
70
87
|
}
|
|
71
88
|
|
|
72
|
-
get(key) {
|
|
89
|
+
async get(key) {
|
|
90
|
+
await this.ready; // Veritabanı hazır olana kadar bekle
|
|
91
|
+
this._ensureDatabaseInitialized();
|
|
73
92
|
const keys = this._parseNestedKey(key);
|
|
74
93
|
return this.getQuery(`SELECT value FROM key_value_store WHERE key = ?`, [
|
|
75
94
|
keys[0],
|
|
@@ -82,7 +101,9 @@ class Database {
|
|
|
82
101
|
});
|
|
83
102
|
}
|
|
84
103
|
|
|
85
|
-
delete(key) {
|
|
104
|
+
async delete(key) {
|
|
105
|
+
await this.ready; // Veritabanı hazır olana kadar bekle
|
|
106
|
+
this._ensureDatabaseInitialized();
|
|
86
107
|
const keys = this._parseNestedKey(key);
|
|
87
108
|
if (keys.length > 1) {
|
|
88
109
|
return this.get(keys[0]).then((currentValue) => {
|
|
@@ -102,11 +123,13 @@ class Database {
|
|
|
102
123
|
return this.runQuery(`DELETE FROM key_value_store WHERE key = ?`, [key]);
|
|
103
124
|
}
|
|
104
125
|
|
|
105
|
-
has(key) {
|
|
126
|
+
async has(key) {
|
|
127
|
+
await this.ready; // Veritabanı hazır olana kadar bekle
|
|
106
128
|
return this.get(key).then((value) => value !== null);
|
|
107
129
|
}
|
|
108
130
|
|
|
109
|
-
push(key, value) {
|
|
131
|
+
async push(key, value) {
|
|
132
|
+
await this.ready; // Veritabanı hazır olana kadar bekle
|
|
110
133
|
return this.get(key).then((currentValue) => {
|
|
111
134
|
if (!Array.isArray(currentValue)) {
|
|
112
135
|
currentValue = [];
|
|
@@ -116,7 +139,8 @@ class Database {
|
|
|
116
139
|
});
|
|
117
140
|
}
|
|
118
141
|
|
|
119
|
-
add(key, value) {
|
|
142
|
+
async add(key, value) {
|
|
143
|
+
await this.ready; // Veritabanı hazır olana kadar bekle
|
|
120
144
|
return this.get(key).then((currentValue) => {
|
|
121
145
|
if (typeof currentValue !== "number") {
|
|
122
146
|
currentValue = 0;
|
|
@@ -125,7 +149,8 @@ class Database {
|
|
|
125
149
|
});
|
|
126
150
|
}
|
|
127
151
|
|
|
128
|
-
sub(key, value) {
|
|
152
|
+
async sub(key, value) {
|
|
153
|
+
await this.ready; // Veritabanı hazır olana kadar bekle
|
|
129
154
|
return this.get(key).then((currentValue) => {
|
|
130
155
|
if (typeof currentValue !== "number") {
|
|
131
156
|
currentValue = 0;
|
|
@@ -159,13 +184,15 @@ class Database {
|
|
|
159
184
|
}
|
|
160
185
|
|
|
161
186
|
close() {
|
|
162
|
-
this.db
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
187
|
+
if (this.db) {
|
|
188
|
+
this.db.close((err) => {
|
|
189
|
+
if (err) {
|
|
190
|
+
console.error("Error closing database:", err.message);
|
|
191
|
+
} else {
|
|
192
|
+
console.log("Database connection closed.");
|
|
193
|
+
}
|
|
194
|
+
});
|
|
195
|
+
}
|
|
169
196
|
}
|
|
170
197
|
}
|
|
171
198
|
|
|
@@ -173,7 +200,6 @@ module.exports = Database;
|
|
|
173
200
|
|
|
174
201
|
// Example usage:
|
|
175
202
|
// const db = new Database();
|
|
176
|
-
// db.initialize();
|
|
177
203
|
// db.set('foo.bar.baz', 'value')
|
|
178
204
|
// .then(() => db.get('foo.bar'))
|
|
179
205
|
// .then((value) => console.log('Value:', value)) // Output: { baz: 'value' }
|
package/database/test.js
CHANGED
|
@@ -43,7 +43,6 @@ const runJsonDatabase = async () => {
|
|
|
43
43
|
const runSQLite = async () => {
|
|
44
44
|
const { database } = require("../index");
|
|
45
45
|
var db = new database.SQLiteDatabase();
|
|
46
|
-
await db.initialize();
|
|
47
46
|
await db.set("foo.test", "bar");
|
|
48
47
|
console.log(await db.get("foo.test"));
|
|
49
48
|
};
|