@onurege3467/zerohelper 2.0.3 → 2.1.1
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 +66 -35
- package/database/test.js +2 -3
- package/package.json +1 -1
- package/database/mongodb/src/structers/Database.js +0 -53
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,18 @@ class Database {
|
|
|
102
123
|
return this.runQuery(`DELETE FROM key_value_store WHERE key = ?`, [key]);
|
|
103
124
|
}
|
|
104
125
|
|
|
105
|
-
has(key) {
|
|
106
|
-
|
|
126
|
+
async has(key) {
|
|
127
|
+
await this.ready;
|
|
128
|
+
let result = undefined; // Veritabanı hazır olana kadar bekle
|
|
129
|
+
await this.get(key).then((value) => (result = value));
|
|
130
|
+
if (!result) return false;
|
|
131
|
+
else {
|
|
132
|
+
return true;
|
|
133
|
+
}
|
|
107
134
|
}
|
|
108
135
|
|
|
109
|
-
push(key, value) {
|
|
136
|
+
async push(key, value) {
|
|
137
|
+
await this.ready; // Veritabanı hazır olana kadar bekle
|
|
110
138
|
return this.get(key).then((currentValue) => {
|
|
111
139
|
if (!Array.isArray(currentValue)) {
|
|
112
140
|
currentValue = [];
|
|
@@ -116,7 +144,8 @@ class Database {
|
|
|
116
144
|
});
|
|
117
145
|
}
|
|
118
146
|
|
|
119
|
-
add(key, value) {
|
|
147
|
+
async add(key, value) {
|
|
148
|
+
await this.ready; // Veritabanı hazır olana kadar bekle
|
|
120
149
|
return this.get(key).then((currentValue) => {
|
|
121
150
|
if (typeof currentValue !== "number") {
|
|
122
151
|
currentValue = 0;
|
|
@@ -125,7 +154,8 @@ class Database {
|
|
|
125
154
|
});
|
|
126
155
|
}
|
|
127
156
|
|
|
128
|
-
sub(key, value) {
|
|
157
|
+
async sub(key, value) {
|
|
158
|
+
await this.ready; // Veritabanı hazır olana kadar bekle
|
|
129
159
|
return this.get(key).then((currentValue) => {
|
|
130
160
|
if (typeof currentValue !== "number") {
|
|
131
161
|
currentValue = 0;
|
|
@@ -159,13 +189,15 @@ class Database {
|
|
|
159
189
|
}
|
|
160
190
|
|
|
161
191
|
close() {
|
|
162
|
-
this.db
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
192
|
+
if (this.db) {
|
|
193
|
+
this.db.close((err) => {
|
|
194
|
+
if (err) {
|
|
195
|
+
console.error("Error closing database:", err.message);
|
|
196
|
+
} else {
|
|
197
|
+
console.log("Database connection closed.");
|
|
198
|
+
}
|
|
199
|
+
});
|
|
200
|
+
}
|
|
169
201
|
}
|
|
170
202
|
}
|
|
171
203
|
|
|
@@ -173,7 +205,6 @@ module.exports = Database;
|
|
|
173
205
|
|
|
174
206
|
// Example usage:
|
|
175
207
|
// const db = new Database();
|
|
176
|
-
// db.initialize();
|
|
177
208
|
// db.set('foo.bar.baz', 'value')
|
|
178
209
|
// .then(() => db.get('foo.bar'))
|
|
179
210
|
// .then((value) => console.log('Value:', value)) // Output: { baz: 'value' }
|
package/database/test.js
CHANGED
|
@@ -43,9 +43,8 @@ const runJsonDatabase = async () => {
|
|
|
43
43
|
const runSQLite = async () => {
|
|
44
44
|
const { database } = require("../index");
|
|
45
45
|
var db = new database.SQLiteDatabase();
|
|
46
|
-
await db.
|
|
47
|
-
await db.
|
|
48
|
-
console.log(await db.get("foo.test"));
|
|
46
|
+
await db.set("foo.test2", { Date: Date.now(), name: "Onur" });
|
|
47
|
+
console.log(await db.has("foo.test2.a"));
|
|
49
48
|
};
|
|
50
49
|
|
|
51
50
|
runSQLite();
|
package/package.json
CHANGED
|
@@ -1,53 +0,0 @@
|
|
|
1
|
-
const mongodb = require("mongodb");
|
|
2
|
-
const { Collection } = require("./Collection.js");
|
|
3
|
-
|
|
4
|
-
class Database {
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* It has Db feature in MongoDB. Most operations are done with this area.
|
|
8
|
-
* @type {mongodb.Db}
|
|
9
|
-
*/
|
|
10
|
-
Db;
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* It carries the Client connected to MongoDB.
|
|
14
|
-
* @type {mongodb.MongoClient}
|
|
15
|
-
*/
|
|
16
|
-
Client;
|
|
17
|
-
|
|
18
|
-
/**
|
|
19
|
-
*
|
|
20
|
-
* @param {mongodb.MongoClient} client
|
|
21
|
-
* @param {mongodb.Db} databaseName
|
|
22
|
-
*/
|
|
23
|
-
constructor(client, databaseName) {
|
|
24
|
-
this.Client = client;
|
|
25
|
-
this.Db = this.Client.db(databaseName);
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
/**
|
|
29
|
-
* If collection does not exists, creates collection then returns the collection. Otherwise just returns collection.
|
|
30
|
-
* @param {String} collectionName
|
|
31
|
-
* @return {Collection}
|
|
32
|
-
*/
|
|
33
|
-
collection(collectionName) {
|
|
34
|
-
const dbCollection = this.Db.collection(collectionName);
|
|
35
|
-
dbCollection.createIndex({ key: 1 });
|
|
36
|
-
const collection = new Collection(dbCollection);
|
|
37
|
-
|
|
38
|
-
return collection;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
/**
|
|
42
|
-
* Drops the Database Collection.
|
|
43
|
-
* @param {String} collectionName
|
|
44
|
-
* @return {Promise<void>}
|
|
45
|
-
*/
|
|
46
|
-
async dropCollection(collectionName) {
|
|
47
|
-
const flag = await this.Db.dropCollection(collectionName);
|
|
48
|
-
|
|
49
|
-
return flag;
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
module.exports = { Database };
|