From 615ba5c157aa6249c343b6bc5772629457e43f70 Mon Sep 17 00:00:00 2001 From: Ankan Saha Date: Wed, 7 Jun 2023 12:22:20 +0530 Subject: [PATCH] Added - Added A Separate File for Connection to use New Features in connect Method --- README.md | 4 ++- package-lock.json | 4 +-- package.json | 2 +- src/Connect.js | 8 +++++ src/Start.ts | 87 ++++++++++++++++++++++++++++++++--------------- tsconfig.json | 2 +- 6 files changed, 75 insertions(+), 32 deletions(-) create mode 100644 src/Connect.js diff --git a/README.md b/README.md index cb87660..1ed0cc9 100644 --- a/README.md +++ b/README.md @@ -20,7 +20,9 @@ const Connector = new mongoAlways.Mongo('MongoDB_URL', 'Log: true/false'); // Lo ## Methods ```javascript -Connector.connect() // Connects to MongoDB +Connector.SingleConnect() // Connects to MongoDB once and you can disconnect using Connector.disconnect() +Connector.alwaysConnect() // Connects to MongoDB and automatically reconnects if disconnected & you can't disconnect +Connector.disconnect() // Disconnects from MongoDB if connected whenever you want (Only works with SingleConnect()) ``` diff --git a/package-lock.json b/package-lock.json index cc0d4d0..84aa02f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "mongo-always", - "version": "1.0.4", + "version": "2.3.5", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "mongo-always", - "version": "1.0.4", + "version": "2.3.5", "license": "MIT", "dependencies": { "mongoose": "^7.2.2" diff --git a/package.json b/package.json index 989233b..09da034 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "mongo-always", - "version": "2.3.5", + "version": "2.3.7", "description": "mongo-always is a simple package that helps you to connect to your MongoDB database with Mongoose. It also helps you to keep your connection alive.", "main": "./lib/Start.js", "types":"./lib/Start.d.ts", diff --git a/src/Connect.js b/src/Connect.js new file mode 100644 index 0000000..52080d9 --- /dev/null +++ b/src/Connect.js @@ -0,0 +1,8 @@ +import { connect } from "mongoose"; // import the mongoose module + +export default async function connectDB(MongoURL) { + await connect(MongoURL, { + useNewUrlParser: true, + useUnifiedTopology: true + }); // connect to the database +}; // create a function to connect to the database \ No newline at end of file diff --git a/src/Start.ts b/src/Start.ts index 424efe3..634bb96 100644 --- a/src/Start.ts +++ b/src/Start.ts @@ -1,4 +1,5 @@ import { connect, connection } from "mongoose"; // import the mongoose module +import connectDB from "./Connect"; // import the Connect function from the Connect.js file // global types type str = string; @@ -7,12 +8,13 @@ type bool = boolean; // class to run on start/* The above class is a TypeScript implementation of a MongoDB connection // handler that can connect to a local or cloud server and listen for // connection events. */ - export class Mongo { /* These are private properties of the `Mongo` class in TypeScript. */ - private MongoURL: str; - private Log: bool; - private ConnectionState: str; + private MongoURL: str; // string value to store the URL of the MongoDB database to connect to + private Log: bool; // boolean value to check if the logging is enabled or not + private ConnectionState: str; // string value to check if the connection is to cloud or local + private connection: typeof connection; // mongoose connection type + private Connect: (MongoURL:str) => Promise; // function to connect to the database /** * This is a constructor function that initializes properties for a MongoDB connection, including @@ -25,31 +27,50 @@ export class Mongo { * If set to false, no logging will occur. */ constructor( - MongoURL: str = "mongodb://localhost:27017/test", - Log: bool = true + MongoURL: str = "mongodb://localhost:27017/test", // default value is 'mongodb://localhost:27017/test' + Log: bool = true, // default value is true ) { - this.MongoURL = MongoURL; - this.Log = Log; - this.ConnectionState = "Local"; - } + this.MongoURL = MongoURL; // assign the MongoURL property + this.Log = Log; // assign the Log property + this.ConnectionState = "Local"; // assign the ConnectionState property + this.connection = connection; // assign the connection property + this.Connect = connectDB; // assign the Connect property + } // end of constructor /* The `connect()` method is a method of the `Mongo` class that connects to a MongoDB database using the `connect()` function from the `mongoose` module. It also checks if the connection is to a cloud or local server by checking if the `MongoURL` property includes the string "mongodb+srv". Finally, it calls the `listen()` method to listen for events related to the database connection. */ // method to connect to the database - async connect() { - connect(this.MongoURL); // connect to the database + async SingleConnect() { + this.Connect(this.MongoURL); // connect to the database this.MongoURL.includes("mongodb+srv") ? (this.ConnectionState = "Cloud") : "Local"; // check if the connection is to cloud or local + + this.Log === true + ? console.log( + `MongoDB connected successfully with ${this.ConnectionState} Server` + ) + : null; + } // end of SingleConnect method + + // method to connect to the database and listen for events + async alwaysConnect(): Promise { + this.Connect(this.MongoURL); // connect to the database + this.MongoURL.includes("mongodb+srv") + ? (this.ConnectionState = "Cloud") + : "Local"; // check if the connection is to cloud or local + // listen for events this.listen(); // listen for events - } // end of connect method + } // end of alwaysConnect method + + /* The `private listen()` method is a method of the `Mongo` class that listens for events related to the MongoDB database connection. It uses the `connection.on()` method from the `mongoose` module to listen for three events: `connected`, `error`, and `disconnected`. */ private listen() { - connection.on("connected", async (): Promise => { + this.connection.on("connected", async (): Promise => { this.Log === true ? console.log( `MongoDB connected successfully with ${this.ConnectionState} Server` @@ -57,7 +78,7 @@ it calls the `listen()` method to listen for events related to the database conn : null; }); // listen for connected event - connection.on("error", async (): Promise => { + this.connection.on("error", async (): Promise => { this.Log === true ? console.log(" Error: MongoDB connection failed") : null; @@ -68,18 +89,30 @@ it calls the `listen()` method to listen for events related to the database conn ) : null; }); - connection.on("disconnected", async (): Promise => { - this.Log === true - ? console.log( - `MongoDB disconnected with ${this.ConnectionState} Server and trying to reconnect` - ) - : null; - await connect(this.MongoURL); // connect to the database - this.Log === true - ? console.log( - `MongoDB reconnected successfully with ${this.ConnectionState} Server` - ) - : null; + this.connection.on("disconnected", async (): Promise => { + // check if the connection is to cloud or local + this.Log === true + ? console.log( + `MongoDB disconnected with ${this.ConnectionState} Server and trying to reconnect` + ) + : null; + await connect(this.MongoURL); // connect to the database + this.Log === true + ? console.log( + `MongoDB reconnected successfully with ${this.ConnectionState} Server` + ) + : null; }); } // end of listen method + + // method to disconnect from the database + async disconnect(): Promise { + this.connection.close(); // disconnect from the database + + this.Log === true + ? console.log( + `MongoDB Disconnected successfully with ${this.ConnectionState} Server` + ) + : null; + } // end of disconnect method } // end of alwaysRun class diff --git a/tsconfig.json b/tsconfig.json index 03c2299..8099dd4 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -44,7 +44,7 @@ // "noResolve": true, /* Disallow 'import's, 'require's or ''s from expanding the number of files TypeScript should add to a project. */ /* JavaScript Support */ - // "allowJs": true, /* Allow JavaScript files to be a part of your program. Use the 'checkJS' option to get errors from these files. */ + "allowJs": true, /* Allow JavaScript files to be a part of your program. Use the 'checkJS' option to get errors from these files. */ // "checkJs": true, /* Enable error reporting in type-checked JavaScript files. */ // "maxNodeModuleJsDepth": 1, /* Specify the maximum folder depth used for checking JavaScript files from 'node_modules'. Only applicable with 'allowJs'. */