|
| 1 | +# AGENTS.md |
| 2 | + |
| 3 | +This file provides guidance to AI coding agents (Claude Code, Codex, Copilot, Cursor, Aider, etc.) when working with code in this repository. |
| 4 | + |
| 5 | +## Project Overview |
| 6 | + |
| 7 | +This is the official MongoDB Node.js driver (`mongodb` npm package). It provides a TypeScript/JavaScript interface for applications to interact with MongoDB deployments. The driver implements the cross-driver MongoDB specifications. |
| 8 | + |
| 9 | +## Related Repositories |
| 10 | + |
| 11 | +- **[mongodb/specifications](https://github.com/mongodb/specifications)** — Cross-driver MongoDB specifications. **This is the source of truth** for behavior the driver must implement (CRUD, SDAM, CMAP, retryable reads/writes, sessions, transactions, change streams, CSFLE, etc.). Spec test fixtures (YAML/JSON) are vendored under `test/spec/`. When behavior is ambiguous, the spec wins; do not change behavior away from the spec without raising it there first. |
| 12 | +- **[mongodb/js-bson](https://github.com/mongodb/js-bson)** — BSON serialization (`bson` npm package). Owned by this team. Bug reports and changes that touch BSON encoding/decoding belong there. |
| 13 | +- **[mongodb-js/kerberos](https://github.com/mongodb-js/kerberos)** — Native Kerberos bindings (`kerberos` npm package, optional dependency). Owned by this team. GSSAPI auth issues are typically fixed there, not in the driver. |
| 14 | +- **[mongodb-js/mongodb-client-encryption](https://github.com/mongodb-js/mongodb-client-encryption)** — Native bindings to `libmongocrypt` for CSFLE/Queryable Encryption (`mongodb-client-encryption` npm package). Owned by this team. |
| 15 | +- **[mongodb-js/zstd](https://github.com/mongodb-js/zstd)** and **[mongodb-js/saslprep](https://github.com/mongodb-js/saslprep)** — Compression and SCRAM SASLprep helpers, also team-owned. |
| 16 | + |
| 17 | +**Do not hand-edit**: `lib/` (build output), `mongodb.d.ts` (generated), `HISTORY.md` (release-please managed), `test/spec/` (vendored from specifications repo). |
| 18 | + |
| 19 | +## Common Commands |
| 20 | + |
| 21 | +### Building |
| 22 | + |
| 23 | +```bash |
| 24 | +npm run build:ts # Compile TypeScript to lib/ |
| 25 | +npm run check:ts # Type-check without emitting |
| 26 | +``` |
| 27 | + |
| 28 | +### Linting |
| 29 | + |
| 30 | +```bash |
| 31 | +npm run check:eslint # Run ESLint |
| 32 | +npm run fix:eslint # Auto-fix ESLint issues |
| 33 | +``` |
| 34 | + |
| 35 | +### Testing |
| 36 | + |
| 37 | +Integration tests require a running MongoDB instance (unit tests do not). To start one locally: |
| 38 | + |
| 39 | +```bash |
| 40 | +git submodule update --init |
| 41 | +export DRIVERS_TOOLS=$(pwd)/drivers-evergreen-tools |
| 42 | +VERSION='latest' TOPOLOGY='replica_set' bash .evergreen/run-orchestration.sh |
| 43 | +source mo-expansion.sh |
| 44 | +``` |
| 45 | + |
| 46 | +```bash |
| 47 | +npm run check:unit # Unit tests (no database required) |
| 48 | +npm run check:test # Integration tests (requires database) |
| 49 | +npm test # Full check (lint + d.ts/tsd) + unit + integration |
| 50 | + |
| 51 | +# Run a single test by name pattern |
| 52 | +npm run check:unit -- -g "pattern" |
| 53 | +npm run check:test -- -g "pattern" |
| 54 | +``` |
| 55 | + |
| 56 | +Tests use Mocha with 60-second timeout. Integration tests use a custom metadata UI that supports test filtering by topology, MongoDB version, auth, etc. via metadata: |
| 57 | + |
| 58 | +```js |
| 59 | +describe( |
| 60 | + 'my test', |
| 61 | + { metadata: { requires: { topology: ['replicaset'], mongodb: '>=6.0' } } }, |
| 62 | + function () {} |
| 63 | +); |
| 64 | +``` |
| 65 | + |
| 66 | +## Architecture |
| 67 | + |
| 68 | +### Layered Design |
| 69 | + |
| 70 | +``` |
| 71 | +Public API (MongoClient, Db, Collection, Cursors) |
| 72 | + → Operations (CRUD, Aggregation, Indexes, Bulk writes) |
| 73 | + → Sessions & Transactions |
| 74 | + → SDAM – Server Discovery And Monitoring (src/sdam/) |
| 75 | + → CMAP – Connection Management And Pooling (src/cmap/) |
| 76 | + → Wire Protocol & BSON serialization |
| 77 | +``` |
| 78 | + |
| 79 | +### Key Source Directories |
| 80 | + |
| 81 | +- **`src/operations/`** — Each database command is an `AbstractOperation` subclass. Operations declare aspects (retryable, read/write, explainable) via Symbols. `execute_operation.ts` is the central execution engine handling retries, sessions, server selection. |
| 82 | +- **`src/sdam/`** — Topology discovery and monitoring. `topology.ts` manages servers, `server_selection.ts` picks the best server based on read preference and latency, `monitor.ts` sends periodic heartbeats. |
| 83 | +- **`src/cmap/`** — Connection pooling per server, wire protocol encoding/decoding, authentication handshakes. `auth/` contains implementations for each auth mechanism (SCRAM, X.509, AWS, OIDC, Kerberos, PLAIN). |
| 84 | +- **`src/cursor/`** — `AbstractCursor` base with lazy evaluation, async iteration, and streaming. Specialized cursors: `FindCursor`, `AggregationCursor`, `ChangeStreamCursor`, etc. |
| 85 | +- **`src/bulk/`** — Ordered and unordered bulk write operations. |
| 86 | +- **`src/client-side-encryption/`** — Auto-encryption and explicit encryption (CSFLE/Queryable Encryption). |
| 87 | +- **`src/gridfs/`** — GridFS file storage using upload/download streams. |
| 88 | + |
| 89 | +### How Operations Execute |
| 90 | + |
| 91 | +1. User calls a method (e.g., `collection.insertOne()`) |
| 92 | +2. An operation object is created (e.g., `InsertOperation`) |
| 93 | +3. `executeOperation()` handles: implicit session creation → server selection → connection checkout → command building → wire protocol send → response handling → retry on transient errors |
| 94 | +4. Connection returned to pool, session cleaned up |
| 95 | + |
| 96 | +### Test Structure |
| 97 | + |
| 98 | +- **`test/unit/`** — Mirrors `src/` structure. No database interaction, uses mocks. |
| 99 | +- **`test/integration/`** — Real database tests organized by feature area. |
| 100 | +- **`test/spec/`** — YAML/JSON test specifications from the cross-driver specs. Implemented by spec runners in integration tests. Files named `*.spec.test.ts` use standardized runners; `*.prose.test.ts` are hand-written prose test implementations. |
| 101 | +- **`test/mongodb.ts`** — Central re-export of all `src/` internals for test access. Tests import from `../../mongodb` (or appropriate depth), never directly from `src/`. |
| 102 | + |
| 103 | +## Code Conventions |
| 104 | + |
| 105 | +- **Public API stability** — Anything exported from `src/index.ts` flows into the published `mongodb.d.ts` via api-extractor. Renaming, removing, or narrowing exported types/signatures is a breaking change; confirm with a maintainer before doing so. |
| 106 | +- **No `export default`** — All exports must be named. |
| 107 | +- **No TypeScript enums** — Use string unions or `as const` objects instead. |
| 108 | +- **`src/`: no `node:` import prefix** — In source files, use bare module names (e.g., `import { setTimeout } from 'timers'`). Tests may use `node:` imports where allowed by the repo config. |
| 109 | +- **`src/`: timer/process imports** — In source files, import `setTimeout`, `setInterval`, `clearTimeout`, `process`, etc. from their modules instead of using globals. |
| 110 | +- **No `Buffer`** — Use `Uint8Array` in source code. |
| 111 | +- **BSON imports** — Source code must import from `src/bson.ts`, not from the `bson` package directly. |
| 112 | +- **Null/undefined checks** — Use loose equality (`== null`) not strict (`=== null` or `=== undefined`). |
| 113 | +- **Type imports** — Use `import { type Foo }` (inline type imports). |
| 114 | +- **`return await`** — Required in `src/` (enforced by `@typescript-eslint/return-await: always`). |
| 115 | +- **Error messages** — Sentence case, no trailing period. Use driver-specific error types extending `MongoError`. |
| 116 | +- **Formatting** — Prettier with single quotes, 2-space indentation, 100-char width, no trailing commas. |
| 117 | + |
| 118 | +## Commit Messages |
| 119 | + |
| 120 | +Follow [Conventional Commits](https://www.conventionalcommits.org/): `<type>(NODE-XXXX): <subject>` |
| 121 | + |
| 122 | +Types: `feat`, `fix`, `docs`, `style`, `refactor`, `perf`, `test`, `chore` |
| 123 | + |
| 124 | +Breaking changes use `!`: `feat(NODE-XXXX)!: description` |
0 commit comments