node package manager
It’s your turn. Help us improve JavaScript. Take the 2017 JavaScript Ecosystem Survey »

moleculer-db-adapter-mongo

Moleculer logo

moleculer-db-adapter-mongo NPM version

MongoDB native adapter for Moleculer DB service.

Features

  • schemaless adapter
  • MongoDB driver v2.2

Install

$ npm install moleculer-db moleculer-db-adapter-mongo --save

Usage

"use strict";
 
const { ServiceBroker } = require("moleculer");
const DbService = require("moleculer-db");
const MongoDBAdapter = require("moleculer-db-adapter-mongo");
 
const broker = new ServiceBroker();
 
// Create a Mongoose service for `post` entities
broker.createService({
    name: "posts",
    mixins: [DbService],
    adapter: new MongoDBAdapter("mongodb://localhost/moleculer-demo"),
    collection: "posts"
});
 
 
broker.start()
// Create a new post
.then(() => broker.call("posts.create", {
    title: "My first post",
    content: "Lorem ipsum...",
    votes: 0
}))
 
// Get all posts
.then(() => broker.call("posts.find").then(console.log));

Options

The constructor options need to be a String or an Object.

Example with connection URI

new MongoDBAdapter("mongodb://localhost/moleculer-db")

Example with connection options

new MongoDBAdapter({
    uri: "mongodb://db-server-hostname/my-db",
    options: {
        user: process.env.MONGO_USERNAME,
        pass: process.env.MONGO_PASSWORD
        server: {
            socketOptions: {
                keepAlive: 1
            }
        }
    })

Test

$ npm test

In development with watching

$ npm run ci

License

The project is available under the MIT license.

Contact

Copyright (c) 2016-2017 Ice Services

@ice-services @MoleculerJS