Skip to content

feat: add MongoDB driver - #113

Open
Siyet wants to merge 4 commits into
trunkfrom
10-mongodb-driver
Open

feat: add MongoDB driver#113
Siyet wants to merge 4 commits into
trunkfrom
10-mongodb-driver

Conversation

@Siyet

@Siyet Siyet commented Apr 25, 2026

Copy link
Copy Markdown
Owner

Summary

  • Add MongoDriver implementing DatabaseDriver interface for MongoDB — connect, browse collections, execute shell-like commands (db.collection.find/aggregate/insert/update/delete), view DDL (indexes + validation rules), autocomplete, statistics
  • Connection form updated with MongoDB option and authentication database field
  • Webpack IgnorePlugin entries for mongodb optional peer dependencies (encryption, aws4, snappy, kerberos, zstd)

Closes #10

Assumptions

  • Field types are inferred by sampling the first 100 documents in each collection (no schema introspection since MongoDB is schemaless). This may miss fields present only in later documents.
  • Nested documents are flattened for tabular display: sub-objects become JSON strings, arrays become JSON strings, ObjectIds become hex strings. Deep nested expansion is not implemented.
  • The execute() command parser requires db.<collection>.<operation>(<JSON args>) syntax. Bare collection names are a shorthand for find({}). MongoDB aggregation framework syntax with $ operators works when properly JSON-quoted.
  • Authentication database defaults to admin (standard MongoDB convention).
  • No proxy/tunnel support for MongoDB in this PR (SSH tunneling through the existing tunnel module could be added later).

Manual test cases

  • Create a MongoDB connection with host/port/credentials → verify connection succeeds via Test Connection button
  • Browse the schema tree → verify databases/collections/fields appear with inferred types
  • Open a collection's data → verify documents display in the result grid with pagination
  • Execute db.users.find({"age": {"$gt": 25}}) → verify filtered results
  • Execute db.users.aggregate([{"$group": {"_id": "$city", "count": {"$sum": 1}}}]) → verify aggregation
  • Execute db.users.insertOne({"name": "Test"}) → verify acknowledgement result
  • Check DDL for a collection with indexes → verify index definitions shown
  • View collection statistics → verify document count, sizes, index info
  • Change database type in connection form → verify MongoDB-specific fields (auth DB) show/hide correctly
  • Connect with wrong credentials → verify clear error message

Checklist

  • README — not updated (no README changes needed for driver addition per existing pattern)
  • CHANGELOG — updated with MongoDB driver entry
  • CLAUDE.md — updated drivers list and added MongoDB conventions
  • l10n — updated extension description to include MongoDB
  • Wiki — new driver page needed (noted here, not pushed)

Generated by Claude Code

claude added 4 commits April 25, 2026 12:39
Implement MongoDriver with full DatabaseDriver interface support:
- connect/disconnect/ping via official mongodb npm package
- execute() parses db.collection.operation() shell syntax (find, findOne,
  aggregate, countDocuments, distinct, insert/update/delete, createIndex)
- getSchema() lists collections with field types inferred from sampling
- getTableData() with server-side pagination and sorting
- getDDL() shows indexes and schema validation rules
- getCompletions() for collection/field autocomplete
- getTableObjects() exposes index metadata
- getTableStatistics() via collStats command
- getEstimatedRowCount()/getTableRowCount() for fast/exact counts

Connection form: MongoDB option with authentication database field.
Webpack: IgnorePlugin for mongodb optional peer dependencies.
Tests: parseMongoCommand unit tests + driver contract spec.

Closes #10

https://claude.ai/code/session_01TmxFgxzSzoa1BrV6urdbPk
…hens in collection names

- Remove unused `config` field from MongoDriver (stored but never read)
- Fix relaxed JSON parser: protect string literals before quoting bare
  keys so colons inside values (e.g. "test: value") are not corrupted
- Expand collection name regex to allow hyphens (e.g. my-collection)

https://claude.ai/code/session_013cvrZFra3212F4iH8jmPy3
- Hyphenated collection names (db.my-collection.find)
- Hyphenated shorthand (my-events)
- Unquoted keys with colons in string values
- Unquoted keys with multiple args

https://claude.ai/code/session_013cvrZFra3212F4iH8jmPy3

@Siyet Siyet left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixes pushed

  • fa4d32c — Remove dead config field from MongoDriver (stored in connect() but never read), fix parseArgs relaxed JSON regex that corrupted string values containing colons (e.g. {name: "test: value"} → mangled the value), expand collection name regex to allow hyphens (db.my-collection.find() was rejected).
  • d43cf4e — Add 4 edge case tests for the mongo command parser: hyphenated collection names (full + shorthand), unquoted keys with colons in string values, unquoted keys with multiple args.

Issues to address

  • src/drivers/mongodb.ts:429inferBsonType classifies any 24-char hex string as ObjectId. After flattenDocument converts real ObjectIds to hex strings, the type heuristic can't distinguish them from other 24-char hex values (e.g. hash prefixes). This makes the schema column type hint misleading for non-ObjectId hex fields.
  • src/drivers/mongodb.ts:450inferColumns uses only the first occurrence of each column to determine its type. If the first row has null for a field and the second row has a string, the column type will be null. Consider iterating all rows or using the sampleFields approach used elsewhere in the driver.

Needs maintainer

  • No MongoDB-specific readonly enforcement exists. The command layer's readonly checks are SQL-specific (SELECT/EXPLAIN/SHOW/WITH). A MongoDB connection marked readonly will still allow insertOne, updateMany, deleteMany, etc. via execute(). Fixing this requires deciding which mongo operations are "read" vs "write" and wiring that into the command layer or the driver itself — touches the readonly contract across modules.

Generated by Claude Code

@Siyet Siyet added the reviewed label Apr 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat: add MongoDB driver

2 participants