2025-11-17 02:59:44 +00:00
|
|
|
# Project Hints
|
|
|
|
|
|
|
|
|
|
## Deno Compatibility
|
|
|
|
|
|
|
|
|
|
### mongodb-memory-server Import
|
|
|
|
|
- The package uses a **default import** for `mongodb-memory-server` in `ts/smartmongo.plugins.ts`
|
|
|
|
|
- Syntax: `import mongoPlugin from 'mongodb-memory-server';`
|
|
|
|
|
- This works in both Node.js and Deno environments
|
|
|
|
|
- **Why:** Deno wraps CommonJS exports in a `default` property, so default imports are required
|
|
|
|
|
- Fixed in version 2.0.13 (changed from `import * as mongoPlugin`)
|
2026-01-31 11:33:11 +00:00
|
|
|
|
2026-02-01 14:34:07 +00:00
|
|
|
## TsmDB - MongoDB Wire Protocol Server
|
2026-01-31 11:33:11 +00:00
|
|
|
|
|
|
|
|
### Architecture
|
2026-02-01 14:34:07 +00:00
|
|
|
TsmDB implements the MongoDB binary wire protocol (OP_MSG, OP_QUERY) allowing official MongoDB drivers to connect directly.
|
2026-01-31 11:33:11 +00:00
|
|
|
|
|
|
|
|
```
|
2026-02-01 14:34:07 +00:00
|
|
|
Official MongoClient → TCP (wire protocol) → TsmdbServer → Engines → Storage
|
2026-01-31 11:33:11 +00:00
|
|
|
(mongodb npm) OP_MSG/BSON (port)
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### Module Structure
|
|
|
|
|
```
|
2026-02-01 14:34:07 +00:00
|
|
|
ts/tsmdb/
|
2026-01-31 11:33:11 +00:00
|
|
|
├── server/ # Wire protocol server
|
2026-02-01 14:34:07 +00:00
|
|
|
│ ├── TsmdbServer.ts # TCP server, connection handling
|
2026-01-31 11:33:11 +00:00
|
|
|
│ ├── WireProtocol.ts # OP_MSG/OP_QUERY parsing & encoding
|
|
|
|
|
│ ├── CommandRouter.ts # Route commands to handlers
|
|
|
|
|
│ └── handlers/ # Command implementations
|
|
|
|
|
│ ├── HelloHandler.ts # hello/isMaster handshake
|
|
|
|
|
│ ├── FindHandler.ts # find, getMore, killCursors, count, distinct
|
|
|
|
|
│ ├── InsertHandler.ts # insert
|
|
|
|
|
│ ├── UpdateHandler.ts # update, findAndModify
|
|
|
|
|
│ ├── DeleteHandler.ts # delete
|
|
|
|
|
│ ├── AggregateHandler.ts # aggregate
|
|
|
|
|
│ ├── IndexHandler.ts # createIndexes, dropIndexes, listIndexes
|
|
|
|
|
│ └── AdminHandler.ts # ping, listDatabases, listCollections, etc.
|
|
|
|
|
│
|
|
|
|
|
├── engine/ # Core logic (reused)
|
|
|
|
|
│ ├── QueryEngine.ts # Query filtering with mingo
|
|
|
|
|
│ ├── UpdateEngine.ts # Update operations
|
|
|
|
|
│ ├── AggregationEngine.ts # Aggregation pipelines
|
|
|
|
|
│ ├── IndexEngine.ts # Index management
|
|
|
|
|
│ └── TransactionEngine.ts # Transaction support
|
|
|
|
|
│
|
|
|
|
|
├── storage/ # Storage layer
|
|
|
|
|
│ ├── IStorageAdapter.ts # Interface
|
|
|
|
|
│ ├── MemoryStorageAdapter.ts
|
|
|
|
|
│ └── FileStorageAdapter.ts
|
|
|
|
|
│
|
|
|
|
|
└── types/interfaces.ts # Type definitions
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### Usage Example
|
|
|
|
|
```typescript
|
2026-02-01 14:34:07 +00:00
|
|
|
import { TsmdbServer } from '@push.rocks/smartmongo/tsmdb';
|
2026-01-31 11:33:11 +00:00
|
|
|
import { MongoClient } from 'mongodb';
|
|
|
|
|
|
|
|
|
|
// Start server
|
2026-02-01 14:34:07 +00:00
|
|
|
const server = new TsmdbServer({ port: 27117 });
|
2026-01-31 11:33:11 +00:00
|
|
|
await server.start();
|
|
|
|
|
|
|
|
|
|
// Connect with official MongoDB driver
|
|
|
|
|
const client = new MongoClient('mongodb://127.0.0.1:27117', {
|
|
|
|
|
directConnection: true
|
|
|
|
|
});
|
|
|
|
|
await client.connect();
|
|
|
|
|
|
|
|
|
|
// Use like any MongoDB instance
|
|
|
|
|
const db = client.db('mydb');
|
|
|
|
|
await db.collection('users').insertOne({ name: 'John' });
|
|
|
|
|
const user = await db.collection('users').findOne({ name: 'John' });
|
|
|
|
|
|
|
|
|
|
// Cleanup
|
|
|
|
|
await client.close();
|
|
|
|
|
await server.stop();
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### Supported Commands
|
|
|
|
|
- **Handshake**: hello, isMaster
|
|
|
|
|
- **CRUD**: find, insert, update, delete, findAndModify, getMore, killCursors
|
|
|
|
|
- **Aggregation**: aggregate, count, distinct
|
|
|
|
|
- **Indexes**: createIndexes, dropIndexes, listIndexes
|
|
|
|
|
- **Admin**: ping, listDatabases, listCollections, drop, dropDatabase, create, serverStatus, buildInfo
|