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 🙏

© 2025 – Pkg Stats / Ryan Hefner

@taglivros/mongodb-api

v1.0.9

Published

mongodb api simplified for applications

Downloads

3

Readme

Mongo DB API simplified

This is a wrapper above the Node.js MongoDB driver API. It's main goal is to reduce the complexity of handling connections and providing custom made and easy to use APIs to access the mongodb.

For full reference of the options refer to their documentation.

How to use

There are two ways of using this module:

  1. Managing the mongo connection
  2. The module manages the mongo connections

1. Managing the mongo connection

When you import the createDbApi, the function expects a function that creates connections. With this approach, you have more flexibility and a bigger burden since you are the responsible to manage the connections.

This approach is used for the injecting the testConnection. See below Testing.

2. The module manages the mongo connections

When you import the dbApi the module looks for the following environment variable:

  • MONGODB_SERVER_URI : Url to access the database with the credentials
  • MONGODB_NAME : The name of the database to connect

Then it creates a connection, if it wasn't created yet, and returns the dbApi which uses the connection. Once the connection is created, the module relies on a caching mechanism, to reuse connections.

Testing

The package provides the mongo in memory and and a test connection.

To start the mongo in memory, add to your jest.config.js file:

{
    globalSetup: '<rootDir>/node_modules/@taglivros/mongodb-api/__tests__/globalSetup.js',
    globalTeardown: '<rootDir>/node_modules/@taglivros/mongodb-api/__tests__/globalTeardown.js'
}

The testConnection returns starts a connection and returns a Promise that enables you to getDatabase, cleanUp, or close the connection.

The code below demonstrates how to use the testConnection:

let connectionPromise = testConnection()

let dbApi = createDbApi( async() => {
  const connection = await connectionPromise
  return connection.getDatabase()
})

afterAll( async() => {
    const connection = await connectionPromise
    await connection.cleanUp()
    await connection.close()
} )

First, it starts the connection with the testConnection.

Then it creates the dbApi injecting the connection through the async function that returns the connection's database.

At last, in the afterAll, it clears the database using cleanUp and closes the connection with close. Once the connection is closed, it cannot be reused. But you could still start a new connection with the testConnection.

API

aggregate

// TO DO

count

// TO DO

find

// TO DO

softDeleteOne

// TO DO

deleteOne

//TO DO

updateOne

//TO DO

updateOneById

//TO DO

createOrUpdateONe

//TO DO

findOne

Find one document in a collection.

Usage:

async ( id, email ) => {
    const document = await dbApi.findOne( 'Collection', { _id: id, email }, { sort: { createdAt: -1 } } )
}

Returns the document or a NotFound in case query don't match anything. May also throw a DatabaseException.

findOneById

Sintax sugar for findOne( { _id: id } )

Usage:

async () => {
    const document = await dbApi.findOneById( 'Collection', 'id' )
}

Returns the document or a NotFound in case id don't exist. May also throw a DatabaseException.

insertOne

Inserts a document to the collection

Usage:

async () => {
    const document = await dbApi.insertOne( 'Collection', { name: 'UserName', age: 23 } )
}

Returns the document created. May also throw a DatabaseException.