@moleculer/database 
@moleculer/database is the official database access service for Moleculer. It is a service mixin that gives your service a validated field schema, generated CRUD actions (with REST endpoints for the API Gateway), populating between services, scopes, soft delete, caching, entity events and multi-tenancy — on top of pluggable adapters for NeDB, MongoDB and Knex (PostgreSQL, MySQL, SQLite, MSSQL…).
It is the successor of moleculer-db. New projects should use @moleculer/database; moleculer-db is still maintained for existing projects.
This page is a short introduction. The full reference lives in the module repository: @moleculer/database documentation.
RequirementsNode.js >= 22 and Moleculer >= 0.14.12 (0.15 recommended). The module follows the one database per service pattern: one service handles one entity/table. For multiple entities per service see the FAQ.
Features
- pluggable adapters: NeDB (in-memory or file, good for prototyping), MongoDB, Knex (SQL databases)
- generated CRUD actions with parameter validation and REST endpoints
- field definitions with sanitization, validation, defaults, read-only / immutable / virtual / hidden fields
- field-level permissions (read/write)
- populating entities from other Moleculer services
- scopes, soft delete, indexes, cascade delete
- action caching with automatic cache invalidation
- entity lifecycle events (
posts.created,posts.updated,posts.removed) - create/update/remove hooks
- streaming results
- multi-tenancy (record-, table- or database-based)
Install
npm install @moleculer/database |
Install the driver of the adapter you want to use:
npm install @seald-io/nedb # NeDB (optional, prototyping) |
Define a service
The field definitions use the fastest-validator schema format, extended with database-specific properties (primaryKey, columnName, readonly, onCreate, populate, …). All fields are optional unless required: true is set.
// posts.service.js |
Generated actions
The mixin registers these actions on the service (all of them can be disabled with the createActions: false mixin option). With moleculer-web and autoAliases: true the REST endpoints are generated automatically.
| Action | REST endpoint | Description |
|---|---|---|
find |
GET /posts/all |
Find entities by query (query, sort, limit, offset, fields, search, populate, scope) |
list |
GET /posts |
Paginated list (page, pageSize + the find params). Returns { rows, total, page, pageSize, totalPages } |
count |
GET /posts/count |
Count entities by query |
get |
GET /posts/:id |
Get an entity by ID |
resolve |
– | Get one or more entities by ID(s), optionally as a map |
create |
POST /posts |
Create an entity |
createMany |
– | Create multiple entities |
update |
PATCH /posts/:id |
Update fields of an entity |
replace |
PUT /posts/:id |
Replace an entity |
remove |
DELETE /posts/:id |
Delete an entity (or soft-delete it if configured) |
// Create a new post |
Expose via API Gateway
// api.service.js |
Adapters
If no adapter is defined, the service uses an in-memory NeDB database. The adapter connects lazily, on the first request, so the service starts even if the database server is not available yet.
MongoDB
mixins: [ |
Knex (PostgreSQL)
mixins: [ |
Adapter documentation: NeDB, MongoDB, Knex
Custom actions
Add your own actions next to the generated ones and use the entity methods of the mixin (findEntities, resolveEntities, createEntity, updateEntity, removeEntity, …).
// posts.service.js |
Populating
A field can be resolved from another service (typically its resolve action) when the caller asks for it with the populate parameter.
// posts.service.js |
const posts = await broker.call("posts.find", { populate: ["author"] }); |
Further reading
Everything below is documented in the module repository: