{"id":20354355,"url":"https://github.com/hemerajs/hemera-mongo-store","last_synced_at":"2026-02-12T08:02:16.774Z","repository":{"id":57262749,"uuid":"114797133","full_name":"hemerajs/hemera-mongo-store","owner":"hemerajs","description":"Use Mongodb with Hemera","archived":false,"fork":false,"pushed_at":"2018-04-10T13:40:36.000Z","size":39,"stargazers_count":1,"open_issues_count":1,"forks_count":2,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-04-25T05:40:52.814Z","etag":null,"topics":["hemera","hemerajs","mongodb","store"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/hemerajs.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-12-19T18:15:04.000Z","updated_at":"2018-04-10T13:40:37.000Z","dependencies_parsed_at":"2022-09-01T04:27:09.429Z","dependency_job_id":null,"html_url":"https://github.com/hemerajs/hemera-mongo-store","commit_stats":null,"previous_names":[],"tags_count":9,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hemerajs%2Fhemera-mongo-store","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hemerajs%2Fhemera-mongo-store/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hemerajs%2Fhemera-mongo-store/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hemerajs%2Fhemera-mongo-store/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/hemerajs","download_url":"https://codeload.github.com/hemerajs/hemera-mongo-store/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":241886062,"owners_count":20036959,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["hemera","hemerajs","mongodb","store"],"created_at":"2024-11-14T23:08:19.999Z","updated_at":"2026-02-12T08:02:11.753Z","avatar_url":"https://github.com/hemerajs.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# :guardsman: Hemera-mongo-store package\n\n[![Build Status](https://travis-ci.org/hemerajs/hemera-mongo-store.svg?branch=master)](https://travis-ci.org/hemerajs/hemera-mongo-store)\n[![npm](https://img.shields.io/npm/v/hemera-mongo-store.svg?maxAge=3600)](https://www.npmjs.com/package/hemera-mongo-store)\n[![styled with prettier](https://img.shields.io/badge/styled_with-prettier-ff69b4.svg)](#badge)\n\nThis is a plugin to use [Mongodb](https://www.mongodb.com/) with Hemera.\n\n## Install\n\n```\nnpm i hemera-mongo-store --save\n```\n\n## Start Mongodb with Docker\n\n```js\ndocker run -d -p 27017:27017 -p 28017:28017 -e AUTH=no tutum/mongodb\n```\n\n## Example\n\n```js\nconst hemera = new Hemera(nats)\nhemera.use(require('hemera-joi'))\nhemera.use(require('hemera-mongo-store'), {\n  mongo: {\n    url: 'mongodb://localhost:27017/test'\n  }\n})\n```\n\n## Plugin decorators\n\n* mongodb.client\n* mongodb.db\n\n## Tests\n\n```\nnpm run test\n```\n\n## Access multiple databases\n\nIf you decide to use multiple MongoDB databases for your services you can use the `useDbAsTopicSuffix` option.\nThe 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.\n\nYou can find a full example [here](https://github.com/hemerajs/hemera/blob/master/examples/databases/mongo-store-suffix.js)\n\n## API\n\nSee [Store](https://github.com/hemerajs/hemera/tree/master/packages/hemera-store) Interface.\n\n## Extended JSON\n\nBecause 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.\n\n```js\nhemera.ready(() =\u003e {\n  const ObjectID = hemera.mongodb.client.ObjectID\n  hemera.act(\n    {\n      topic: 'mongo-store',\n      cmd: 'create',\n      collection: 'collTest',\n      data: {\n        name: 'peter',\n        date: new Date(),\n        objectId: new ObjectID()\n      }\n    },\n    (err, resp) =\u003e {\n      // The `date` and `objectId` values will be saved as strings in the database!\n    }\n  )\n})\n```\n\nIn order to \"fix\" this issue, the `mongo-store` supports the use of [MongoDB Extended JSON](https://docs.mongodb.com/manual/reference/mongodb-extended-json/). For example:\n\n```js\nhemera.ready(() =\u003e {\n  const ObjectID = hemera.mongodb.client.ObjectID\n  hemera.act(\n    {\n      topic: 'mongo-store',\n      cmd: 'create',\n      collection: 'collTest',\n      data: {\n        name: 'peter',\n        date: { $date: new Date() },\n        objectId: { $oid: new ObjectID() }\n      }\n    },\n    (err, resp) =\u003e {\n      // The data will now be persisted with the correct `Date` and `ObjectId` types.\n    }\n  )\n})\n```\n\nTo make things easiser, you can also use the `mongodb-extended-json` [package](https://www.npmjs.com/package/mongodb-extended-json) and its serialization capabilities (which the store uses under the hood), to make the objects \"less messy.\" For example:\n\n```js\nconst EJSON = require('mongodb-extended-json')\n\nhemera.ready(() =\u003e {\n  const ObjectID = hemera.mongodb.client.ObjectID\n  hemera.act(\n    {\n      topic: 'mongo-store',\n      cmd: 'create',\n      collection: 'collTest',\n      data: EJSON.serialize({\n        name: 'peter',\n        date: new Date(),\n        objectId: new ObjectID()\n      })\n    },\n    (err, resp) =\u003e {\n      // The data will now be persisted with the correct `Date` and `ObjectId` types.\n    }\n  )\n})\n```\n\nBe 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:\n\n1.  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)\n2.  We didn't want to enforce a hard dependency on the `mongodb-extended-json` package in your application code\n\nThat 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:\n\n```js\nconst EJSON = require('mongodb-extended-json')\n\nhemera.use(hemeraMongo, {\n  serializeResult: true,\n  mongo: {\n    url: 'mongodb://localhost:27017/test'\n  }\n})\n\nhemera.ready(() =\u003e {\n  hemera.act(\n    {\n      topic: 'mongo-store',\n      cmd: 'findById',\n      collection: 'collTest',\n      id: 'some-id-value'\n    },\n    function(err, resp) {\n      const doc = EJSON.deserialize(resp)\n      // Now `doc.date` and `doc.objectId` will be deserialized into\n      // `Date` and `ObjectId` types, respectively.\n    }\n  )\n})\n```\n\nFinally, 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:\n\n```js\nconst EJSON = require('mongodb-extended-json')\n\nhemera.ready(() =\u003e {\n  hemera.act(\n    {\n      topic: 'mongo-store',\n      cmd: 'find',\n      collection: 'collTest',\n      query: EJSON.serialize({\n        name: new RegExp(/^ja/, 'i')\n      })\n      // Without the EJSON library...\n      // query: {\n      //   name: { $regex: '^ja', $options: 'i' }\n      // }\n    },\n    function(err, resp) {}\n  )\n})\n```\n\n## Options\n\nFine-tuning of the calls to the MongoDB Node.js driver can be performed via `options.store`. The mapping from [Store API](https://github.com/hemerajs/hemera/tree/master/packages/hemera-store#store-api) to [MongoDB API](http://mongodb.github.io/node-mongodb-native/2.2/api/) is the following:\n\n* [create](https://github.com/hemerajs/hemera/tree/master/packages/hemera-store#create) =\u003e [insertMany](http://mongodb.github.io/node-mongodb-native/2.2/api/Collection.html#insertMany) and [insertOne](http://mongodb.github.io/node-mongodb-native/2.2/api/Collection.html#insertOne)\n* [update](https://github.com/hemerajs/hemera/tree/master/packages/hemera-store#update) =\u003e [findOneAndUpdate](http://mongodb.github.io/node-mongodb-native/2.2/api/Collection.html#findOneAndUpdate)\n* [updateById](https://github.com/hemerajs/hemera/tree/master/packages/hemera-store#updatebyid) =\u003e [findOneAndUpdate](http://mongodb.github.io/node-mongodb-native/2.2/api/Collection.html#findOneAndUpdate)\n* [find](https://github.com/hemerajs/hemera/tree/master/packages/hemera-store#find) =\u003e [find](http://mongodb.github.io/node-mongodb-native/2.2/api/Collection.html#find)\n* [findById](https://github.com/hemerajs/hemera/tree/master/packages/hemera-store#findbyid) =\u003e [findOne](http://mongodb.github.io/node-mongodb-native/2.2/api/Collection.html#findOne)\n* [remove](https://github.com/hemerajs/hemera/tree/master/packages/hemera-store#remove) =\u003e [deleteMany](http://mongodb.github.io/node-mongodb-native/2.2/api/Collection.html#deleteMany)\n* [removeById](https://github.com/hemerajs/hemera/tree/master/packages/hemera-store#removebyid) =\u003e [findOneAndDelete](http://mongodb.github.io/node-mongodb-native/2.2/api/Collection.html#findOneAndDelete)\n* [replace](https://github.com/hemerajs/hemera/tree/master/packages/hemera-store#replace) =\u003e [updateMany](http://mongodb.github.io/node-mongodb-native/2.2/api/Collection.html#updateMany)\n* [replaceById](https://github.com/hemerajs/hemera/tree/master/packages/hemera-store#replacebyid) =\u003e [findOneAndReplace](http://mongodb.github.io/node-mongodb-native/2.2/api/Collection.html#findOneAndReplace)\n* [count](https://github.com/hemerajs/hemera/tree/master/packages/hemera-store#count) =\u003e [count](http://mongodb.github.io/node-mongodb-native/2.2/api/Collection.html#count)\n\n### Example:\n\n```js\nhemera.use(hemeraMongo, {\n  mongo: { url: 'mongodb://localhost:27017/test' },\n  store: {\n    updateById: { returnOriginal: false }\n  }\n})\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhemerajs%2Fhemera-mongo-store","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhemerajs%2Fhemera-mongo-store","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhemerajs%2Fhemera-mongo-store/lists"}