Moleculer Database Service

0.9.0 · active · verified Wed Apr 22

moleculer-db is an official service mixin for the Moleculer microservices framework, providing a standardized way to interact with various databases. Its current stable version is 0.9.0, released in March 2026, which is compatible with Moleculer v0.15 and requires Node.js 22.x.x. The library offers common CRUD actions, robust caching, pagination, field filtering, and entity lifecycle events. It is designed with a pluggable adapter architecture, supporting popular databases like MongoDB, PostgreSQL, SQLite, MySQL, MSSQL, and NeDB (for prototyping). A key differentiator is its seamless integration into the Moleculer ecosystem, adhering to the 'database per service' pattern, and simplifying data persistence within a microservice architecture while maintaining active development and regular updates to align with the core framework.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to create a basic Moleculer service using `moleculer-db` with the default NeDB adapter, performing common CRUD operations like create, find, list (with pagination), update, and remove. It shows the mixin pattern for integrating `DbService` into a Moleculer service.

import { ServiceBroker } from 'moleculer';
import DbService from 'moleculer-db';

const broker = new ServiceBroker();

// Create a DB service for 'user' entities using the default NeDB adapter
broker.createService({
    name: 'users',
    mixins: [DbService],

    settings: {
        // Define which fields are exposed publicly
        fields: ['_id', 'username', 'name']
    },

    afterConnected() {
        // Seed the DB or perform other post-connection setup
        console.log('DB Service connected for users!');
        // Example: this.create({ username: 'initialUser', name: 'Initial Name', status: 1 });
    }
});

broker.start()
.then(async () => {
    console.log('Broker started. Performing CRUD operations...');

    // Create a new user
    const newUser = await broker.call('users.create', {
        username: 'john',
        name: 'John Doe',
        status: 1
    });
    console.log('Created user:', newUser);

    // Get all users
    const allUsers = await broker.call('users.find');
    console.log('All users:', allUsers);

    // List users with pagination (default page size/max page size apply)
    const pagedUsers = await broker.call('users.list', { page: 1, pageSize: 5 });
    console.log('Paged users (page 1):', pagedUsers.rows);

    // Update a user (assuming _id is 'john' for NeDB/default adapter, or a generated ID)
    // Note: In real scenarios, use the actual _id returned from creation
    const updatedUser = await broker.call('users.update', { id: newUser._id, name: 'Jane Doe' });
    console.log('Updated user:', updatedUser);

    // Delete a user
    await broker.call('users.remove', { id: newUser._id });
    console.log('User deleted.');
})
.catch(err => {
    console.error('Error starting broker or performing actions:', err);
    process.exit(1);
});

view raw JSON →