@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). 🚀
Install
npm install @push.rocks/smartmongo --save-dev
# or
pnpm add -D @push.rocks/smartmongo
Issue Reporting and Security
For reporting bugs, issues, or security vulnerabilities, please visit community.foss.global/. This is the central community hub for all issue reporting. Developers who sign and comply with our contribution agreement and go through identification can also get a code.foss.global/ account to submit Pull Requests directly.
Overview
@push.rocks/smartmongo provides three powerful approaches for MongoDB in testing and development:
| 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) |
| 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 |
| Persistence | Dump to directory | Memory or file | File-based (automatic) |
| Use Case | Integration testing | Unit testing, CI/CD | Quick prototyping, local dev |
🚀 Quick Start
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!
import { LocalTsmDb } from '@push.rocks/smartmongo';
import { MongoClient } from 'mongodb';
// Create a local database backed by files
const db = new LocalTsmDb({ folderPath: './my-data' });
// Start and get connection info (Unix socket path + connection URI)
const { connectionUri } = await db.start();
// Connect with your own MongoDB client
const client = new MongoClient(connectionUri, { directConnection: true });
await client.connect();
// Use exactly like MongoDB
const users = client.db('myapp').collection('users');
await users.insertOne({ name: 'Alice', email: 'alice@example.com' });
const user = await users.findOne({ name: 'Alice' });
console.log(user); // { _id: ObjectId(...), name: 'Alice', email: 'alice@example.com' }
// Data persists to disk automatically!
await client.close();
await db.stop();
// Later... data is still there
const db2 = new LocalTsmDb({ folderPath: './my-data' });
const { connectionUri: uri2 } = await db2.start();
const client2 = new MongoClient(uri2, { directConnection: true });
await client2.connect();
const savedUser = await client2.db('myapp').collection('users').findOne({ name: 'Alice' });
// savedUser exists!
Option 2: TsmDB (Wire Protocol Server)
A lightweight, pure TypeScript MongoDB-compatible server — use the official mongodb driver directly!
import { tsmdb } from '@push.rocks/smartmongo';
import { MongoClient } from 'mongodb';
// Start TsmDB server (TCP mode)
const server = new tsmdb.TsmdbServer({ port: 27017 });
await server.start();
// Connect with the official MongoDB driver
const client = new MongoClient('mongodb://127.0.0.1:27017');
await client.connect();
// Use exactly like real MongoDB
const db = client.db('myapp');
await db.collection('users').insertOne({ name: 'Alice', age: 30 });
const user = await db.collection('users').findOne({ name: 'Alice' });
console.log(user); // { _id: ObjectId(...), name: 'Alice', age: 30 }
// Clean up
await client.close();
await server.stop();
Option 3: SmartMongo (Real MongoDB)
Spin up a real MongoDB replica set in memory — perfect for integration tests that need full MongoDB compatibility.
import { SmartMongo } from '@push.rocks/smartmongo';
// Start a MongoDB replica set
const mongo = await SmartMongo.createAndStart();
// Get connection details
const descriptor = await mongo.getMongoDescriptor();
console.log(descriptor.mongoDbUrl); // mongodb://127.0.0.1:xxxxx/...
// Use with your MongoDB client or ORM
// ... run your tests ...
// Clean up
await mongo.stop();
📖 LocalTsmDb API
The simplest option for local development and prototyping — lightweight, Unix socket-based, and automatic persistence.
Configuration
import { LocalTsmDb } from '@push.rocks/smartmongo';
import type { ILocalTsmDbOptions, ILocalTsmDbConnectionInfo } from '@push.rocks/smartmongo';
const options: ILocalTsmDbOptions = {
folderPath: './data', // Required: where to store data
socketPath: '/tmp/my.sock', // Optional: custom socket path (default: auto-generated)
};
const db = new LocalTsmDb(options);
Methods
| Method | Returns | Description |
|---|---|---|
start() |
Promise<ILocalTsmDbConnectionInfo> |
Starts the server and returns connection info |
stop() |
Promise<void> |
Stops the server and cleans up the socket |
getConnectionInfo() |
ILocalTsmDbConnectionInfo |
Returns current connection info |
getConnectionUri() |
string |
Returns the MongoDB connection URI |
getServer() |
TsmdbServer |
Returns the underlying TsmDB server instance |
running |
boolean |
Property indicating if the server is running |
Connection Info
The start() method returns an ILocalTsmDbConnectionInfo object:
interface ILocalTsmDbConnectionInfo {
socketPath: string; // The Unix socket file path, e.g., /tmp/smartmongo-abc123.sock
connectionUri: string; // MongoDB URI, e.g., mongodb://%2Ftmp%2Fsmartmongo-abc123.sock
}
Basic Usage
import { LocalTsmDb } from '@push.rocks/smartmongo';
import { MongoClient } from 'mongodb';
const db = new LocalTsmDb({ folderPath: './data' });
// Start and get connection info
const { socketPath, connectionUri } = await db.start();
console.log(socketPath); // /tmp/smartmongo-abc123.sock (auto-generated)
console.log(connectionUri); // mongodb://%2Ftmp%2Fsmartmongo-abc123.sock
// Connect with your own MongoDB client
const client = new MongoClient(connectionUri, { directConnection: true });
await client.connect();
// Use the client
const users = client.db('mydb').collection('users');
await users.insertOne({ name: 'Alice' });
// Check status
console.log(db.running); // true
// Stop when done (close your client first!)
await client.close();
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
📖 SmartMongo API
Full MongoDB replica set in memory using mongodb-memory-server.
Creating an Instance
import { SmartMongo } from '@push.rocks/smartmongo';
// Default: single replica
const mongo = await SmartMongo.createAndStart();
// Multiple replicas for testing replication
const mongo = await SmartMongo.createAndStart(3);
Getting Connection Details
const descriptor = await mongo.getMongoDescriptor();
// {
// mongoDbName: 'smartmongo_testdatabase',
// mongoDbUrl: 'mongodb://127.0.0.1:xxxxx/?replicaSet=testset'
// }
Stopping & Cleanup
// Simple stop (data discarded)
await mongo.stop();
// Stop and dump data to disk for inspection
await mongo.stopAndDumpToDir('./test-data');
// With custom file naming
await mongo.stopAndDumpToDir('./test-data', (doc) => `${doc.collection}-${doc._id}.bson`);
🔧 TsmDB API
Pure TypeScript MongoDB wire protocol server. No external dependencies.
Server Configuration
import { tsmdb } from '@push.rocks/smartmongo';
// TCP mode (default)
const server = new tsmdb.TsmdbServer({
port: 27017, // Default MongoDB port
host: '127.0.0.1', // Bind address
storage: 'memory', // 'memory' or 'file'
storagePath: './data', // For file-based storage
});
// Unix socket mode (no port conflicts!)
const server = new tsmdb.TsmdbServer({
socketPath: '/tmp/my-tsmdb.sock',
storage: 'file',
storagePath: './data',
});
await server.start();
console.log(server.getConnectionUri());
// TCP: mongodb://127.0.0.1:27017
// Socket: mongodb://%2Ftmp%2Fmy-tsmdb.sock
// Server properties
console.log(server.running); // true
console.log(server.port); // 27017 (TCP mode)
console.log(server.host); // '127.0.0.1' (TCP mode)
console.log(server.socketPath); // '/tmp/my-tsmdb.sock' (socket mode)
console.log(server.getUptime()); // seconds since start
console.log(server.getConnectionCount()); // active client connections
await server.stop();
Supported MongoDB Operations
TsmDB supports the core MongoDB operations via the wire protocol:
🔹 CRUD Operations
// Insert
await collection.insertOne({ name: 'Bob' });
await collection.insertMany([{ a: 1 }, { a: 2 }]);
// Find
const doc = await collection.findOne({ name: 'Bob' });
const docs = await collection.find({ age: { $gte: 18 } }).toArray();
// Update
await collection.updateOne({ name: 'Bob' }, { $set: { age: 25 } });
await collection.updateMany({ active: false }, { $set: { archived: true } });
// Delete
await collection.deleteOne({ name: 'Bob' });
await collection.deleteMany({ archived: true });
// Replace
await collection.replaceOne({ _id: id }, { name: 'New Bob', age: 30 });
// Find and Modify
const result = await collection.findOneAndUpdate(
{ name: 'Bob' },
{ $inc: { visits: 1 } },
{ returnDocument: 'after' }
);
🔹 Query Operators
// Comparison
{ age: { $eq: 25 } }
{ age: { $ne: 25 } }
{ age: { $gt: 18, $lt: 65 } }
{ age: { $gte: 18, $lte: 65 } }
{ status: { $in: ['active', 'pending'] } }
{ status: { $nin: ['deleted'] } }
// Logical
{ $and: [{ age: { $gte: 18 } }, { active: true }] }
{ $or: [{ status: 'active' }, { admin: true }] }
{ $not: { status: 'deleted' } }
// Element
{ email: { $exists: true } }
{ type: { $type: 'string' } }
// Array
{ tags: { $all: ['mongodb', 'database'] } }
{ scores: { $elemMatch: { $gte: 80, $lt: 90 } } }
{ tags: { $size: 3 } }
// Regex
{ name: { $regex: /^Al/i } }
{ email: { $regex: '@example\\.com$' } }
🔹 Update Operators
{ $set: { name: 'New Name' } }
{ $unset: { tempField: '' } }
{ $inc: { count: 1 } }
{ $mul: { price: 1.1 } }
{ $min: { lowScore: 50 } }
{ $max: { highScore: 100 } }
{ $push: { tags: 'new-tag' } }
{ $pull: { tags: 'old-tag' } }
{ $addToSet: { tags: 'unique-tag' } }
{ $pop: { queue: 1 } } // Remove last
{ $pop: { queue: -1 } } // Remove first
{ $rename: { oldField: 'newField' } }
{ $currentDate: { lastModified: true } }
🔹 Aggregation Pipeline
const results = await collection.aggregate([
{ $match: { status: 'active' } },
{ $group: { _id: '$category', total: { $sum: '$amount' } } },
{ $sort: { total: -1 } },
{ $limit: 10 },
{ $project: { category: '$_id', total: 1, _id: 0 } }
]).toArray();
Supported stages: $match, $project, $group, $sort, $limit, $skip, $unwind, $lookup, $addFields, $count, $facet, $replaceRoot, $set, $unset, and more.
Supported group accumulators: $sum, $avg, $min, $max, $first, $last, $push, $addToSet, $count.
🔹 Index Operations
// 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();
// Drop indexes
await collection.dropIndex('email_1');
await collection.dropIndexes(); // Drop all except _id
🔹 Database Operations
// List databases
const dbs = await client.db().admin().listDatabases();
// List collections
const collections = await db.listCollections().toArray();
// Create/drop collections
await db.createCollection('newcollection');
await db.dropCollection('oldcollection');
// Drop database
await db.dropDatabase();
// Database stats
const stats = await db.stats();
🔹 Count & Distinct
// Count documents
const total = await collection.countDocuments({});
const active = await collection.countDocuments({ status: 'active' });
const estimated = await collection.estimatedDocumentCount();
// Distinct values
const departments = await collection.distinct('department');
const activeDepts = await collection.distinct('department', { status: 'active' });
🔹 Bulk Operations
const result = await collection.bulkWrite([
{ insertOne: { document: { name: 'Bulk1' } } },
{ updateOne: { filter: { name: 'John' }, update: { $set: { bulk: true } } } },
{ deleteOne: { filter: { name: 'Expired' } } },
{ replaceOne: { filter: { _id: id }, replacement: { name: 'Replaced' } } }
]);
console.log(result.insertedCount); // 1
console.log(result.modifiedCount); // 1
console.log(result.deletedCount); // 1
Storage Adapters
TsmDB supports pluggable storage with data integrity features:
// In-memory (default) - fast, data lost on stop
const server = new tsmdb.TsmdbServer({ storage: 'memory' });
// In-memory with persistence - periodic snapshots to disk
const server = new tsmdb.TsmdbServer({
storage: 'memory',
persistPath: './data/snapshot.json',
persistIntervalMs: 30000 // Save every 30 seconds
});
// File-based - persistent storage with checksums
const server = new tsmdb.TsmdbServer({
storage: 'file',
storagePath: './data/tsmdb'
});
⚡ Performance & Reliability Features
TsmDB includes enterprise-grade features for robustness:
🔍 Index-Accelerated Queries
Indexes are automatically used to accelerate queries:
- Hash indexes for equality queries (
$eq,$in) - B-tree indexes for range queries (
$gt,$gte,$lt,$lte)
// Create an index
await collection.createIndex({ email: 1 });
await collection.createIndex({ age: 1 });
// These queries will use the index (fast!)
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
TsmDB includes a query planner that analyzes queries and selects optimal execution strategies:
import { tsmdb } from '@push.rocks/smartmongo';
// For debugging, you can access the query planner
const planner = new tsmdb.QueryPlanner(indexEngine);
const plan = await planner.plan(filter);
console.log(plan);
// {
// type: 'IXSCAN', // or 'IXSCAN_RANGE', 'COLLSCAN'
// indexName: 'email_1',
// selectivity: 0.01,
// indexCovering: true
// }
📝 Write-Ahead Logging (WAL)
For durability, TsmDB supports write-ahead logging:
import { tsmdb } from '@push.rocks/smartmongo';
const wal = new tsmdb.WAL('./data/wal.log');
await wal.initialize();
// WAL entries include:
// - LSN (Log Sequence Number)
// - Timestamp
// - Operation type (insert, update, delete, checkpoint)
// - Document data (BSON serialized)
// - CRC32 checksum for integrity
// Recovery support
const entries = wal.getEntriesAfter(lastCheckpointLsn);
🔐 Session Management
TsmDB tracks client sessions with automatic timeout and transaction linking:
// Sessions are automatically managed when using the MongoDB driver
const session = client.startSession();
try {
session.startTransaction();
await collection.insertOne({ name: 'Alice' }, { session });
await collection.updateOne({ name: 'Bob' }, { $inc: { balance: 100 } }, { session });
await session.commitTransaction();
} catch (error) {
await session.abortTransaction();
} finally {
session.endSession();
}
// Session features:
// - Automatic session timeout (30 minutes default)
// - Transaction auto-abort on session expiry
// - Session activity tracking
✅ Data Integrity Checksums
File-based storage supports CRC32 checksums to detect corruption:
import { tsmdb } from '@push.rocks/smartmongo';
// Checksums are used internally for WAL and data integrity
// Documents are checksummed on write, verified on read
const checksum = tsmdb.calculateDocumentChecksum(doc);
const isValid = tsmdb.verifyChecksum(docWithChecksum);
📋 Supported Wire Protocol Commands
| Category | Commands |
|---|---|
| Handshake | hello, isMaster, ismaster |
| CRUD | find, insert, update, delete, findAndModify, getMore, killCursors |
| Aggregation | aggregate, count, distinct |
| Indexes | createIndexes, dropIndexes, listIndexes |
| Transactions | startTransaction, commitTransaction, abortTransaction |
| Sessions | startSession, endSessions, refreshSessions |
| Admin | ping, listDatabases, listCollections, drop, dropDatabase, create, serverStatus, buildInfo, dbStats, collStats, connectionStatus |
TsmDB supports MongoDB wire protocol versions 0-21, compatible with MongoDB 3.6 through 7.0 drivers.
🧪 Testing Examples
Jest/Mocha with LocalTsmDb
import { LocalTsmDb } from '@push.rocks/smartmongo';
import { MongoClient, Db } from 'mongodb';
let db: LocalTsmDb;
let client: MongoClient;
beforeAll(async () => {
db = new LocalTsmDb({ folderPath: './test-data' });
const { connectionUri } = await db.start();
client = new MongoClient(connectionUri, { directConnection: true });
await client.connect();
});
afterAll(async () => {
await client.close();
await db.stop();
});
beforeEach(async () => {
// Clean slate for each test
await client.db('test').dropDatabase();
});
test('should insert and find user', async () => {
const users = client.db('test').collection('users');
await users.insertOne({ name: 'Alice', email: 'alice@example.com' });
const user = await users.findOne({ name: 'Alice' });
expect(user?.email).toBe('alice@example.com');
});
Jest/Mocha with TsmDB
import { tsmdb } from '@push.rocks/smartmongo';
import { MongoClient, Db } from 'mongodb';
let server: tsmdb.TsmdbServer;
let client: MongoClient;
let testDb: Db;
beforeAll(async () => {
server = new tsmdb.TsmdbServer({ port: 27117 });
await server.start();
client = new MongoClient('mongodb://127.0.0.1:27117');
await client.connect();
testDb = client.db('test');
});
afterAll(async () => {
await client.close();
await server.stop();
});
beforeEach(async () => {
await testDb.dropDatabase();
});
test('should insert and find user', async () => {
const users = testDb.collection('users');
await users.insertOne({ name: 'Alice', email: 'alice@example.com' });
const user = await users.findOne({ name: 'Alice' });
expect(user?.email).toBe('alice@example.com');
});
With @push.rocks/tapbundle
import { expect, tap } from '@git.zone/tstest/tapbundle';
import { LocalTsmDb } from '@push.rocks/smartmongo';
import { MongoClient } from 'mongodb';
let db: LocalTsmDb;
let client: MongoClient;
tap.test('setup', async () => {
db = new LocalTsmDb({ folderPath: './test-data' });
const { connectionUri } = await db.start();
client = new MongoClient(connectionUri, { directConnection: true });
await client.connect();
});
tap.test('should perform CRUD operations', async () => {
const col = client.db('test').collection('items');
// Create
const result = await col.insertOne({ name: 'Widget', price: 9.99 });
expect(result.insertedId).toBeTruthy();
// Read
const item = await col.findOne({ name: 'Widget' });
expect(item?.price).toEqual(9.99);
// Update
await col.updateOne({ name: 'Widget' }, { $set: { price: 12.99 } });
const updated = await col.findOne({ name: 'Widget' });
expect(updated?.price).toEqual(12.99);
// Delete
await col.deleteOne({ name: 'Widget' });
const deleted = await col.findOne({ name: 'Widget' });
expect(deleted).toBeNull();
});
tap.test('teardown', async () => {
await client.close();
await db.stop();
});
export default tap.start();
🏗️ 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)
TsmDB Wire Protocol Stack
┌─────────────────────────────────────────────────────────────┐
│ Official MongoDB Driver │
│ (mongodb npm) │
└─────────────────────────┬───────────────────────────────────┘
│ TCP/Unix Socket + OP_MSG/BSON
▼
┌─────────────────────────────────────────────────────────────┐
│ TsmdbServer │
│ ┌──────────────┐ ┌──────────────┐ ┌──────────────────┐ │
│ │ WireProtocol │→ │CommandRouter │→ │ Handlers │ │
│ │ (OP_MSG) │ │ │ │ (Find, Insert..) │ │
│ └──────────────┘ └──────────────┘ └──────────────────┘ │
└─────────────────────────┬───────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────┐
│ Engines │
│ ┌─────────┐ ┌────────┐ ┌───────────┐ ┌───────┐ ┌───────┐ │
│ │ Query │ │ Update │ │Aggregation│ │ Index │ │Session│ │
│ │ Planner │ │ Engine │ │ Engine │ │Engine │ │Engine │ │
│ └─────────┘ └────────┘ └───────────┘ └───────┘ └───────┘ │
│ ┌──────────────────────┐ │
│ │ Transaction Engine │ │
│ └──────────────────────┘ │
└─────────────────────────┬───────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────┐
│ Storage Layer │
│ ┌──────────────────┐ ┌──────────────────┐ ┌──────────┐ │
│ │ MemoryStorage │ │ FileStorage │ │ WAL │ │
│ │ │ │ (+ Checksums) │ │ │ │
│ └──────────────────┘ └──────────────────┘ └──────────┘ │
└─────────────────────────────────────────────────────────────┘
Key Components
| Component | Description |
|---|---|
| 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 |
| UpdateEngine | Processes update operators ($set, $inc, etc.) |
| AggregationEngine | Executes aggregation pipelines |
| IndexEngine | Manages B-tree and hash indexes |
| SessionEngine | Tracks client sessions and timeouts |
| TransactionEngine | Handles ACID transaction semantics |
| WAL | Write-ahead logging for durability |
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 file.
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.
Company Information
Task Venture Capital GmbH 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.
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.