@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 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
- * @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({ key: this.#key }, { $set: { [path]: value } }, { upsert: true });
34
- return value;
35
- }
36
-
37
- /**
38
- * Deletes the specified path.
39
- * @param {String} path
40
- * @return {Promise<void>}
41
- */
42
- async delete(path) {
43
- path = PathFormat(path);
44
-
45
- await this.#collection.updateOne({ key: this.#key }, { $unset: { [path]: "" } });
46
- }
47
-
48
- /**
49
- * Returns the value/object at the specified path.
50
- * @param {String} path
51
- * @return {any}
52
- */
53
- async get(path) {
54
- path = PathFormat(path);
55
-
56
- const data = await this.#collection.findOne({ key: this.#key }, {
57
- projection: {
58
- "result": `$${path}`
59
- }
60
- });
61
-
62
- return data ? data.result : undefined;
63
- }
64
-
65
- async getAll() {
66
-
67
- const data = await this.#collection.findOne({ key: this.#key })
68
-
69
- return data ? data.data : {};
70
- }
71
-
72
- async ping() {
73
- var run1 = Date.now()
74
- await this.set(`${run1}`,1)
75
- var set = Date.now()
76
- var run2 = Date.now()
77
- await this.get(run1)
78
- var get = Date.now()
79
- var run3 = Date.now()
80
- await this.delete(run1)
81
- var deletet = Date.now()
82
-
83
- return {total:((get+set+deletet)/3)-run1,set:set-run1,get:get-run2,delete:deletet-run3}
84
- }
85
-
86
- /**
87
- * Sorts the array/values ​​in path.
88
- * @param {String} path
89
- * @param {("DESC"|"ASC")} orderType Sorts the data in DESC (descending) or ASC (ascending).
90
- * @param {Number} limit determines how many elements will rotate. (Default: 0 this is mean of returns in array all elements)
91
- * @return {Promise<any[]>}
92
- */
93
- async sort(path, orderType, limit = 0) {
94
- path = PathFormat(path);
95
-
96
- const order = orderType == "DESC" ? -1 : 1;
97
- const pipelines = [
98
- {
99
- '$match': {
100
- 'key': this.#key
101
- }
102
- }, {
103
- '$unwind': {
104
- 'path': `$${path}`
105
- }
106
- }, {
107
- '$sort': {
108
- [`${path}`]: order
109
- }
110
- }, {
111
- '$project': {
112
- 'element': `$${path}`
113
- }
114
- }, {
115
- '$limit': limit
116
- }
117
- ];
118
-
119
- let result = (await this.#collection.aggregate(pipelines).toArray()).map(e => e.element);
120
- return result;
121
- }
122
-
123
- /**
124
- * Do mathematical addition to specified path.
125
- * @param {String} path
126
- * @param {Number} value
127
- * @return {any}
128
- */
129
- async add(path, value) {
130
- path = PathFormat(path);
131
-
132
- const data = await this.#collection.findOneAndUpdate({ key: this.#key }, { $inc: { [path]: value } }, {
133
- projection: {
134
- "result": `$${path}`
135
- },
136
- upsert: true,
137
- new: true
138
- });
139
-
140
- return data ? data.result : undefined;
141
- }
142
-
143
- /**
144
- * Do mathematical subtraction to specified path.
145
- * @param {String} path
146
- * @param {Number} value
147
- * @return {any}
148
- */
149
- async sub(path, value) {
150
- path = PathFormat(path);
151
-
152
- const data = await this.#collection.findOneAndUpdate({ key: this.#key }, { $inc: { [path]: -Math.abs(value) } }, {
153
- projection: {
154
- "result": `$${path}`
155
- },
156
- upsert: true,
157
- new: true
158
- });
159
-
160
- return data ? data.result : undefined;
161
- }
162
-
163
- /**
164
- * Check if Field exists to specified path.
165
- * @param {String} path
166
- * @return {Promise<Boolean>}
167
- */
168
- async has(path) {
169
- path = PathFormat(path);
170
-
171
- const data = await this.#collection.findOne({ key: this.#key, [path]: { $exists: true } }, {
172
- projection: {
173
- "_id": 1
174
- }
175
- });
176
- return data ? true : false;
177
- }
178
-
179
- /**
180
- * Push to value an array to specified path.
181
- * @param {String} path
182
- * @param {any} value
183
- * @return {Promise<void>}
184
- */
185
- async push(path, value) {
186
- path = PathFormat(path);
187
-
188
- await this.#collection.updateOne({ key: this.#key }, { $push: { [path]: value } }, { upsert: true });
189
- }
190
-
191
- /**
192
- * Push to multiple values an array to specified path.
193
- * @param {String} path
194
- * @param {any[]} values
195
- * @return {Promise<void>}
196
- */
197
- async pushRange(path, values) {
198
- path = PathFormat(path);
199
-
200
- await this.#collection.updateOne({ key: this.#key }, { $push: { [path]: { $each: values } } }, { upsert: true });
201
- }
202
-
203
- /**
204
- * Extract element from Array to specified path.
205
- * @param {String} path
206
- * @param {any} value
207
- * @return {Promise<void>}
208
- */
209
- async pull(path, value) {
210
- path = PathFormat(path);
211
-
212
- await this.#collection.updateOne({ key: this.#key }, { $pull: { [path]: value } }, { upsert: true });
213
- }
214
-
215
- /**
216
- * Extract all elements from Array to specified path.
217
- * @param {String} path
218
- * @param {any} value
219
- * @return {Promise<void>}
220
- */
221
- async pullAll(path, value) {
222
- path = PathFormat(path);
223
-
224
- await this.#collection.updateOne({ key: this.#key }, { $pullAll: { [path]: value } }, { upsert: true });
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 };