Skip to content

Latest commit

 

History

History
113 lines (94 loc) · 2.65 KB

NeDB.md

File metadata and controls

113 lines (94 loc) · 2.65 KB

NeDB adapter

The NeDB adapter is the default adapter. It uses the NeDB library which is a lightweight embedded persistent or in-memory database. The API is a subset of MongoDB's API.

Use this adapter for prototyping and testing.

Install

This module contains the source code of the adapter. You just need to install the dependent library.

npm install nedb

Usage

In memory NeDB adapter

This is the default adapter, no need any configuration.

// posts.service.js
const DbService = require("@moleculer/database").Service;

module.exports = {
    name: "posts",
    mixins: [DbService()]
}

Persistent database

To use a persistent database, set the filename.

// posts.service.js
const DbService = require("@moleculer/database").Service;

module.exports = {
    name: "posts",
    mixins: [DbService({
        adapter: {
            type: "NeDB",
            options: "./posts.db"
        }
    })]
}

Using NeDB options

// posts.service.js
const DbService = require("@moleculer/database").Service;

module.exports = {
    name: "posts",
    mixins: [DbService({
        adapter: {
            type: "NeDB",
            options: {
                neDB: {
                    inMemoryOnly: true,
                    corruptAlertThreshold: 0.5
                }
            }
        }
    })]
}

Using custom NeDB instance

// posts.service.js
const DbService = require("@moleculer/database").Service;
const MyNeDB = require("...");

module.exports = {
    name: "posts",
    mixins: [DbService({
        adapter: {
            type: "NeDB",
            options: {
                neDB: new MyNeDB({ filename: "./posts.db" })
            }
        }
    })]
}

Options

Property Type Default Description
neDB `Object DataStore` null

Raw update

If you want to update entity and using raw changes, use the updateEntity method with { raw: true } options. In this case, you can use NeDB modifiers in the params parameter.

Example

const row = await this.updateEntity(ctx, {
    id: "YVdnh5oQCyEIRja0",

    $set: {
        status: false,
        height: 192
    },
    $inc: {
        age: 1
    },
    $unset: {
        dob: true
    }
}, { raw: true });

Additional methods

No any additional methods.