Learn about our RFC process, Open RFC meetings & more.Join in the discussion! »

moleculer-db-adapter-mongoose

0.8.6 • Public • Published

Moleculer logo

moleculer-db-adapter-mongoose NPM version

Mongoose adapter for Moleculer DB service

Features

Install

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

Usage

"use strict";
 
const { ServiceBroker } = require("moleculer");
const DbService = require("moleculer-db");
const MongooseAdapter = require("moleculer-db-adapter-mongoose");
const mongoose = require("mongoose");
 
const broker = new ServiceBroker();
 
// Create a Mongoose service for `post` entities
broker.createService({
    name: "posts",
    mixins: [DbService],
    adapter: new MongooseAdapter("mongodb://localhost/moleculer-demo"),
    model: mongoose.model("Post", mongoose.Schema({
        title: { type: String },
        content: { type: String },
        votes: { type: Number, default: 0}
    }))
});
 
 
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

Example with connection URI

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

Example with URI and options

new MongooseAdapter("mongodb://db-server-hostname/my-db", {
    user: process.env.MONGO_USERNAME,
    pass: process.env.MONGO_PASSWORD
    keepAlive: true
})

Test

$ npm test

In development with watching

$ npm run ci

License

The project is available under the MIT license.

Contact

Copyright (c) 2016-2020 MoleculerJS

@moleculerjs @MoleculerJS

Install

npm i moleculer-db-adapter-mongoose

DownloadsWeekly Downloads

7,258

Version

0.8.6

License

MIT

Unpacked Size

49.5 kB

Total Files

16

Last publish

Collaborators

  • avatar