mahfuz-connect 1.0.0 → 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- package/package.json +4 -1
- package/mahfuz-connect2.js +0 -73
- package/test.success.js +0 -15
- package/test2.js +0 -25
package/package.json
CHANGED
@@ -1,8 +1,11 @@
|
|
1
1
|
{
|
2
2
|
"name": "mahfuz-connect",
|
3
|
-
"version": "1.0.
|
3
|
+
"version": "1.0.1",
|
4
4
|
"description": "module to connect databases in web development",
|
5
5
|
"main": "mahfuz-connect-obfuscated.js",
|
6
|
+
"files": [
|
7
|
+
"MahfuzConnect-obfuscated.js"
|
8
|
+
],
|
6
9
|
"scripts": {
|
7
10
|
"test": "echo \"Error: no test specified\" && exit 1"
|
8
11
|
},
|
package/mahfuz-connect2.js
DELETED
@@ -1,73 +0,0 @@
|
|
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/test.success.js
DELETED
@@ -1,15 +0,0 @@
|
|
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
DELETED
@@ -1,25 +0,0 @@
|
|
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
|
-
})();
|