Files
smartdb/readme.md
2026-03-26 16:03:25 +00:00

530 lines
20 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# @push.rocks/smartdb
A pure TypeScript MongoDB wire-protocol-compatible database server. Zero binary dependencies, instant startup, pluggable storage — use the official MongoDB driver and it just works. ⚡
## Install
```bash
pnpm add @push.rocks/smartdb
# or
npm install @push.rocks/smartdb
```
## Issue Reporting and Security
For reporting bugs, issues, or security vulnerabilities, please visit [community.foss.global/](https://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/](https://code.foss.global/) account to submit Pull Requests directly.
## What It Does
`@push.rocks/smartdb` is a **real database server** written entirely in TypeScript that speaks the MongoDB binary wire protocol. Connect with the official `mongodb` Node.js driver — no mocks, no stubs, no MongoDB binary required.
### Why SmartDB?
| | SmartDB | Real MongoDB |
|---|---|---|
| **Startup time** | ~5ms | ~2-5s |
| **Binary download** | None | ~200MB |
| **Node.js only** | ✅ | ❌ |
| **Persistence** | Memory or file-based | Full disk engine |
| **Perfect for** | Unit tests, CI/CD, prototyping, local dev | Production |
### Two Ways to Use It
- 🏗️ **`SmartdbServer`** — Full control. Configure port, host, storage backend, Unix sockets. Great for test fixtures or custom setups.
- 🎯 **`LocalSmartDb`** — Zero-config convenience. Give it a folder path, get a persistent MongoDB-compatible database over a Unix socket. Done.
## Quick Start
### Option 1: LocalSmartDb (Zero Config) 🎯
The fastest way to get a persistent local database:
```typescript
import { LocalSmartDb } from '@push.rocks/smartdb';
import { MongoClient } from 'mongodb';
// Point it at a folder — that's it
const db = new LocalSmartDb({ folderPath: './my-data' });
const { connectionUri } = await db.start();
// Connect with the standard MongoDB driver
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 — survives restarts!
await client.close();
await db.stop();
```
### Option 2: SmartdbServer (Full Control) 🏗️
```typescript
import { SmartdbServer } from '@push.rocks/smartdb';
import { MongoClient } from 'mongodb';
// TCP mode
const server = new SmartdbServer({ port: 27017 });
await server.start();
const client = new MongoClient('mongodb://127.0.0.1:27017');
await client.connect();
const db = client.db('myapp');
await db.collection('users').insertOne({ name: 'Alice', age: 30 });
const user = await db.collection('users').findOne({ name: 'Alice' });
await client.close();
await server.stop();
```
---
## API Reference
### SmartdbServer
The core server class. Speaks MongoDB wire protocol over TCP or Unix sockets.
#### Constructor Options (`ISmartdbServerOptions`)
```typescript
import { SmartdbServer } from '@push.rocks/smartdb';
// TCP mode (default)
const server = new SmartdbServer({
port: 27017, // Default: 27017
host: '127.0.0.1', // Default: 127.0.0.1
storage: 'memory', // 'memory' or 'file' (default: 'memory')
storagePath: './data', // Required when storage is 'file'
});
// Unix socket mode — no port conflicts!
const server = new SmartdbServer({
socketPath: '/tmp/smartdb.sock',
storage: 'file',
storagePath: './data',
});
// Memory storage with periodic persistence
const server = new SmartdbServer({
storage: 'memory',
persistPath: './data/snapshot.json',
persistIntervalMs: 30000, // Save every 30s
});
```
#### Methods & Properties
| Method / Property | Type | Description |
|---|---|---|
| `start()` | `Promise<void>` | Start the server |
| `stop()` | `Promise<void>` | Stop the server and clean up |
| `getConnectionUri()` | `string` | Get the MongoDB connection URI |
| `running` | `boolean` | Whether the server is currently running |
| `port` | `number` | Bound port (TCP mode) |
| `host` | `string` | Bound host (TCP mode) |
| `socketPath` | `string` | Socket path (socket mode) |
| `getUptime()` | `number` | Seconds since start |
| `getConnectionCount()` | `number` | Active client connections |
### LocalSmartDb
Zero-config wrapper around SmartdbServer. Uses Unix sockets and file-based persistence.
#### Constructor Options (`ILocalSmartDbOptions`)
```typescript
import { LocalSmartDb } from '@push.rocks/smartdb';
const db = new LocalSmartDb({
folderPath: './data', // Required: data storage directory
socketPath: '/tmp/custom.sock', // Optional: custom socket (default: auto-generated)
});
```
#### Methods & Properties
| Method / Property | Type | Description |
|---|---|---|
| `start()` | `Promise<ILocalSmartDbConnectionInfo>` | Start and return connection info |
| `stop()` | `Promise<void>` | Stop the server |
| `getConnectionInfo()` | `ILocalSmartDbConnectionInfo` | Get current connection info |
| `getConnectionUri()` | `string` | Get the MongoDB URI |
| `getServer()` | `SmartdbServer` | Access the underlying server |
| `running` | `boolean` | Whether the server is running |
#### Connection Info (`ILocalSmartDbConnectionInfo`)
```typescript
interface ILocalSmartDbConnectionInfo {
socketPath: string; // e.g., /tmp/smartdb-abc123.sock
connectionUri: string; // e.g., mongodb://%2Ftmp%2Fsmartdb-abc123.sock
}
```
---
## Supported MongoDB Operations
SmartDB supports the core MongoDB operations through the wire protocol. Use the standard `mongodb` driver — these all work:
### CRUD
```typescript
// 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
await collection.findOneAndUpdate({ name: 'Bob' }, { $inc: { visits: 1 } }, { returnDocument: 'after' });
await collection.findOneAndDelete({ expired: true });
await collection.findOneAndReplace({ _id: id }, { name: 'Replaced' }, { returnDocument: 'after' });
```
### Query Operators
```typescript
// Comparison
{ age: { $eq: 25 } } { age: { $ne: 25 } }
{ age: { $gt: 18 } } { age: { $lt: 65 } }
{ age: { $gte: 18 } } { age: { $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 } }
```
### Update Operators
```typescript
{ $set: { name: 'New Name' } }
{ $unset: { tempField: '' } }
{ $inc: { count: 1 } }
{ $mul: { price: 1.1 } }
{ $min: { low: 50 } } { $max: { high: 100 } }
{ $push: { tags: 'new' } } { $pull: { tags: 'old' } }
{ $addToSet: { tags: 'unique' } }
{ $pop: { queue: 1 } } // Remove last
{ $pop: { queue: -1 } } // Remove first
{ $rename: { old: 'new' } }
{ $currentDate: { lastModified: true } }
```
### Aggregation Pipeline
```typescript
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`
**Group accumulators:** `$sum`, `$avg`, `$min`, `$max`, `$first`, `$last`, `$push`, `$addToSet`, `$count`
### Indexes
```typescript
await collection.createIndex({ email: 1 }, { unique: true });
await collection.createIndex({ name: 1, age: -1 }); // compound
await collection.createIndex({ field: 1 }, { sparse: true });
const indexes = await collection.listIndexes().toArray();
await collection.dropIndex('email_1');
await collection.dropIndexes(); // drop all except _id
```
### Database & Admin
```typescript
await db.listCollections().toArray();
await db.createCollection('new');
await db.dropCollection('old');
await db.dropDatabase();
await db.stats();
const admin = client.db().admin();
await admin.listDatabases();
await admin.ping();
await admin.serverStatus();
```
### Bulk Operations
```typescript
const result = await collection.bulkWrite([
{ insertOne: { document: { name: 'Bulk1' } } },
{ updateOne: { filter: { name: 'X' }, update: { $set: { bulk: true } } } },
{ deleteOne: { filter: { name: 'Expired' } } },
]);
```
### Count & Distinct
```typescript
const count = await collection.countDocuments({ status: 'active' });
const estimated = await collection.estimatedDocumentCount();
const names = await collection.distinct('name');
```
---
## 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`, `currentOp`, `collMod`, `renameCollection` |
Compatible with MongoDB wire protocol versions 021 (MongoDB 3.6 through 7.0 drivers).
---
## Architecture
```
┌─────────────────────────────────────────────────────────────┐
│ Official MongoDB Driver │
│ (mongodb npm) │
└─────────────────────────┬───────────────────────────────────┘
│ TCP / Unix Socket + OP_MSG / BSON
┌─────────────────────────────────────────────────────────────┐
│ SmartdbServer │
│ ┌──────────────┐ ┌──────────────┐ ┌──────────────────┐ │
│ │ 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 | What It Does |
|---|---|
| **WireProtocol** | Parses/encodes MongoDB OP_MSG binary frames |
| **CommandRouter** | Routes parsed commands to the right handler |
| **QueryPlanner** | Picks COLLSCAN vs IXSCAN based on available indexes |
| **QueryEngine** | Filter matching powered by [mingo](https://github.com/kofrasa/mingo) |
| **UpdateEngine** | Processes `$set`, `$inc`, `$push`, and all update operators |
| **AggregationEngine** | Runs aggregation pipelines via mingo |
| **IndexEngine** | B-tree (range) and hash (equality) indexes |
| **TransactionEngine** | ACID transactions with snapshot isolation |
| **SessionEngine** | Client session tracking with automatic timeouts |
| **WAL** | Write-ahead logging with CRC32 checksums for crash recovery |
---
## Advanced Usage
### Storage Adapters
```typescript
import { SmartdbServer } from '@push.rocks/smartdb';
// In-memory (default) — fast, data lost on stop
const server = new SmartdbServer({ storage: 'memory' });
// In-memory with periodic persistence
const server = new SmartdbServer({
storage: 'memory',
persistPath: './data/snapshot.json',
persistIntervalMs: 30000,
});
// File-based — persistent storage with CRC32 checksums
const server = new SmartdbServer({
storage: 'file',
storagePath: './data/smartdb',
});
```
### Query Planner (Debugging)
```typescript
import { QueryPlanner, IndexEngine, MemoryStorageAdapter } from '@push.rocks/smartdb';
const storage = new MemoryStorageAdapter();
await storage.initialize();
const indexEngine = new IndexEngine('mydb', 'mycoll', storage);
const planner = new QueryPlanner(indexEngine);
const plan = await planner.plan({ age: { $gte: 18 } });
console.log(plan);
// { type: 'IXSCAN_RANGE', indexName: 'age_1', selectivity: 0.3, usesRange: true, ... }
const explain = await planner.explain({ age: 18 });
// Returns winning plan, rejected plans, and detailed analysis
```
### Data Integrity Checksums
```typescript
import { calculateDocumentChecksum, addChecksum, verifyChecksum, removeChecksum } from '@push.rocks/smartdb';
const doc = { name: 'Alice', age: 30 };
const protected = addChecksum(doc); // Adds _checksum field
const valid = verifyChecksum(protected); // true
protected.age = 31; // Tamper!
const still = verifyChecksum(protected); // false
const clean = removeChecksum(protected); // Removes _checksum
```
### Write-Ahead Logging
```typescript
import { WAL } from '@push.rocks/smartdb';
const wal = new WAL('./data/wal.log', { checkpointInterval: 100 });
await wal.initialize();
// Entries include: LSN, timestamp, operation, BSON data, CRC32 checksum
const lsn = await wal.logInsert('mydb', 'users', doc);
const entries = wal.getEntriesAfter(lastCheckpoint);
const recovered = wal.recoverDocument(entry);
await wal.checkpoint();
await wal.close();
```
---
## Testing Examples
### Unit Tests with @git.zone/tstest
```typescript
import { expect, tap } from '@git.zone/tstest/tapbundle';
import { SmartdbServer } from '@push.rocks/smartdb';
import { MongoClient } from 'mongodb';
let server: SmartdbServer;
let client: MongoClient;
tap.test('setup', async () => {
server = new SmartdbServer({ port: 27117 });
await server.start();
client = new MongoClient('mongodb://127.0.0.1:27117', { directConnection: true });
await client.connect();
});
tap.test('should insert and find', async () => {
const col = client.db('test').collection('items');
await col.insertOne({ name: 'Widget', price: 9.99 });
const item = await col.findOne({ name: 'Widget' });
expect(item?.price).toEqual(9.99);
});
tap.test('teardown', async () => {
await client.close();
await server.stop();
});
export default tap.start();
```
### With LocalSmartDb (Persistent Tests)
```typescript
import { LocalSmartDb } from '@push.rocks/smartdb';
import { MongoClient } from 'mongodb';
const db = new LocalSmartDb({ folderPath: './test-data' });
const { connectionUri } = await db.start();
const client = new MongoClient(connectionUri, { directConnection: true });
await client.connect();
// Tests here — data persists between test runs!
await client.close();
await db.stop();
```
## 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.
**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.