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

hemera-mongo-store

v6.0.0

Published

This is a plugin to use Mongodb with Hemera

Readme

:guardsman: Hemera-mongo-store package

Build Status npm styled with prettier

This is a plugin to use Mongodb with Hemera.

Install

npm i hemera-mongo-store --save

Start Mongodb with Docker

docker run -d -p 27017:27017 -p 28017:28017 -e AUTH=no tutum/mongodb

Example

const hemera = new Hemera(nats)
hemera.use(require('hemera-joi'))
hemera.use(require('hemera-mongo-store'), {
  mongo: {
    url: 'mongodb://localhost:27017/test'
  }
})

Plugin decorators

  • mongodb.client
  • mongodb.db

Tests

npm run test

Access multiple databases

If you decide to use multiple MongoDB databases for your services you can use the useDbAsTopicSuffix option. The database name e.g test is appended to the topic topic:"mongo-store.test". This ensures that your database is able to run under a different hemera service.

You can find a full example here

API

See Store Interface.

Extended JSON

Because the underlying NATS transport is simply passing JSON stringified messages between actions, certain native (or extended) MongoDB types will be lost. For example, sending the following action will result in the date and objectId fields being saved as strings, not as their corresponding Date and ObjectId types.

hemera.ready(() => {
  const ObjectID = hemera.mongodb.client.ObjectID
  hemera.act(
    {
      topic: 'mongo-store',
      cmd: 'create',
      collection: 'collTest',
      data: {
        name: 'peter',
        date: new Date(),
        objectId: new ObjectID()
      }
    },
    (err, resp) => {
      // The `date` and `objectId` values will be saved as strings in the database!
    }
  )
})

In order to "fix" this issue, the mongo-store supports the use of MongoDB Extended JSON. For example:

hemera.ready(() => {
  const ObjectID = hemera.mongodb.client.ObjectID
  hemera.act(
    {
      topic: 'mongo-store',
      cmd: 'create',
      collection: 'collTest',
      data: {
        name: 'peter',
        date: { $date: new Date() },
        objectId: { $oid: new ObjectID() }
      }
    },
    (err, resp) => {
      // The data will now be persisted with the correct `Date` and `ObjectId` types.
    }
  )
})

To make things easiser, you can also use the mongodb-extended-json package and its serialization capabilities (which the store uses under the hood), to make the objects "less messy." For example:

const EJSON = require('mongodb-extended-json')

hemera.ready(() => {
  const ObjectID = hemera.mongodb.client.ObjectID
  hemera.act(
    {
      topic: 'mongo-store',
      cmd: 'create',
      collection: 'collTest',
      data: EJSON.serialize({
        name: 'peter',
        date: new Date(),
        objectId: new ObjectID()
      })
    },
    (err, resp) => {
      // The data will now be persisted with the correct `Date` and `ObjectId` types.
    }
  )
})

Be default, responses returned from the mongo-store (via find or other actions) will not return the document(s) in MongoDB extended JSON format. This was done for two reasons:

  1. We wanted to avoid "forcing" users into dealing with { date: { $date: "2017-05..." } } response formats (instead, you can simply re-init the date by running new Date(resp.date) when/if you need to)
  2. We didn't want to enforce a hard dependency on the mongodb-extended-json package in your application code

That being said, if you want responses to be converted into extended format, you can enable the serializeResult plugin option. Also, to "auto-magically" convert all extended types, you can utilize mongodb-extended-json's deserialization capabilities. Example:

const EJSON = require('mongodb-extended-json')

hemera.use(hemeraMongo, {
  serializeResult: true,
  mongo: {
    url: 'mongodb://localhost:27017/test'
  }
})

hemera.ready(() => {
  hemera.act(
    {
      topic: 'mongo-store',
      cmd: 'findById',
      collection: 'collTest',
      id: 'some-id-value'
    },
    function(err, resp) {
      const doc = EJSON.deserialize(resp)
      // Now `doc.date` and `doc.objectId` will be deserialized into
      // `Date` and `ObjectId` types, respectively.
    }
  )
})

Finally, extended JSON can also be used in queries. This is useful when you need to query an ObjectId value that isn't the native _id field, or for Regular Expressions. For example:

const EJSON = require('mongodb-extended-json')

hemera.ready(() => {
  hemera.act(
    {
      topic: 'mongo-store',
      cmd: 'find',
      collection: 'collTest',
      query: EJSON.serialize({
        name: new RegExp(/^ja/, 'i')
      })
      // Without the EJSON library...
      // query: {
      //   name: { $regex: '^ja', $options: 'i' }
      // }
    },
    function(err, resp) {}
  )
})

Options

Fine-tuning of the calls to the MongoDB Node.js driver can be performed via options.store. The mapping from Store API to MongoDB API is the following:

Example:

hemera.use(hemeraMongo, {
  mongo: { url: 'mongodb://localhost:27017/test' },
  store: {
    updateById: { returnOriginal: false }
  }
})