This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
This is the @moleculer/database package - an advanced database access service for the Moleculer microservices framework. It provides a mixin that adds CRUD operations, field validation, data transformation, populating, scoping, multi-tenancy, and other database features to Moleculer services.
- Mixin Pattern: The service is implemented as a Moleculer mixin that other services can use
- Multi-Adapter Support: Supports NeDB, MongoDB, and Knex (SQL databases) adapters
- Field-Based Schema: Uses a comprehensive field definition system similar to Fastest Validator
- Transformation Pipeline: Data flows through validation → transformation → storage → retrieval → transformation
src/index.js- Main mixin factory function with lifecycle hookssrc/actions.js- Auto-generated CRUD actions (find, list, get, create, update, etc.)src/methods.js- Core database methods used by actions and custom implementationssrc/validation.js- Field validation and sanitization logicsrc/transform.js- Data transformation (encoding/decoding, populating, field filtering)src/adapters/- Database adapter implementations (base, mongodb, knex, nedb)src/schema.js- Field schema processing and validation schema generation
src/adapters/base.js- Abstract base adaptersrc/adapters/mongodb.js- MongoDB adaptersrc/adapters/knex.js- SQL databases via Knex.jssrc/adapters/nedb.js- NeDB (in-memory/file-based) adapter
npm test- Run all tests (unit, integration, leak detection)npm run test:unit- Run unit tests onlynpm run test:integration- Run integration tests with coveragenpm run test:leak- Run memory leak detection testsnpm run ci:unit- Watch mode for unit testsnpm run ci:integration- Watch mode for integration testsnpm run ci:leak- Watch mode for leak detection tests
npm run dev- Start example service in development modenpm run lint- Run ESLint on source code, examples, and testsnpm run bench- Run benchmarksnpm run bench:watch- Run benchmarks in watch mode
npm run deps- Check and update dependencies interactivelynpm run ci-update-deps- Auto-update minor version dependencies
When working with database functionality, understand that:
- NeDB is used for development/testing (default if no adapter specified)
- MongoDB supports full document operations and nested field querying
- Knex supports SQL databases (MySQL, PostgreSQL, SQLite, etc.) but converts objects/arrays to JSON strings
The service uses a comprehensive field definition system. Each field can have:
- Basic validation properties (type, required, min, max, etc.)
- Database properties (columnName, columnType, primaryKey)
- Permission properties (readPermission, permission)
- Lifecycle hooks (onCreate, onUpdate, onRemove)
- Transformation functions (get, set, validate)
- Populate configurations for relationships
The service supports three multi-tenancy modes:
- Record-based: Same table with tenant ID field + scopes
- Table-based: Different tables per tenant
- Database-based: Different databases per tenant
Implement via the getAdapterByContext method to customize adapter creation per context.
When testing services that use this mixin:
- Use NeDB adapter for simple unit tests (no external dependencies)
- Use real databases for integration tests (see
test/docker-compose.yml) - Test both single operations and batch operations
- Test scoping, populating, and multi-tenancy if used
- Check soft delete behavior if implemented
const DbService = require("@moleculer/database").Service;
module.exports = {
name: "posts",
mixins: [DbService({ adapter: "MongoDB" })],
settings: {
fields: {
id: { type: "string", primaryKey: true, columnName: "_id" },
title: { type: "string", required: true },
// ... more fields
}
}
}Services often implement custom methods that use the built-in methods like findEntities, createEntity, updateEntity, etc.
Adapters connect lazily on first use, supporting multi-tenancy scenarios where connection details depend on context.