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.
This module contains the source code of the adapter. You just need to install the dependent library.
npm install nedb
This is the default adapter, no need any configuration.
// posts.service.js
const DbService = require("@moleculer/database").Service;
module.exports = {
name: "posts",
mixins: [DbService()]
}
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"
}
})]
}
// 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
}
}
}
})]
}
// 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" })
}
}
})]
}
Property | Type | Default | Description |
---|---|---|---|
neDB |
`Object | DataStore` | null |
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.
const row = await this.updateEntity(ctx, {
id: "YVdnh5oQCyEIRja0",
$set: {
status: false,
height: 192
},
$inc: {
age: 1
},
$unset: {
dob: true
}
}, { raw: true });
No any additional methods.