{"library":"moleculer-db","title":"Moleculer Database Service","description":"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.","language":"javascript","status":"active","last_verified":"Wed Apr 22","install":{"commands":["npm install moleculer-db"],"cli":null},"imports":["import { ServiceBroker } from 'moleculer';","import DbService from 'moleculer-db';","import MongooseAdapter from 'moleculer-db-adapter-mongoose';"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import { ServiceBroker } from 'moleculer';\nimport DbService from 'moleculer-db';\n\nconst broker = new ServiceBroker();\n\n// Create a DB service for 'user' entities using the default NeDB adapter\nbroker.createService({\n    name: 'users',\n    mixins: [DbService],\n\n    settings: {\n        // Define which fields are exposed publicly\n        fields: ['_id', 'username', 'name']\n    },\n\n    afterConnected() {\n        // Seed the DB or perform other post-connection setup\n        console.log('DB Service connected for users!');\n        // Example: this.create({ username: 'initialUser', name: 'Initial Name', status: 1 });\n    }\n});\n\nbroker.start()\n.then(async () => {\n    console.log('Broker started. Performing CRUD operations...');\n\n    // Create a new user\n    const newUser = await broker.call('users.create', {\n        username: 'john',\n        name: 'John Doe',\n        status: 1\n    });\n    console.log('Created user:', newUser);\n\n    // Get all users\n    const allUsers = await broker.call('users.find');\n    console.log('All users:', allUsers);\n\n    // List users with pagination (default page size/max page size apply)\n    const pagedUsers = await broker.call('users.list', { page: 1, pageSize: 5 });\n    console.log('Paged users (page 1):', pagedUsers.rows);\n\n    // Update a user (assuming _id is 'john' for NeDB/default adapter, or a generated ID)\n    // Note: In real scenarios, use the actual _id returned from creation\n    const updatedUser = await broker.call('users.update', { id: newUser._id, name: 'Jane Doe' });\n    console.log('Updated user:', updatedUser);\n\n    // Delete a user\n    await broker.call('users.remove', { id: newUser._id });\n    console.log('User deleted.');\n})\n.catch(err => {\n    console.error('Error starting broker or performing actions:', err);\n    process.exit(1);\n});","lang":"typescript","description":"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.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}