@taglivros/mongodb-api

1.0.9 • Public • Published

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.

Readme

Keywords

Package Sidebar

Install

npm i @taglivros/mongodb-api

Weekly Downloads

0

Version

1.0.9

License

ISC

Unpacked Size

23.7 kB

Total Files

16

Last publish

Collaborators

  • taglivrosti