@onurege3467/zerohelper 3.0.0 → 3.2.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 +6 -0
- package/database/jsondatabase/index.js +8 -3
- package/database/migrate/index.js +63 -0
- package/database/mongodb/src/structers/Data.js +275 -221
- package/database/mysql/structures/classes/MySQL.js +38 -34
- package/database/mysql/structures/methods/add.js +16 -18
- package/database/mysql/structures/methods/all.js +22 -21
- package/database/mysql/structures/methods/auto_increment.js +13 -10
- package/database/mysql/structures/methods/base_get.js +11 -9
- package/database/mysql/structures/methods/base_set.js +18 -10
- package/database/mysql/structures/methods/clear.js +13 -12
- package/database/mysql/structures/methods/delete.js +28 -21
- package/database/mysql/structures/methods/exists.js +12 -10
- package/database/mysql/structures/methods/get.js +37 -37
- package/database/mysql/structures/methods/getAllData.js +35 -0
- package/database/mysql/structures/methods/has.js +39 -37
- package/database/mysql/structures/methods/includes.js +14 -10
- package/database/mysql/structures/methods/pull.js +20 -16
- package/database/mysql/structures/methods/push.js +20 -17
- package/database/mysql/structures/methods/set.js +58 -46
- package/database/mysql/structures/methods/stats.js +10 -7
- package/database/mysql/structures/methods/sub.js +16 -18
- package/database/postgresql/index.js +45 -13
- package/database/redis/index.js +24 -1
- package/database/sqldb/index.js +28 -0
- package/functions/index.js +141 -0
- package/package.json +3 -2
- package/readme.md +148 -8
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 MigrateDatabase = require("./migrate/index");
|
|
11
12
|
|
|
12
13
|
module.exports = {
|
|
13
14
|
/**
|
|
@@ -45,4 +46,9 @@ module.exports = {
|
|
|
45
46
|
* @type {PostgreSQL}
|
|
46
47
|
*/
|
|
47
48
|
PostgreSQL,
|
|
49
|
+
/**
|
|
50
|
+
* Migration utility for databases.
|
|
51
|
+
* @type {MigrateDatabase}
|
|
52
|
+
*/
|
|
53
|
+
MigrateDatabase,
|
|
48
54
|
};
|
|
@@ -33,6 +33,11 @@ class database {
|
|
|
33
33
|
this.set(path, data);
|
|
34
34
|
return data;
|
|
35
35
|
}
|
|
36
|
+
getAllData() {
|
|
37
|
+
let data = this.read();
|
|
38
|
+
if (!data) data = {};
|
|
39
|
+
return data;
|
|
40
|
+
}
|
|
36
41
|
get(path) {
|
|
37
42
|
let data = this.read(),
|
|
38
43
|
result = undefined;
|
|
@@ -72,9 +77,9 @@ class database {
|
|
|
72
77
|
push(path, value) {
|
|
73
78
|
let data = this.read();
|
|
74
79
|
if (!data) data = {};
|
|
75
|
-
if (_get(path,data) && Array.isArray(_get(path,data))) {
|
|
76
|
-
_get(path,data).push(value);
|
|
77
|
-
} else if (!_get(path,data)) {
|
|
80
|
+
if (_get(path, data) && Array.isArray(_get(path, data))) {
|
|
81
|
+
_get(path, data).push(value);
|
|
82
|
+
} else if (!_get(path, data)) {
|
|
78
83
|
_set(path, [value], data);
|
|
79
84
|
}
|
|
80
85
|
fs.truncateSync(this.FilePath);
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
const JsonDatabase = require("../jsondatabase/index");
|
|
2
|
+
const MongoDB = require("../mongodb/index");
|
|
3
|
+
const MySQLDatabase = require("../mysql/index");
|
|
4
|
+
const SQLiteDatabase = require("../sqldb/index");
|
|
5
|
+
const RedisDatabase = require("../redis/index");
|
|
6
|
+
const PostgreSQL = require("../postgresql/index");
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Migrates data from one database to another.
|
|
10
|
+
* @param {Object} source - Source database configuration.
|
|
11
|
+
* @param {Object} target - Target database configuration.
|
|
12
|
+
*/
|
|
13
|
+
async function migrateData(source, target) {
|
|
14
|
+
const sourceDb = await initializeDatabase(source);
|
|
15
|
+
const targetDb = await initializeDatabase(target);
|
|
16
|
+
|
|
17
|
+
const allData = await sourceDb.getAllData();
|
|
18
|
+
console.log(allData);
|
|
19
|
+
|
|
20
|
+
for (const [key, value] of Object.entries(allData)) {
|
|
21
|
+
await targetDb.set(key, value);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
console.log(`Migration completed from ${source.type} to ${target.type}`);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Initializes a database instance based on the configuration.
|
|
29
|
+
* @param {Object} config - Database configuration.
|
|
30
|
+
* @returns {Object} - Database instance.
|
|
31
|
+
*/
|
|
32
|
+
async function initializeDatabase(config) {
|
|
33
|
+
switch (config.type) {
|
|
34
|
+
case "json":
|
|
35
|
+
return new JsonDatabase(config.options.filePath);
|
|
36
|
+
case "mongodb":
|
|
37
|
+
const mongoClient = await MongoDB.createData(
|
|
38
|
+
config.options.database,
|
|
39
|
+
config.options.collection,
|
|
40
|
+
config.options.data,
|
|
41
|
+
undefined,
|
|
42
|
+
config.options.url
|
|
43
|
+
);
|
|
44
|
+
return mongoClient;
|
|
45
|
+
case "mysql":
|
|
46
|
+
const mysqlDb = new MySQLDatabase();
|
|
47
|
+
await mysqlDb.connect(config.options);
|
|
48
|
+
return mysqlDb;
|
|
49
|
+
case "sqlite":
|
|
50
|
+
return new SQLiteDatabase(config.options.filePath);
|
|
51
|
+
case "redis":
|
|
52
|
+
const redisDb = new RedisDatabase(config.options);
|
|
53
|
+
await redisDb.connect();
|
|
54
|
+
return redisDb;
|
|
55
|
+
case "postgresql":
|
|
56
|
+
return new PostgreSQL(config.options);
|
|
57
|
+
default:
|
|
58
|
+
throw new Error(`Unsupported database type: ${config.type}`);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
// Export the migrateData function
|
|
63
|
+
module.exports = migrateData;
|
|
@@ -2,227 +2,281 @@ const mongodb = require("mongodb");
|
|
|
2
2
|
const PathFormat = require("../tools/FormatTool.js");
|
|
3
3
|
|
|
4
4
|
class Data {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
5
|
+
/**
|
|
6
|
+
* @type {mongodb.Collection}
|
|
7
|
+
*/
|
|
8
|
+
#collection;
|
|
9
|
+
/**
|
|
10
|
+
* @type {String}
|
|
11
|
+
*/
|
|
12
|
+
#key;
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
*
|
|
16
|
+
* @param {String} key
|
|
17
|
+
* @param {mongodb.Collection} collection
|
|
18
|
+
*/
|
|
19
|
+
constructor(key, collection) {
|
|
20
|
+
this.#key = key;
|
|
21
|
+
this.#collection = collection;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Assign to the specified path.
|
|
26
|
+
* @param {String} path
|
|
27
|
+
* @param {any} value Value to be assigned.
|
|
28
|
+
* @return {any}
|
|
29
|
+
*/
|
|
30
|
+
async set(path, value) {
|
|
31
|
+
path = PathFormat(path);
|
|
32
|
+
|
|
33
|
+
await this.#collection.updateOne(
|
|
34
|
+
{ key: this.#key },
|
|
35
|
+
{ $set: { [path]: value } },
|
|
36
|
+
{ upsert: true }
|
|
37
|
+
);
|
|
38
|
+
return value;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Deletes the specified path.
|
|
43
|
+
* @param {String} path
|
|
44
|
+
* @return {Promise<void>}
|
|
45
|
+
*/
|
|
46
|
+
async delete(path) {
|
|
47
|
+
path = PathFormat(path);
|
|
48
|
+
|
|
49
|
+
await this.#collection.updateOne(
|
|
50
|
+
{ key: this.#key },
|
|
51
|
+
{ $unset: { [path]: "" } }
|
|
52
|
+
);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Returns the value/object at the specified path.
|
|
57
|
+
* @param {String} path
|
|
58
|
+
* @return {any}
|
|
59
|
+
*/
|
|
60
|
+
async get(path) {
|
|
61
|
+
path = PathFormat(path);
|
|
62
|
+
|
|
63
|
+
const data = await this.#collection.findOne(
|
|
64
|
+
{ key: this.#key },
|
|
65
|
+
{
|
|
66
|
+
projection: {
|
|
67
|
+
result: `$${path}`,
|
|
68
|
+
},
|
|
69
|
+
}
|
|
70
|
+
);
|
|
71
|
+
|
|
72
|
+
return data ? data.result : undefined;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
async getAll() {
|
|
76
|
+
const data = await this.#collection.findOne({ key: this.#key });
|
|
77
|
+
|
|
78
|
+
return data ? data.data : {};
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
async getAllData() {
|
|
82
|
+
this.getAll().then((data) => {
|
|
83
|
+
if (!data) data = {};
|
|
84
|
+
return data;
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
async ping() {
|
|
89
|
+
var run1 = Date.now();
|
|
90
|
+
await this.set(`${run1}`, 1);
|
|
91
|
+
var set = Date.now();
|
|
92
|
+
var run2 = Date.now();
|
|
93
|
+
await this.get(run1);
|
|
94
|
+
var get = Date.now();
|
|
95
|
+
var run3 = Date.now();
|
|
96
|
+
await this.delete(run1);
|
|
97
|
+
var deletet = Date.now();
|
|
98
|
+
|
|
99
|
+
return {
|
|
100
|
+
total: (get + set + deletet) / 3 - run1,
|
|
101
|
+
set: set - run1,
|
|
102
|
+
get: get - run2,
|
|
103
|
+
delete: deletet - run3,
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Sorts the array/values in path.
|
|
109
|
+
* @param {String} path
|
|
110
|
+
* @param {("DESC"|"ASC")} orderType Sorts the data in DESC (descending) or ASC (ascending).
|
|
111
|
+
* @param {Number} limit determines how many elements will rotate. (Default: 0 this is mean of returns in array all elements)
|
|
112
|
+
* @return {Promise<any[]>}
|
|
113
|
+
*/
|
|
114
|
+
async sort(path, orderType, limit = 0) {
|
|
115
|
+
path = PathFormat(path);
|
|
116
|
+
|
|
117
|
+
const order = orderType == "DESC" ? -1 : 1;
|
|
118
|
+
const pipelines = [
|
|
119
|
+
{
|
|
120
|
+
$match: {
|
|
121
|
+
key: this.#key,
|
|
122
|
+
},
|
|
123
|
+
},
|
|
124
|
+
{
|
|
125
|
+
$unwind: {
|
|
126
|
+
path: `$${path}`,
|
|
127
|
+
},
|
|
128
|
+
},
|
|
129
|
+
{
|
|
130
|
+
$sort: {
|
|
131
|
+
[`${path}`]: order,
|
|
132
|
+
},
|
|
133
|
+
},
|
|
134
|
+
{
|
|
135
|
+
$project: {
|
|
136
|
+
element: `$${path}`,
|
|
137
|
+
},
|
|
138
|
+
},
|
|
139
|
+
{
|
|
140
|
+
$limit: limit,
|
|
141
|
+
},
|
|
142
|
+
];
|
|
143
|
+
|
|
144
|
+
let result = (await this.#collection.aggregate(pipelines).toArray()).map(
|
|
145
|
+
(e) => e.element
|
|
146
|
+
);
|
|
147
|
+
return result;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* Do mathematical addition to specified path.
|
|
152
|
+
* @param {String} path
|
|
153
|
+
* @param {Number} value
|
|
154
|
+
* @return {any}
|
|
155
|
+
*/
|
|
156
|
+
async add(path, value) {
|
|
157
|
+
path = PathFormat(path);
|
|
158
|
+
|
|
159
|
+
const data = await this.#collection.findOneAndUpdate(
|
|
160
|
+
{ key: this.#key },
|
|
161
|
+
{ $inc: { [path]: value } },
|
|
162
|
+
{
|
|
163
|
+
projection: {
|
|
164
|
+
result: `$${path}`,
|
|
165
|
+
},
|
|
166
|
+
upsert: true,
|
|
167
|
+
new: true,
|
|
168
|
+
}
|
|
169
|
+
);
|
|
170
|
+
|
|
171
|
+
return data ? data.result : undefined;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* Do mathematical subtraction to specified path.
|
|
176
|
+
* @param {String} path
|
|
177
|
+
* @param {Number} value
|
|
178
|
+
* @return {any}
|
|
179
|
+
*/
|
|
180
|
+
async sub(path, value) {
|
|
181
|
+
path = PathFormat(path);
|
|
182
|
+
|
|
183
|
+
const data = await this.#collection.findOneAndUpdate(
|
|
184
|
+
{ key: this.#key },
|
|
185
|
+
{ $inc: { [path]: -Math.abs(value) } },
|
|
186
|
+
{
|
|
187
|
+
projection: {
|
|
188
|
+
result: `$${path}`,
|
|
189
|
+
},
|
|
190
|
+
upsert: true,
|
|
191
|
+
new: true,
|
|
192
|
+
}
|
|
193
|
+
);
|
|
194
|
+
|
|
195
|
+
return data ? data.result : undefined;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/**
|
|
199
|
+
* Check if Field exists to specified path.
|
|
200
|
+
* @param {String} path
|
|
201
|
+
* @return {Promise<Boolean>}
|
|
202
|
+
*/
|
|
203
|
+
async has(path) {
|
|
204
|
+
path = PathFormat(path);
|
|
205
|
+
|
|
206
|
+
const data = await this.#collection.findOne(
|
|
207
|
+
{ key: this.#key, [path]: { $exists: true } },
|
|
208
|
+
{
|
|
209
|
+
projection: {
|
|
210
|
+
_id: 1,
|
|
211
|
+
},
|
|
212
|
+
}
|
|
213
|
+
);
|
|
214
|
+
return data ? true : false;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/**
|
|
218
|
+
* Push to value an array to specified path.
|
|
219
|
+
* @param {String} path
|
|
220
|
+
* @param {any} value
|
|
221
|
+
* @return {Promise<void>}
|
|
222
|
+
*/
|
|
223
|
+
async push(path, value) {
|
|
224
|
+
path = PathFormat(path);
|
|
225
|
+
|
|
226
|
+
await this.#collection.updateOne(
|
|
227
|
+
{ key: this.#key },
|
|
228
|
+
{ $push: { [path]: value } },
|
|
229
|
+
{ upsert: true }
|
|
230
|
+
);
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
/**
|
|
234
|
+
* Push to multiple values an array to specified path.
|
|
235
|
+
* @param {String} path
|
|
236
|
+
* @param {any[]} values
|
|
237
|
+
* @return {Promise<void>}
|
|
238
|
+
*/
|
|
239
|
+
async pushRange(path, values) {
|
|
240
|
+
path = PathFormat(path);
|
|
241
|
+
|
|
242
|
+
await this.#collection.updateOne(
|
|
243
|
+
{ key: this.#key },
|
|
244
|
+
{ $push: { [path]: { $each: values } } },
|
|
245
|
+
{ upsert: true }
|
|
246
|
+
);
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
/**
|
|
250
|
+
* Extract element from Array to specified path.
|
|
251
|
+
* @param {String} path
|
|
252
|
+
* @param {any} value
|
|
253
|
+
* @return {Promise<void>}
|
|
254
|
+
*/
|
|
255
|
+
async pull(path, value) {
|
|
256
|
+
path = PathFormat(path);
|
|
257
|
+
|
|
258
|
+
await this.#collection.updateOne(
|
|
259
|
+
{ key: this.#key },
|
|
260
|
+
{ $pull: { [path]: value } },
|
|
261
|
+
{ upsert: true }
|
|
262
|
+
);
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
/**
|
|
266
|
+
* Extract all elements from Array to specified path.
|
|
267
|
+
* @param {String} path
|
|
268
|
+
* @param {any} value
|
|
269
|
+
* @return {Promise<void>}
|
|
270
|
+
*/
|
|
271
|
+
async pullAll(path, value) {
|
|
272
|
+
path = PathFormat(path);
|
|
273
|
+
|
|
274
|
+
await this.#collection.updateOne(
|
|
275
|
+
{ key: this.#key },
|
|
276
|
+
{ $pullAll: { [path]: value } },
|
|
277
|
+
{ upsert: true }
|
|
278
|
+
);
|
|
279
|
+
}
|
|
226
280
|
}
|
|
227
281
|
|
|
228
282
|
module.exports = { Data };
|