1.0.1 • Published 7 years ago
p-mongo-database v1.0.1
A simple client that caches Db and Collection instances for mongo-db.
- The instance is created using require call with database url as parameter
- The returned instance provides calls to get a collection and close the database
- Two events namely "closed" and "connected" are also provided.
var url = "mongodb://localhost:27017/databaseName";
var mongodatabase = require("p-mongo-database")(url);
var collectionName = "collectionName";
mongodatabase.on("closed", function() {
console.log("database closed");
})
mongodatabase.on("connected", function() {
console.log("database connected");
mongodatabase.collection(collectionName, (err, collection) => {
console.log("You have access to collection object here.");
console.log("Subsequent requests to mongodatase.collection for the same collection name shall provide the cached value");
console.log("Now ensure that the database is closed");
mongodatabase.close();
});
});