Skip to content

Commit cf1ee99

Browse files
committed
add claude.md
1 parent 8b4f918 commit cf1ee99

File tree

1 file changed

+111
-0
lines changed

1 file changed

+111
-0
lines changed

CLAUDE.md

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
# CLAUDE.md
2+
3+
This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
4+
5+
## Project Overview
6+
7+
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.
8+
9+
## Key Architecture Components
10+
11+
### Core Structure
12+
- **Mixin Pattern**: The service is implemented as a Moleculer mixin that other services can use
13+
- **Multi-Adapter Support**: Supports NeDB, MongoDB, and Knex (SQL databases) adapters
14+
- **Field-Based Schema**: Uses a comprehensive field definition system similar to Fastest Validator
15+
- **Transformation Pipeline**: Data flows through validation → transformation → storage → retrieval → transformation
16+
17+
### Main Files
18+
- `src/index.js` - Main mixin factory function with lifecycle hooks
19+
- `src/actions.js` - Auto-generated CRUD actions (find, list, get, create, update, etc.)
20+
- `src/methods.js` - Core database methods used by actions and custom implementations
21+
- `src/validation.js` - Field validation and sanitization logic
22+
- `src/transform.js` - Data transformation (encoding/decoding, populating, field filtering)
23+
- `src/adapters/` - Database adapter implementations (base, mongodb, knex, nedb)
24+
- `src/schema.js` - Field schema processing and validation schema generation
25+
26+
### Adapter Pattern
27+
- `src/adapters/base.js` - Abstract base adapter
28+
- `src/adapters/mongodb.js` - MongoDB adapter
29+
- `src/adapters/knex.js` - SQL databases via Knex.js
30+
- `src/adapters/nedb.js` - NeDB (in-memory/file-based) adapter
31+
32+
## Development Commands
33+
34+
### Testing
35+
- `npm test` - Run all tests (unit, integration, leak detection)
36+
- `npm run test:unit` - Run unit tests only
37+
- `npm run test:integration` - Run integration tests with coverage
38+
- `npm run test:leak` - Run memory leak detection tests
39+
- `npm run ci:unit` - Watch mode for unit tests
40+
- `npm run ci:integration` - Watch mode for integration tests
41+
- `npm run ci:leak` - Watch mode for leak detection tests
42+
43+
### Development
44+
- `npm run dev` - Start example service in development mode
45+
- `npm run lint` - Run ESLint on source code, examples, and tests
46+
- `npm run bench` - Run benchmarks
47+
- `npm run bench:watch` - Run benchmarks in watch mode
48+
49+
### Maintenance
50+
- `npm run deps` - Check and update dependencies interactively
51+
- `npm run ci-update-deps` - Auto-update minor version dependencies
52+
53+
## Database Adapters
54+
55+
When working with database functionality, understand that:
56+
- **NeDB** is used for development/testing (default if no adapter specified)
57+
- **MongoDB** supports full document operations and nested field querying
58+
- **Knex** supports SQL databases (MySQL, PostgreSQL, SQLite, etc.) but converts objects/arrays to JSON strings
59+
60+
## Field Definitions
61+
62+
The service uses a comprehensive field definition system. Each field can have:
63+
- Basic validation properties (type, required, min, max, etc.)
64+
- Database properties (columnName, columnType, primaryKey)
65+
- Permission properties (readPermission, permission)
66+
- Lifecycle hooks (onCreate, onUpdate, onRemove)
67+
- Transformation functions (get, set, validate)
68+
- Populate configurations for relationships
69+
70+
## Multi-Tenancy Support
71+
72+
The service supports three multi-tenancy modes:
73+
1. **Record-based**: Same table with tenant ID field + scopes
74+
2. **Table-based**: Different tables per tenant
75+
3. **Database-based**: Different databases per tenant
76+
77+
Implement via the `getAdapterByContext` method to customize adapter creation per context.
78+
79+
## Testing Integration Services
80+
81+
When testing services that use this mixin:
82+
- Use NeDB adapter for simple unit tests (no external dependencies)
83+
- Use real databases for integration tests (see `test/docker-compose.yml`)
84+
- Test both single operations and batch operations
85+
- Test scoping, populating, and multi-tenancy if used
86+
- Check soft delete behavior if implemented
87+
88+
## Common Patterns
89+
90+
### Service Implementation
91+
```javascript
92+
const DbService = require("@moleculer/database").Service;
93+
94+
module.exports = {
95+
name: "posts",
96+
mixins: [DbService({ adapter: "MongoDB" })],
97+
settings: {
98+
fields: {
99+
id: { type: "string", primaryKey: true, columnName: "_id" },
100+
title: { type: "string", required: true },
101+
// ... more fields
102+
}
103+
}
104+
}
105+
```
106+
107+
### Custom Methods
108+
Services often implement custom methods that use the built-in methods like `findEntities`, `createEntity`, `updateEntity`, etc.
109+
110+
### Adapter Connection
111+
Adapters connect lazily on first use, supporting multi-tenancy scenarios where connection details depend on context.

0 commit comments

Comments
 (0)