@onurege3467/zerohelper 1.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.
Files changed (44) hide show
  1. package/database/index.js +9 -0
  2. package/database/jsondatabase/index.js +127 -0
  3. package/database/mongodb/index.js +49 -0
  4. package/database/mongodb/src/client/Client.js +37 -0
  5. package/database/mongodb/src/structers/Collection.js +136 -0
  6. package/database/mongodb/src/structers/Data.js +228 -0
  7. package/database/mongodb/src/structers/Database.js +53 -0
  8. package/database/mongodb/src/tools/FormatTool.js +5 -0
  9. package/database/mysql/examples/example.js +301 -0
  10. package/database/mysql/index.js +1 -0
  11. package/database/mysql/structures/classes/MySQL.js +37 -0
  12. package/database/mysql/structures/errors/strings.js +23 -0
  13. package/database/mysql/structures/methods/add.js +21 -0
  14. package/database/mysql/structures/methods/all.js +24 -0
  15. package/database/mysql/structures/methods/auto_increment.js +13 -0
  16. package/database/mysql/structures/methods/base_get.js +12 -0
  17. package/database/mysql/structures/methods/base_set.js +13 -0
  18. package/database/mysql/structures/methods/clear.js +15 -0
  19. package/database/mysql/structures/methods/connect.js +15 -0
  20. package/database/mysql/structures/methods/create.js +11 -0
  21. package/database/mysql/structures/methods/create_db.js +10 -0
  22. package/database/mysql/structures/methods/delete.js +24 -0
  23. package/database/mysql/structures/methods/drop.js +13 -0
  24. package/database/mysql/structures/methods/end.js +7 -0
  25. package/database/mysql/structures/methods/exists.js +13 -0
  26. package/database/mysql/structures/methods/get.js +40 -0
  27. package/database/mysql/structures/methods/has.js +40 -0
  28. package/database/mysql/structures/methods/includes.js +13 -0
  29. package/database/mysql/structures/methods/ping.js +11 -0
  30. package/database/mysql/structures/methods/process.js +7 -0
  31. package/database/mysql/structures/methods/pull.js +19 -0
  32. package/database/mysql/structures/methods/push.js +20 -0
  33. package/database/mysql/structures/methods/query.js +9 -0
  34. package/database/mysql/structures/methods/rename.js +16 -0
  35. package/database/mysql/structures/methods/set.js +48 -0
  36. package/database/mysql/structures/methods/stats.js +10 -0
  37. package/database/mysql/structures/methods/sub.js +21 -0
  38. package/database/mysql/structures/methods/tables.js +8 -0
  39. package/database/mysql/structures/methods/variables.js +20 -0
  40. package/database/test.js +37 -0
  41. package/functions/functions.js +47 -0
  42. package/index.js +7 -0
  43. package/package.json +29 -0
  44. package/readme.md +91 -0
