Reputation: 457
How can I access the mongo db connection variable globally in loopback. Currently it is done by executing the async:
Alerts.getDataSource().connector.connect(function (err, db) {
db.collection("dbname");
......................//other code
});
Is it possible make this db variable directly accessible from everywhere inside a model instead than calling connector.connect everywhere? Thanks in advance!
Upvotes: 1
Views: 325
Reputation: 459
All you need is connection pooling. Here you go!
// This is a global variable to use for handing the MongoDB client
var mongodb;
// Connection URL
var url = '[connectionString]';
// Create the db connection
Alerts.getDataSource().connector.connect(function (err, db) {
mongodb = db
......................//other code
});
Here is a better explanation on Mongodb Connection Pooling
Upvotes: 1