mahfuz-connect 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1 @@
1
+ const a0_0x2e1965=a0_0x2c1f;(function(_0x58bd10,_0x75c45a){const _0x2735e6=a0_0x2c1f,_0x3682b7=_0x58bd10();while(!![]){try{const _0x465cb1=-parseInt(_0x2735e6(0x96))/0x1+-parseInt(_0x2735e6(0x8a))/0x2*(-parseInt(_0x2735e6(0x9e))/0x3)+parseInt(_0x2735e6(0x97))/0x4+-parseInt(_0x2735e6(0x8f))/0x5+-parseInt(_0x2735e6(0x9d))/0x6+parseInt(_0x2735e6(0x93))/0x7*(-parseInt(_0x2735e6(0x9f))/0x8)+parseInt(_0x2735e6(0x91))/0x9*(parseInt(_0x2735e6(0x99))/0xa);if(_0x465cb1===_0x75c45a)break;else _0x3682b7['push'](_0x3682b7['shift']());}catch(_0xf2bb97){_0x3682b7['push'](_0x3682b7['shift']());}}}(a0_0x3d62,0x824c6));function a0_0x3d62(){const _0x124a6f=['stack','7eRErAP','message','connectToMySQL','683255JXNDSl','2702580KPEliS','mongodb://','20jiTbXn','Error\x20connecting\x20to\x20MySQL:','Connected\x20to\x20MongoDB\x20successfully','MongoDB\x20connection\x20failed:\x20','4492518hPVPxg','30123QUkfXV','2229272igmnrK','mysql2/promise','mongoose','46FYFfok','connect','error','config','log','4552565Qbnzxd','exports','10117323JKakUm'];a0_0x3d62=function(){return _0x124a6f;};return a0_0x3d62();}const mongoose=require(a0_0x2e1965(0x89)),mysql=require(a0_0x2e1965(0x88));function a0_0x2c1f(_0x487f79,_0x254706){const _0x3d6257=a0_0x3d62();return a0_0x2c1f=function(_0x2c1fca,_0x12fa66){_0x2c1fca=_0x2c1fca-0x88;let _0x3b07d6=_0x3d6257[_0x2c1fca];return _0x3b07d6;},a0_0x2c1f(_0x487f79,_0x254706);}class MahfuzConnect{constructor(_0x466115,_0x2465a1={}){const _0x21b96f=a0_0x2e1965;this[_0x21b96f(0x8d)]=_0x466115,this['options']=_0x2465a1;}['connectToMongoDB']=async()=>{const _0xaf5aa4=a0_0x2e1965,{host:_0x4ec7a2,port:port=0x6989,username:_0x22ffd3,password:_0x5c954e,dbName:_0x17d3b0}=this[_0xaf5aa4(0x8d)];let _0x279a87;_0x22ffd3&&_0x5c954e?_0x279a87=_0xaf5aa4(0x98)+_0x22ffd3+':'+encodeURIComponent(_0x5c954e)+'@'+_0x4ec7a2+':'+port+'/'+_0x17d3b0+'?authSource=admin':_0x279a87=_0xaf5aa4(0x98)+_0x4ec7a2+':'+port+'/'+_0x17d3b0;try{await mongoose[_0xaf5aa4(0x8b)](_0x279a87,{...this['options']}),console[_0xaf5aa4(0x8e)](_0xaf5aa4(0x9b));}catch(_0x2549fd){console['error']('Error\x20connecting\x20to\x20MongoDB:',_0x2549fd['message']);throw new Error(_0xaf5aa4(0x9c)+_0x2549fd[_0xaf5aa4(0x94)]);}};[a0_0x2e1965(0x95)]=async()=>{const _0x4229a8=a0_0x2e1965,{host:_0x49530f,port:port=0xcea,username:_0x1974f6,password:_0x14e059,dbName:_0x2bccbb}=this[_0x4229a8(0x8d)];try{const _0x28f30b=await mysql['createConnection']({'host':_0x49530f,'port':port,'user':_0x1974f6,'password':_0x14e059,'database':_0x2bccbb});return console[_0x4229a8(0x8e)]('Connected\x20to\x20MySQL\x20successfully'),_0x28f30b;}catch(_0x20e9e7){console['error'](_0x4229a8(0x9a),_0x20e9e7['message']),console[_0x4229a8(0x8c)]('Stack\x20trace:',_0x20e9e7[_0x4229a8(0x92)]);throw _0x20e9e7;}};}module[a0_0x2e1965(0x90)]=MahfuzConnect;
@@ -0,0 +1,73 @@
1
+ const mongoose = require("mongoose");
2
+ const mysql = require("mysql2/promise");
3
+
4
+ /**
5
+ * MahfuzConnect: A class to handle connections to both MongoDB and MySQL.
6
+ */
7
+ class MahfuzConnect {
8
+ constructor(config, options = {}) {
9
+ this.config = config;
10
+ this.options = options;
11
+ }
12
+
13
+ /**
14
+ * Connects to MongoDB.
15
+ */
16
+ connectToMongoDB = async () => {
17
+ const { host, port = 27017, username, password, dbName } = this.config;
18
+
19
+ // Check if username/password are provided (optional for non-authenticated MongoDB)
20
+ let mongoURI;
21
+ if (username && password) {
22
+ mongoURI = `mongodb://${username}:${encodeURIComponent(password)}@${host}:${port}/${dbName}?authSource=admin`;
23
+ } else {
24
+ mongoURI = `mongodb://${host}:${port}/${dbName}`;
25
+ }
26
+
27
+ try {
28
+ // Connect using Mongoose with default options
29
+ await mongoose.connect(mongoURI, { ...this.options });
30
+ console.log("Connected to MongoDB successfully");
31
+ } catch (error) {
32
+ console.error("Error connecting to MongoDB:", error.message);
33
+ throw new Error(`MongoDB connection failed: ${error.message}`);
34
+ }
35
+ };
36
+
37
+ /**
38
+ * Connects to MySQL.
39
+ */
40
+ connectToMySQL = async () => {
41
+ const { host, port = 3306, username, password, dbName } = this.config;
42
+
43
+ try {
44
+ // Create a connection to MySQL
45
+ const connection = await mysql.createConnection({
46
+ host: host,
47
+ port: port,
48
+ user: username,
49
+ password: password,
50
+ database: dbName,
51
+ });
52
+
53
+ console.log("Connected to MySQL successfully");
54
+
55
+ // Return the connection for further usage
56
+ return connection;
57
+ } catch (error) {
58
+ console.error("Error connecting to MySQL:", error.message);
59
+ console.error("Stack trace:", error.stack); // Additional debugging information
60
+ throw error; // Rethrow the error for handling elsewhere
61
+ }
62
+ };
63
+
64
+ }
65
+
66
+ module.exports = MahfuzConnect;
67
+
68
+ // Example Usage:
69
+ // const MahfuzConnect = require("./MahfuzConnect");
70
+ // const dbConfig = { host: "localhost", username: "root", password: "password", dbName: "test" };
71
+ // const mahfuz = new MahfuzConnect(dbConfig);
72
+ // mahfuz.connectToMongoDB(); // For MongoDB
73
+ // mahfuz.connectToMySQL(); // For MySQL
package/package.json ADDED
@@ -0,0 +1,19 @@
1
+ {
2
+ "name": "mahfuz-connect",
3
+ "version": "1.0.0",
4
+ "description": "module to connect databases in web development",
5
+ "main": "mahfuz-connect-obfuscated.js",
6
+ "scripts": {
7
+ "test": "echo \"Error: no test specified\" && exit 1"
8
+ },
9
+ "author": "Mohammad Mahfuz Rahman",
10
+ "license": "ISC",
11
+ "dependencies": {
12
+ "mongoose": "^8.8.2",
13
+ "mysql2": "^3.11.4"
14
+ },
15
+ "devDependencies": {
16
+ "javascript-obfuscator": "^4.1.1",
17
+ "terser": "^5.36.0"
18
+ }
19
+ }
@@ -0,0 +1,15 @@
1
+ const MahfuzConnect = require('./mahfuz-connect')
2
+ // Example usage
3
+ const dbConfig = {
4
+ host: "localhost",
5
+ port: 27017,
6
+ username: "admin",
7
+ password: "adminPassword",
8
+ dbName: "myDatabase",
9
+ };
10
+
11
+ // Create an instance of MongoDBConnector
12
+ const dbConnector1 = new MahfuzConnect(dbConfig);
13
+
14
+ // Call the connectToMongoDB method
15
+ dbConnector1.connectToMongoDB();
package/test2.js ADDED
@@ -0,0 +1,25 @@
1
+ const MahfuzConnect = require('./mahfuz-connect')
2
+ // Example usage
3
+ const dbConfig = {
4
+ host: "localhost",
5
+ port: 3306,
6
+ username: "",
7
+ password: "",
8
+ dbName: "myDatabase",
9
+ };
10
+
11
+ // Create an instance of MySQLConnector
12
+ const dbConnector = new MahfuzConnect(dbConfig);
13
+
14
+ // Call the connectToMySQL method
15
+ (async () => {
16
+ try {
17
+ const connection = await dbConnector.connectToMySQL();
18
+
19
+ // Use the connection (e.g., run a query)
20
+ const [rows] = await connection.query("SELECT NOW()");
21
+ console.log("Current Time:", rows[0]["NOW()"]);
22
+ } catch (error) {
23
+ console.error("Failed to connect to MySQL:", error);
24
+ }
25
+ })();