@@ -0,0 +1,9 @@
1
+ var jsonDatabasse = require('./jsondatabase/index')
2
+ var MongoDB = require('./mongodb/index')
3
+ var MySQL = require('./mysql/index')
4
+
5
+ module.exports = {
6
+ JsonDatabase: jsonDatabasse,
7
+ MongoDB,
8
+ MySQLDatabase:MySQL
9
+ }
@@ -0,0 +1,127 @@
1
+ const fs = require("fs");
2
+ const path = require("path");
3
+ class database {
4
+ constructor(name = "database", loc2) {
5
+ var location = "database";
6
+ var filePath = `databases/${name}.json`;
7
+ if (name.endsWith(".json")) {
8
+ filePath = `databases/${name}`;
9
+ }
10
+ if (location == "database" && !fs.existsSync(`databases`)) {
11
+ fs.mkdirSync(`databases`, { recursive: true });
12
+ } else if (!fs.existsSync(`${location}`)) {
13
+ fs.mkdirSync(`databases`, { recursive: true });
14
+ }
15
+ if (loc2) {
16
+ if (!fs.existsSync(`${loc2}`)) {
17
+ fs.mkdirSync(`databases/${loc2}`, { recursive: true });
18
+ }
19
+ filePath = `databases/${loc2}/${name}.json`;
20
+ if (name.endsWith(".json")) {
21
+ filePath = `databases/${loc2}/${name}`;
22
+ }
23
+ }
24
+
25
+ if (!fs.existsSync(filePath)) fs.closeSync(fs.openSync(filePath, "w"));
26
+ this.FilePath = filePath;
27
+ this.Location = location;
28
+ }
29
+ add(path, value) {
30
+ let data = this.get(path);
31
+ if (typeof data == "number") data += Number(value);
32
+ else data = Number(value);
33
+ this.set(path, data);
34
+ return data;
35
+ }
36
+ get(path) {
37
+ let data = this.read(),
38
+ result = undefined;
39
+ if (!data) data = {};
40
+ result = _get(path, data);
41
+ return result ? result : undefined;
42
+ }
43
+ has(path) {
44
+ let data = this.read(),
45
+ result = undefined;
46
+ result = _get(path, data);
47
+ if (!result) return false;
48
+ else {
49
+ return true;
50
+ }
51
+ }
52
+ set(path, value) {
53
+ let data = this.read();
54
+ if (!data) data = {};
55
+ data = _set(path, value, data);
56
+ fs.truncateSync(this.FilePath);
57
+ fs.writeFileSync(this.FilePath, JSON.stringify(data, null, 4), {
58
+ encoding: "utf-8",
59
+ });
60
+ return data;
61
+ }
62
+ delete(path) {
63
+ let data = this.read();
64
+ if (!data) data = {};
65
+ data = _set(path, undefined, data);
66
+ fs.truncateSync(this.FilePath);
67
+ fs.writeFileSync(this.FilePath, JSON.stringify(data, null, 4), {
68
+ encoding: "utf-8",
69
+ });
70
+ return data;
71
+ }
72
+ push(path, value) {
73
+ let data = this.read();
74
+ 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)) {
78
+ _set(path, [value], data);
79
+ }
80
+ fs.truncateSync(this.FilePath);
81
+ fs.writeFileSync(this.FilePath, JSON.stringify(data, null, 4), {
82
+ encoding: "utf-8",
83
+ });
84
+ return data;
85
+ }
86
+ sub(path, value) {
87
+ let data = this.get(path);
88
+ if (typeof data == "number") data -= Number(value);
89
+ else data = Number(value);
90
+ this.set(path, data);
91
+ return data;
92
+ }
93
+ read() {
94
+ let data = fs.readFileSync(this.FilePath, { encoding: "utf-8" });
95
+ if (!data || (data && data == null)) return {};
96
+ let obj = JSON.parse(data);
97
+
98
+ return obj;
99
+ }
100
+ }
101
+
102
+ function _set(path, value, obj = undefined) {
103
+ if (obj == undefined) return undefined;
104
+ let locations = path.split("."),
105
+ output = {};
106
+ output = obj;
107
+ let ref = output;
108
+ for (let index = 0; index < locations.length - 1; index++) {
109
+ if (!ref[locations[index]]) ref = ref[locations[index]] = {};
110
+ else ref = ref[locations[index]];
111
+ }
112
+ ref[locations[locations.length - 1]] = value;
113
+ return output;
114
+ }
115
+
116
+ function _get(path, obj = {}) {
117
+ let locations = path.split("."),
118
+ ref = obj;
119
+ for (let index = 0; index < locations.length - 1; index++) {
120
+ ref = ref[locations[index]] ? ref[locations[index]] : undefined;
121
+ if (!ref) return undefined;
122
+ }
123
+ let output = ref[locations[locations.length - 1]];
124
+ return output;
125
+ }
126
+
127
+ module.exports = database;
@@ -0,0 +1,49 @@
1
+ const { MongoClient } = require('mongodb');
2
+
3
+ const { Client } = require('./src/client/Client.js');
4
+ const { Collection } = require("./src/structers/Collection.js");
5
+ const { Data } = require("./src/structers/Data.js");
6
+ const { Database } = require("./src/structers/Database.js");
7
+
8
+
9
+ require('dotenv').config();
10
+
11
+ class MongoDB {
12
+ /**
13
+ * Creates a new client and connects to MongoDB. (Connects to socket)
14
+ * @param {String} url
15
+ * @param {MongoClientOptions} options
16
+ * @return {Promise<Client>}
17
+ */
18
+ static connect = async function (url, options = undefined) {
19
+ const mongoClient = new MongoClient(url, options);
20
+ await mongoClient.connect();
21
+ return new Client(mongoClient);
22
+ }
23
+ }
24
+
25
+ const justConnect = async (database = 'database',collection ='collection',data='data',options = undefined,url = process.env.MONGODB_URL)=>{
26
+ const mongoClient = new MongoClient(url, options);
27
+ await mongoClient.connect();
28
+ return new Client(mongoClient);
29
+ }
30
+
31
+ const createDatabase = async (database = 'database',options = undefined,url = process.env.MONGODB_URL)=>{
32
+ const mongoClient = new MongoClient(url, options);
33
+ await mongoClient.connect();
34
+ return new Client(mongoClient).database(database);
35
+ }
36
+
37
+ const createCollection = async (database = 'database',collection ='collection',options = undefined,url = process.env.MONGODB_URL)=>{
38
+ const mongoClient = new MongoClient(url, options);
39
+ await mongoClient.connect();
40
+ return new Client(mongoClient).database(database).collection(collection);
41
+ }
42
+
43
+ const createData = async (database = 'database',collection ='collection',data='data',options = undefined,url = process.env.MONGODB_URL)=>{
44
+ const mongoClient = new MongoClient(url, options);
45
+ await mongoClient.connect();
46
+ return new Client(mongoClient).database(database).collection(collection).data(data);
47
+ }
48
+
49
+ module.exports = { MongoDB, Client, Database, Collection, Data, createDatabase,createCollection,createData,justConnect };
@@ -0,0 +1,37 @@
1
+ const mongodb = require("mongodb");
2
+ const { Database } = require("../structers/Database.js");
3
+
4
+ class Client {
5
+ /**
6
+ * @type {mongodb.MongoClient}
7
+ */
8
+ Client;
9
+
10
+ /**
11
+ * @param {MongoClient} client
12
+ */
13
+ constructor(client) {
14
+ this.Client = client;
15
+ }
16
+
17
+ /**
18
+ * If database does not exists, creates database then returns the database. Otherwise just returns database.
19
+ * @param {String} databaseName
20
+ * @return {Database}
21
+ */
22
+ database(databaseName) {
23
+ const database = new Database(this.Client, databaseName);
24
+ return database;
25
+ }
26
+
27
+ /**
28
+ * Drops the database.
29
+ * @param {String} databaseName
30
+ * @return {Promise<void>}
31
+ */
32
+ async dropDatabase(databaseName) {
33
+ await this.Client.db(databaseName).dropDatabase();
34
+ }
35
+ }
36
+
37
+ module.exports = { Client };
@@ -0,0 +1,136 @@
1
+ const mongodb = require("mongodb");
2
+ const { Data } = require("./Data.js");
3
+
4
+ const PathFormat = require("../tools/FormatTool.js");
5
+
6
+ class Collection {
7
+ /**
8
+ * @type {mongodb.Collection}
9
+ */
10
+ Collection;
11
+
12
+ /**
13
+ * @param {mongodb.Collection} client
14
+ */
15
+ constructor(collection) {
16
+ this.Collection = collection;
17
+ }
18
+
19
+ /**
20
+ * If data does not exists, creates data then returns the data. Otherwise just returns data.
21
+ * @param {String} key
22
+ * @return {Data}
23
+ */
24
+ data(key) {
25
+ return new Data(key, this.Collection);
26
+ }
27
+
28
+ /**
29
+ * Drop the data in collection.
30
+ * @param {String} key
31
+ * @return {Data}
32
+ */
33
+ async dropData(key) {
34
+ return this.Collection.deleteOne({ key: key });
35
+ }
36
+
37
+ /**
38
+ * Sorts all data by the value in the specified path.
39
+ * @param {String} path It determines the field where the sorting will be done.
40
+ * @param {("DESC"|"ASC")} orderType Sorts the data in DESC (descending) or ASC (ascending).
41
+ * @param {number} limit It determines the data limit to be received.
42
+ * @return {Array<any>}
43
+ */
44
+ async sort(path, orderType, limit = 0) {
45
+ path = PathFormat(path);
46
+
47
+ const order = orderType == "DESC" ? -1 : 1;
48
+ const data = await this.Collection.find({ [path]: { $exists: true } }).sort(path, order).limit(limit).toArray();
49
+
50
+ return data;
51
+ }
52
+
53
+ /**
54
+ * In all data, the value is assigned to the specified path.
55
+ * @param {String} path
56
+ * @param {any} value
57
+ * @return {mongodb.UpdateWriteOpResult}
58
+ */
59
+ async set(path, value) {
60
+ path = PathFormat(path);
61
+
62
+ return await this.Collection.updateMany({}, { $set: { [path]: value } }, { upsert: true });
63
+ }
64
+
65
+ /**
66
+ * Removes a field from all data in the collection.
67
+ * @param {String} path
68
+ * @return {mongodb.UpdateWriteOpResult}
69
+ */
70
+ async delete(path) {
71
+ path = PathFormat(path);
72
+
73
+ return await this.Collection.updateMany({}, { $unset: { [path]: "" } }, { upsert: true });
74
+ }
75
+
76
+ /**
77
+ * Perform mathematical addition in the specified path in all data.
78
+ * @param {String} path
79
+ * @param {Number} value
80
+ * @return {mongodb.UpdateWriteOpResult}
81
+ */
82
+ async add(path, value) {
83
+ path = PathFormat(path);
84
+
85
+ return await this.Collection.updateMany({}, { $inc: { [path]: Math.abs(value) } }, { upsert: true });
86
+ }
87
+ /**
88
+ * Perform mathematical subtraction in the specified path in all data.
89
+ * @param {String} path
90
+ * @param {Number} value
91
+ * @return {mongodb.UpdateWriteOpResult}
92
+ */
93
+ async sub(path, value) {
94
+ path = PathFormat(path);
95
+
96
+ return await this.Collection.updateMany({}, { $inc: { [path]: -Math.abs(value) } }, { upsert: true });
97
+ }
98
+
99
+ /**
100
+ * Push value to array in specified path in all data.
101
+ * @param {String} path
102
+ * @param {any} value
103
+ * @return {mongodb.UpdateWriteOpResult}
104
+ */
105
+ async push(path, value) {
106
+ path = PathFormat(path);
107
+
108
+ return await this.Collection.updateMany({}, { $push: { [path]: value } }, { upsert: true });
109
+ }
110
+
111
+ /**
112
+ * Values ​​in the specified array are added to all data in the collection in the specified path.
113
+ * @param {String} path
114
+ * @param {any[]} values
115
+ * @return {mongodb.UpdateWriteOpResult}
116
+ */
117
+ async pushRange(path, values) {
118
+ path = PathFormat(path);
119
+
120
+ return await this.Collection.updateMany({}, { $push: { [path]: { $each: values } } }, { upsert: true });
121
+ }
122
+
123
+ /**
124
+ * Pull the value from the array in the specified path in all data.
125
+ * @param {String} path
126
+ * @param {any} value
127
+ * @return {mongodb.UpdateWriteOpResult}
128
+ */
129
+ async pull(path, value) {
130
+ path = PathFormat(path);
131
+
132
+ return await this.Collection.updateMany({}, { $pull: { [path]: value } }, { upsert: true });
133
+ }
134
+ }
135
+
136
+ module.exports = { Collection };
@@ -0,0 +1,228 @@
1
+ const mongodb = require("mongodb");
2
+ const PathFormat = require("../tools/FormatTool.js");
3
+
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
+ }
226
+ }
227
+
228
+ module.exports = { Data };
@@ -0,0 +1,53 @@
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 };
@@ -0,0 +1,5 @@
1
+ function PathFormat(str) {
2
+ return "data." + str;
3
+ }
4
+
5
+ module.exports = PathFormat;