npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

dalisra-mongo-wrapper

v4.0.1

Published

Simple mongodb wrapper

Readme

mongo-wrapper

Simple mongodb wrapper. 100% test coverage. Works with Promises, callbacks and async / await.

Install

npm install dalisra-mongo-wrapper -S

Connecting to database

With Default Options

// Callback based:
const mongo = require("dalisra-mongo-wrapper")
// keep trying to connect untill success
mongo.connect(() => {
    // You are now connected to mongodb on localhost:27017 with "test" database as default
})

// Promise based:
mongo.connect().then(client => {
    // You are now connected to mongodb...
})

// In async function:
let client = await mongo.connect()
// You are now connected to mongodb..

Define database

const mongo = require("dalisra-mongo-wrapper")
// keep trying to connect untill success
mongo.connect({
    connectionString: "mongodb://localhost:27017", // as of 3.6 you dont need to provide database in connection string
    database: "foo"
}, (err, client) => {
    // You are now connected to mongodb on localhost:27017 with "foo" database as default
})

With options

Default options that you can override are:

const options = {
    connectionString: "mongodb://localhost:27017/test", //Generated automatically if not specified.
    protocol: "mongodb", // if connectionString is provided this options is ignored
    host: "localhost", // if connectionString is provided this options is ignored
    port: 27017, // if connectionString is provided this options is ignored
    database: "test", // default database to return, since 3.6 driver you can change database
    maxConnectAttempts: 0, //how many times to try before giving up, 0 = never giveup.
    connectRetryDelay: 5000, // how many miliseconds to wait after each failed attempt to connect
    reconnect: true, // what to do if connection to database closes. (on "disconnect" event)
    log:{
        debug: console.log,
        error: console.error
    },
    mongoClientOptions : {
        useNewUrlParser: true,
        useUnifiedTopology: true
    }
}

Once you override wanted options you can connect to database as follows:

mongo.connectoToMongo(options).then(() => {
    // You are now successfully connected to database
}).catch(err => {
    //Reached maxConnectionAttempts, giving up..
})

Helper methods

Utility

mongo.client() // <- MongoClient, if null it means not connected.
mongo.db() // <- Mongo database pointer to default database
mongo.db("foo") // <- Change database and get database pointer
mongo.collection("bar") // <- Get collection pointer.
mongo.mongodb // <- Returns original MongoDb driver
mongo.ObjectID // <- Shortcut for ObjectID
mongo.getConnectionString() // <- Get connection string that has been used
mongo.close() // <- Close db connection

Data

mongo.saveData(collection, data, callback) // <- Shortcut to save (insert or update) data to database (uses bulkWrite for arrays of data)
mongo.clearData(collection, callback) // <- Clears all data in a collection

Example Usage

const mongo = require("dalisra-mongo-wrapper")

// keep trying to connect to mongodb on localhost with default parameters untill success
mongo.connect(async (err, client) => {
    if(err) throw err; // <- It is possible to config "give up conditions".

    // You are now connected to database and can start doing queries.
    await mongo.clearData("test") //deletes all data in collection "test".
    await mongo.saveData("test", {number: 123, name:"Product 123"})
    let data = await mongo.collection("test").find({}).toArray()
    console.log(data) // { _id: ObjectId(...), number: 123, name: "Product 123"}
    await mongo.close()
})

Updates

  • 4.0.0 Updated nodejs driver for mongodb to 4.0.0, supports mongodb 5.0
  • 3.2.4 Bug fixing close() method that failed when calling without connection. Added more tests.
  • 3.2.3 bl version update (merged from security bot): Bump bl from 2.2.0 to 2.2.1
  • 3.2.2 Making tests green again. Fixing typo. Making sure we have 100% coverage.
  • 3.2.1 Fixed bug with logger for libraries such as winston.
  • 3.2.0 100% test coverage.
  • 3.1.2 Better code coverage. Small bugfixes. Removed chai and sinon dev dependencies.
  • 3.1.1 Removed insertData -> use saveData. Removed updateData -> use saveData. Cleaned up.
  • 3.1.0 Cleaned up code. Updated libraries. Implemented promises.
  • 3.0.0 Updated packages to latest. Removed dependency to async library.
  • 2.2.0 Updated packages to latest. Fixed autoreconnect, changed some debug logging to warn / info.