|
|
|
|
@@ -1,6 +1,6 @@
|
|
|
|
|
# @push.rocks/smartmongo
|
|
|
|
|
|
|
|
|
|
A powerful MongoDB toolkit for testing and development — featuring a real MongoDB memory server (**SmartMongo**), an ultra-fast wire-protocol-compatible in-memory database server (**TsmDB**), and a zero-config local database (**LocalTsmDb**). 🚀
|
|
|
|
|
A powerful MongoDB toolkit for testing and development — featuring a real MongoDB memory server (**SmartMongo**), an ultra-fast wire-protocol-compatible in-memory database server (**TsmDB**), and a zero-config local database (**LocalTsmDb**).
|
|
|
|
|
|
|
|
|
|
## Install
|
|
|
|
|
|
|
|
|
|
@@ -21,17 +21,17 @@ For reporting bugs, issues, or security vulnerabilities, please visit [community
|
|
|
|
|
| Feature | SmartMongo | TsmDB | LocalTsmDb |
|
|
|
|
|
|---------|------------|-------|------------|
|
|
|
|
|
| **Type** | Real MongoDB (memory server) | Wire protocol server | Zero-config local DB |
|
|
|
|
|
| **Speed** | ~2-5s startup | ⚡ Instant (~5ms) | ⚡ Instant (Unix socket) |
|
|
|
|
|
| **Speed** | ~2-5s startup | Instant (~5ms) | Instant (Unix socket) |
|
|
|
|
|
| **Compatibility** | 100% MongoDB | MongoDB driver compatible | MongoDB driver compatible |
|
|
|
|
|
| **Dependencies** | Downloads MongoDB binary | Zero external deps | Zero external deps (no MongoDB driver!) |
|
|
|
|
|
| **Connection** | TCP | TCP or Unix socket | Unix socket (default) |
|
|
|
|
|
| **Replication** | ✅ Full replica set | Single node | Single node |
|
|
|
|
|
| **Replication** | Full replica set | Single node | Single node |
|
|
|
|
|
| **Persistence** | Dump to directory | Memory or file | File-based (automatic) |
|
|
|
|
|
| **Use Case** | Integration testing | Unit testing, CI/CD | Quick prototyping, local dev |
|
|
|
|
|
|
|
|
|
|
## 🚀 Quick Start
|
|
|
|
|
## Quick Start
|
|
|
|
|
|
|
|
|
|
### Option 1: LocalTsmDb (Zero-Config Local Database) ⭐
|
|
|
|
|
### Option 1: LocalTsmDb (Zero-Config Local Database)
|
|
|
|
|
|
|
|
|
|
The easiest way to get started — just point it at a folder and you have a persistent MongoDB-compatible database using Unix sockets. No port conflicts, no MongoDB driver dependency in LocalTsmDb!
|
|
|
|
|
|
|
|
|
|
@@ -120,7 +120,7 @@ await mongo.stop();
|
|
|
|
|
|
|
|
|
|
---
|
|
|
|
|
|
|
|
|
|
## 📖 LocalTsmDb API
|
|
|
|
|
## LocalTsmDb API
|
|
|
|
|
|
|
|
|
|
The simplest option for local development and prototyping — lightweight, Unix socket-based, and automatic persistence.
|
|
|
|
|
|
|
|
|
|
@@ -191,15 +191,15 @@ await db.stop();
|
|
|
|
|
|
|
|
|
|
### Features
|
|
|
|
|
|
|
|
|
|
- 🔌 **Unix Sockets** — No port conflicts, faster IPC than TCP
|
|
|
|
|
- 💾 **Automatic Persistence** — Data saved to files, survives restarts
|
|
|
|
|
- 🪶 **Lightweight** — No MongoDB driver dependency in LocalTsmDb itself
|
|
|
|
|
- 🎯 **Zero Config** — Just specify a folder path and you're good to go
|
|
|
|
|
- 🔗 **Connection URI** — Ready-to-use URI for your own MongoClient
|
|
|
|
|
- **Unix Sockets** — No port conflicts, faster IPC than TCP
|
|
|
|
|
- **Automatic Persistence** — Data saved to files, survives restarts
|
|
|
|
|
- **Lightweight** — No MongoDB driver dependency in LocalTsmDb itself
|
|
|
|
|
- **Zero Config** — Just specify a folder path and you're good to go
|
|
|
|
|
- **Connection URI** — Ready-to-use URI for your own MongoClient
|
|
|
|
|
|
|
|
|
|
---
|
|
|
|
|
|
|
|
|
|
## 📖 SmartMongo API
|
|
|
|
|
## SmartMongo API
|
|
|
|
|
|
|
|
|
|
Full MongoDB replica set in memory using `mongodb-memory-server`.
|
|
|
|
|
|
|
|
|
|
@@ -225,7 +225,7 @@ const descriptor = await mongo.getMongoDescriptor();
|
|
|
|
|
// }
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### Stopping & Cleanup
|
|
|
|
|
### Stopping and Cleanup
|
|
|
|
|
|
|
|
|
|
```typescript
|
|
|
|
|
// Simple stop (data discarded)
|
|
|
|
|
@@ -240,7 +240,7 @@ await mongo.stopAndDumpToDir('./test-data', (doc) => `${doc.collection}-${doc._i
|
|
|
|
|
|
|
|
|
|
---
|
|
|
|
|
|
|
|
|
|
## 🔧 TsmDB API
|
|
|
|
|
## TsmDB API
|
|
|
|
|
|
|
|
|
|
Pure TypeScript MongoDB wire protocol server. No external dependencies.
|
|
|
|
|
|
|
|
|
|
@@ -284,7 +284,7 @@ await server.stop();
|
|
|
|
|
|
|
|
|
|
TsmDB supports the core MongoDB operations via the wire protocol:
|
|
|
|
|
|
|
|
|
|
#### 🔹 CRUD Operations
|
|
|
|
|
#### CRUD Operations
|
|
|
|
|
|
|
|
|
|
```typescript
|
|
|
|
|
// Insert
|
|
|
|
|
@@ -314,7 +314,7 @@ const result = await collection.findOneAndUpdate(
|
|
|
|
|
);
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### 🔹 Query Operators
|
|
|
|
|
#### Query Operators
|
|
|
|
|
|
|
|
|
|
```typescript
|
|
|
|
|
// Comparison
|
|
|
|
|
@@ -344,7 +344,7 @@ const result = await collection.findOneAndUpdate(
|
|
|
|
|
{ email: { $regex: '@example\\.com$' } }
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### 🔹 Update Operators
|
|
|
|
|
#### Update Operators
|
|
|
|
|
|
|
|
|
|
```typescript
|
|
|
|
|
{ $set: { name: 'New Name' } }
|
|
|
|
|
@@ -362,7 +362,7 @@ const result = await collection.findOneAndUpdate(
|
|
|
|
|
{ $currentDate: { lastModified: true } }
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### 🔹 Aggregation Pipeline
|
|
|
|
|
#### Aggregation Pipeline
|
|
|
|
|
|
|
|
|
|
```typescript
|
|
|
|
|
const results = await collection.aggregate([
|
|
|
|
|
@@ -378,13 +378,12 @@ const results = await collection.aggregate([
|
|
|
|
|
|
|
|
|
|
**Supported group accumulators:** `$sum`, `$avg`, `$min`, `$max`, `$first`, `$last`, `$push`, `$addToSet`, `$count`.
|
|
|
|
|
|
|
|
|
|
#### 🔹 Index Operations
|
|
|
|
|
#### Index Operations
|
|
|
|
|
|
|
|
|
|
```typescript
|
|
|
|
|
// Create indexes
|
|
|
|
|
await collection.createIndex({ email: 1 }, { unique: true });
|
|
|
|
|
await collection.createIndex({ name: 1, age: -1 });
|
|
|
|
|
await collection.createIndex({ location: '2dsphere' }); // Geospatial
|
|
|
|
|
|
|
|
|
|
// List indexes
|
|
|
|
|
const indexes = await collection.listIndexes().toArray();
|
|
|
|
|
@@ -394,7 +393,7 @@ await collection.dropIndex('email_1');
|
|
|
|
|
await collection.dropIndexes(); // Drop all except _id
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### 🔹 Database Operations
|
|
|
|
|
#### Database Operations
|
|
|
|
|
|
|
|
|
|
```typescript
|
|
|
|
|
// List databases
|
|
|
|
|
@@ -414,7 +413,7 @@ await db.dropDatabase();
|
|
|
|
|
const stats = await db.stats();
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### 🔹 Count & Distinct
|
|
|
|
|
#### Count and Distinct
|
|
|
|
|
|
|
|
|
|
```typescript
|
|
|
|
|
// Count documents
|
|
|
|
|
@@ -427,7 +426,7 @@ const departments = await collection.distinct('department');
|
|
|
|
|
const activeDepts = await collection.distinct('department', { status: 'active' });
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### 🔹 Bulk Operations
|
|
|
|
|
#### Bulk Operations
|
|
|
|
|
|
|
|
|
|
```typescript
|
|
|
|
|
const result = await collection.bulkWrite([
|
|
|
|
|
@@ -466,11 +465,11 @@ const server = new tsmdb.TsmdbServer({
|
|
|
|
|
|
|
|
|
|
---
|
|
|
|
|
|
|
|
|
|
## ⚡ Performance & Reliability Features
|
|
|
|
|
## Performance and Reliability Features
|
|
|
|
|
|
|
|
|
|
TsmDB includes enterprise-grade features for robustness:
|
|
|
|
|
|
|
|
|
|
### 🔍 Index-Accelerated Queries
|
|
|
|
|
### Index-Accelerated Queries
|
|
|
|
|
|
|
|
|
|
Indexes are automatically used to accelerate queries:
|
|
|
|
|
|
|
|
|
|
@@ -487,7 +486,7 @@ await collection.findOne({ email: 'alice@example.com' }); // Uses hash lookup
|
|
|
|
|
await collection.find({ age: { $gte: 18, $lt: 65 } }); // Uses B-tree range scan
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### 📊 Query Planner
|
|
|
|
|
### Query Planner
|
|
|
|
|
|
|
|
|
|
TsmDB includes a query planner that analyzes queries and selects optimal execution strategies:
|
|
|
|
|
|
|
|
|
|
@@ -507,7 +506,7 @@ console.log(plan);
|
|
|
|
|
// }
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### 📝 Write-Ahead Logging (WAL)
|
|
|
|
|
### Write-Ahead Logging (WAL)
|
|
|
|
|
|
|
|
|
|
For durability, TsmDB supports write-ahead logging:
|
|
|
|
|
|
|
|
|
|
@@ -528,7 +527,7 @@ await wal.initialize();
|
|
|
|
|
const entries = wal.getEntriesAfter(lastCheckpointLsn);
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### 🔐 Session Management
|
|
|
|
|
### Session Management
|
|
|
|
|
|
|
|
|
|
TsmDB tracks client sessions with automatic timeout and transaction linking:
|
|
|
|
|
|
|
|
|
|
@@ -553,7 +552,7 @@ try {
|
|
|
|
|
// - Session activity tracking
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### ✅ Data Integrity Checksums
|
|
|
|
|
### Data Integrity Checksums
|
|
|
|
|
|
|
|
|
|
File-based storage supports CRC32 checksums to detect corruption:
|
|
|
|
|
|
|
|
|
|
@@ -566,7 +565,7 @@ const checksum = tsmdb.calculateDocumentChecksum(doc);
|
|
|
|
|
const isValid = tsmdb.verifyChecksum(docWithChecksum);
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### 📋 Supported Wire Protocol Commands
|
|
|
|
|
### Supported Wire Protocol Commands
|
|
|
|
|
|
|
|
|
|
| Category | Commands |
|
|
|
|
|
|----------|----------|
|
|
|
|
|
@@ -576,13 +575,13 @@ const isValid = tsmdb.verifyChecksum(docWithChecksum);
|
|
|
|
|
| **Indexes** | `createIndexes`, `dropIndexes`, `listIndexes` |
|
|
|
|
|
| **Transactions** | `startTransaction`, `commitTransaction`, `abortTransaction` |
|
|
|
|
|
| **Sessions** | `startSession`, `endSessions`, `refreshSessions` |
|
|
|
|
|
| **Admin** | `ping`, `listDatabases`, `listCollections`, `drop`, `dropDatabase`, `create`, `serverStatus`, `buildInfo`, `dbStats`, `collStats`, `connectionStatus` |
|
|
|
|
|
| **Admin** | `ping`, `listDatabases`, `listCollections`, `drop`, `dropDatabase`, `create`, `serverStatus`, `buildInfo`, `dbStats`, `collStats`, `connectionStatus`, `currentOp`, `collMod`, `renameCollection` |
|
|
|
|
|
|
|
|
|
|
TsmDB supports MongoDB wire protocol versions 0-21, compatible with MongoDB 3.6 through 7.0 drivers.
|
|
|
|
|
|
|
|
|
|
---
|
|
|
|
|
|
|
|
|
|
## 🧪 Testing Examples
|
|
|
|
|
## Testing Examples
|
|
|
|
|
|
|
|
|
|
### Jest/Mocha with LocalTsmDb
|
|
|
|
|
|
|
|
|
|
@@ -705,15 +704,15 @@ export default tap.start();
|
|
|
|
|
|
|
|
|
|
---
|
|
|
|
|
|
|
|
|
|
## 🏗️ Architecture
|
|
|
|
|
## Architecture
|
|
|
|
|
|
|
|
|
|
### Module Structure
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
@push.rocks/smartmongo
|
|
|
|
|
├── SmartMongo → Real MongoDB memory server (mongodb-memory-server wrapper)
|
|
|
|
|
├── tsmdb → Wire protocol server with full engine stack
|
|
|
|
|
└── LocalTsmDb → Lightweight Unix socket wrapper (no MongoDB driver dependency)
|
|
|
|
|
├── SmartMongo -> Real MongoDB memory server (mongodb-memory-server wrapper)
|
|
|
|
|
├── tsmdb -> Wire protocol server with full engine stack
|
|
|
|
|
└── LocalTsmDb -> Lightweight Unix socket wrapper (no MongoDB driver dependency)
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### TsmDB Wire Protocol Stack
|
|
|
|
|
@@ -724,16 +723,16 @@ export default tap.start();
|
|
|
|
|
│ (mongodb npm) │
|
|
|
|
|
└─────────────────────────┬───────────────────────────────────┘
|
|
|
|
|
│ TCP/Unix Socket + OP_MSG/BSON
|
|
|
|
|
▼
|
|
|
|
|
v
|
|
|
|
|
┌─────────────────────────────────────────────────────────────┐
|
|
|
|
|
│ TsmdbServer │
|
|
|
|
|
│ ┌──────────────┐ ┌──────────────┐ ┌──────────────────┐ │
|
|
|
|
|
│ │ WireProtocol │→ │CommandRouter │→ │ Handlers │ │
|
|
|
|
|
│ │ WireProtocol │->│CommandRouter │->│ Handlers │ │
|
|
|
|
|
│ │ (OP_MSG) │ │ │ │ (Find, Insert..) │ │
|
|
|
|
|
│ └──────────────┘ └──────────────┘ └──────────────────┘ │
|
|
|
|
|
└─────────────────────────┬───────────────────────────────────┘
|
|
|
|
|
│
|
|
|
|
|
▼
|
|
|
|
|
v
|
|
|
|
|
┌─────────────────────────────────────────────────────────────┐
|
|
|
|
|
│ Engines │
|
|
|
|
|
│ ┌─────────┐ ┌────────┐ ┌───────────┐ ┌───────┐ ┌───────┐ │
|
|
|
|
|
@@ -745,7 +744,7 @@ export default tap.start();
|
|
|
|
|
│ └──────────────────────┘ │
|
|
|
|
|
└─────────────────────────┬───────────────────────────────────┘
|
|
|
|
|
│
|
|
|
|
|
▼
|
|
|
|
|
v
|
|
|
|
|
┌─────────────────────────────────────────────────────────────┐
|
|
|
|
|
│ Storage Layer │
|
|
|
|
|
│ ┌──────────────────┐ ┌──────────────────┐ ┌──────────┐ │
|
|
|
|
|
@@ -762,33 +761,32 @@ export default tap.start();
|
|
|
|
|
| **WireProtocol** | Parses MongoDB OP_MSG binary protocol |
|
|
|
|
|
| **CommandRouter** | Routes commands to appropriate handlers |
|
|
|
|
|
| **QueryPlanner** | Analyzes queries and selects execution strategy |
|
|
|
|
|
| **QueryEngine** | Executes queries with filter matching |
|
|
|
|
|
| **QueryEngine** | Executes queries with filter matching via mingo |
|
|
|
|
|
| **UpdateEngine** | Processes update operators (`$set`, `$inc`, etc.) |
|
|
|
|
|
| **AggregationEngine** | Executes aggregation pipelines |
|
|
|
|
|
| **AggregationEngine** | Executes aggregation pipelines via mingo |
|
|
|
|
|
| **IndexEngine** | Manages B-tree and hash indexes |
|
|
|
|
|
| **SessionEngine** | Tracks client sessions and timeouts |
|
|
|
|
|
| **TransactionEngine** | Handles ACID transaction semantics |
|
|
|
|
|
| **WAL** | Write-ahead logging for durability |
|
|
|
|
|
| **TransactionEngine** | Handles ACID transaction semantics with snapshot isolation |
|
|
|
|
|
| **WAL** | Write-ahead logging for durability and crash recovery |
|
|
|
|
|
| **OpLog** | Operation log for change tracking and streaming |
|
|
|
|
|
|
|
|
|
|
---
|
|
|
|
|
|
|
|
|
|
## License and Legal Information
|
|
|
|
|
|
|
|
|
|
This repository contains open-source code licensed under the MIT License. A copy of the license can be found in the [LICENSE](./LICENSE) file.
|
|
|
|
|
This repository contains open-source code that is licensed under the MIT License. A copy of the MIT License can be found in the [license](license) file within this repository.
|
|
|
|
|
|
|
|
|
|
**Please note:** The MIT License does not grant permission to use the trade names, trademarks, service marks, or product names of the project, except as required for reasonable and customary use in describing the origin of the work and reproducing the content of the NOTICE file.
|
|
|
|
|
|
|
|
|
|
### Trademarks
|
|
|
|
|
|
|
|
|
|
This project is owned and maintained by Task Venture Capital GmbH. The names and logos associated with Task Venture Capital GmbH and any related products or services are trademarks of Task Venture Capital GmbH or third parties, and are not included within the scope of the MIT license granted herein.
|
|
|
|
|
|
|
|
|
|
Use of these trademarks must comply with Task Venture Capital GmbH's Trademark Guidelines or the guidelines of the respective third-party owners, and any usage must be approved in writing. Third-party trademarks used herein are the property of their respective owners and used only in a descriptive manner, e.g. for an implementation of an API or similar.
|
|
|
|
|
This project is owned and maintained by Task Venture Capital GmbH. The names and logos associated with Task Venture Capital GmbH and any related products or services are trademarks of Task Venture Capital GmbH and are not included within the scope of the MIT license granted herein. Use of these trademarks must comply with Task Venture Capital GmbH's Trademark Guidelines, and any usage must be approved in writing by Task Venture Capital GmbH.
|
|
|
|
|
|
|
|
|
|
### Company Information
|
|
|
|
|
|
|
|
|
|
Task Venture Capital GmbH
|
|
|
|
|
Registered at District Court Bremen HRB 35230 HB, Germany
|
|
|
|
|
Registered at District court Bremen HRB 35230 HB, Germany
|
|
|
|
|
|
|
|
|
|
For any legal inquiries or further information, please contact us via email at hello@task.vc.
|
|
|
|
|
For any legal inquiries or if you require further information, please contact us via email at hello@task.vc.
|
|
|
|
|
|
|
|
|
|
By using this repository, you acknowledge that you have read this section, agree to comply with its terms, and understand that the licensing of the code does not imply endorsement by Task Venture Capital GmbH of any derivative works.
|
|
|
|
|
|