I am doing an app using Node - Mongo - Graphql. So which is the best way to establish connections. Suppose if i make a db connection intially and if it fails in any case how can i reconnect it . Any other better way? Prons and cons of this method
I have been using Mongorito lately. I was never a fan of Mongoose, which enforces schema, which IMO nukes one of the best features of Mongo...it's LACK of schema and dynamic objects. Using Mongorito, it is fully async/await compatible, and you only need to call connect once to get the connection pool going:
// Put this in your app.js or wherever you bootstrap your servertry {
const db = new Mongorito(mongoUri);
await db.connect();
// Now you are connected...and you can use Mongorito // models anywhere in the app without having to worry // about connecting again
} catch (ex) {
// Connection failed, deal with it
}
Jon
Mentor, Architect, Developer
I have been using Mongorito lately. I was never a fan of Mongoose, which enforces schema, which IMO nukes one of the best features of Mongo...it's LACK of schema and dynamic objects. Using Mongorito, it is fully async/await compatible, and you only need to call connect once to get the connection pool going:
// Put this in your app.js or wherever you bootstrap your server try { const db = new Mongorito(mongoUri); await db.connect(); // Now you are connected...and you can use Mongorito // models anywhere in the app without having to worry // about connecting again } catch (ex) { // Connection failed, deal with it }