4 Commits

Author SHA1 Message Date
12102255c4 v3.0.0
Some checks failed
Default (tags) / security (push) Successful in 43s
Default (tags) / test (push) Failing after 4m1s
Default (tags) / release (push) Has been skipped
Default (tags) / metadata (push) Has been skipped
2026-02-01 14:34:07 +00:00
a0df731bc0 BREAKING CHANGE(tsmdb): rename CongoDB to TsmDB and relocate/rename wire-protocol server implementation and public exports 2026-02-01 14:34:07 +00:00
28e166ee35 v2.2.0
Some checks failed
Default (tags) / security (push) Successful in 44s
Default (tags) / test (push) Failing after 4m2s
Default (tags) / release (push) Has been skipped
Default (tags) / metadata (push) Has been skipped
2026-01-31 12:13:59 +00:00
06ada11b79 feat(readme): update README with expanded documentation covering CongoDB and SmartMongo, installation, quick start examples, architecture, usage examples, and legal/company information 2026-01-31 12:13:59 +00:00
33 changed files with 595 additions and 227 deletions

View File

@@ -1,5 +1,24 @@
# Changelog # Changelog
## 2026-02-01 - 3.0.0 - BREAKING CHANGE(tsmdb)
rename CongoDB to TsmDB and relocate/rename wire-protocol server implementation and public exports
- Project refactor renames the in-memory wire-protocol server from CongoDB -> TsmDB (identifiers, files and namespaces changed).
- ts/index.ts now exports tsmdb instead of congodb (public API change; consumers must update imports).
- All congodb sources under ts/congodb were removed and equivalent implementations added under ts/tsmdb (errors, engines, storage adapters, server, handlers, WireProtocol, types).
- Readme and usage examples updated to reference TsmDB/tsmdb and example code updated accordingly.
- Tests renamed/updated from test.congodb.ts -> test.tsmdb.ts to exercise the new tsmdb export and server.
## 2026-01-31 - 2.2.0 - feat(readme)
update README with expanded documentation covering CongoDB and SmartMongo, installation, quick start examples, architecture, usage examples, and legal/company information
- Completely expanded README: added detailed overview for SmartMongo and new CongoDB (wire-protocol server)
- Added Quick Start examples for both SmartMongo and CongoDB (TypeScript/ESM snippets)
- Included installation instructions for npm and pnpm and issue reporting/security guidance
- Added architecture diagram, example tests, and storage/engine descriptions
- Clarified license, trademark, and company contact information
- Large non-functional documentation-only change (+398 -44)
## 2026-01-31 - 2.1.0 - feat(congodb) ## 2026-01-31 - 2.1.0 - feat(congodb)
implement CongoDB MongoDB wire-protocol compatible in-memory server and APIs implement CongoDB MongoDB wire-protocol compatible in-memory server and APIs

View File

@@ -1,6 +1,6 @@
{ {
"name": "@push.rocks/smartmongo", "name": "@push.rocks/smartmongo",
"version": "2.1.0", "version": "3.0.0",
"private": false, "private": false,
"description": "A module for creating and managing a local MongoDB instance for testing purposes.", "description": "A module for creating and managing a local MongoDB instance for testing purposes.",
"main": "dist_ts/index.js", "main": "dist_ts/index.js",

View File

@@ -9,21 +9,21 @@
- **Why:** Deno wraps CommonJS exports in a `default` property, so default imports are required - **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`) - Fixed in version 2.0.13 (changed from `import * as mongoPlugin`)
## CongoDB - MongoDB Wire Protocol Server ## TsmDB - MongoDB Wire Protocol Server
### Architecture ### Architecture
CongoDB implements the MongoDB binary wire protocol (OP_MSG, OP_QUERY) allowing official MongoDB drivers to connect directly. TsmDB implements the MongoDB binary wire protocol (OP_MSG, OP_QUERY) allowing official MongoDB drivers to connect directly.
``` ```
Official MongoClient → TCP (wire protocol) → CongoServer → Engines → Storage Official MongoClient → TCP (wire protocol) → TsmdbServer → Engines → Storage
(mongodb npm) OP_MSG/BSON (port) (mongodb npm) OP_MSG/BSON (port)
``` ```
### Module Structure ### Module Structure
``` ```
ts/congodb/ ts/tsmdb/
├── server/ # Wire protocol server ├── server/ # Wire protocol server
│ ├── CongoServer.ts # TCP server, connection handling │ ├── TsmdbServer.ts # TCP server, connection handling
│ ├── WireProtocol.ts # OP_MSG/OP_QUERY parsing & encoding │ ├── WireProtocol.ts # OP_MSG/OP_QUERY parsing & encoding
│ ├── CommandRouter.ts # Route commands to handlers │ ├── CommandRouter.ts # Route commands to handlers
│ └── handlers/ # Command implementations │ └── handlers/ # Command implementations
@@ -53,11 +53,11 @@ ts/congodb/
### Usage Example ### Usage Example
```typescript ```typescript
import { CongoServer } from '@push.rocks/smartmongo/congodb'; import { TsmdbServer } from '@push.rocks/smartmongo/tsmdb';
import { MongoClient } from 'mongodb'; import { MongoClient } from 'mongodb';
// Start server // Start server
const server = new CongoServer({ port: 27117 }); const server = new TsmdbServer({ port: 27117 });
await server.start(); await server.start();
// Connect with official MongoDB driver // Connect with official MongoDB driver
@@ -82,8 +82,3 @@ await server.stop();
- **Aggregation**: aggregate, count, distinct - **Aggregation**: aggregate, count, distinct
- **Indexes**: createIndexes, dropIndexes, listIndexes - **Indexes**: createIndexes, dropIndexes, listIndexes
- **Admin**: ping, listDatabases, listCollections, drop, dropDatabase, create, serverStatus, buildInfo - **Admin**: ping, listDatabases, listCollections, drop, dropDatabase, create, serverStatus, buildInfo
### Notes
- The old CongoClient/CongoDb/CongoCollection classes have been removed
- Use the official `mongodb` npm package's MongoClient instead
- Server supports MongoDB wire protocol versions 0-21 (MongoDB 3.6 through 7.0 compatible)

442
readme.md
View File

@@ -1,104 +1,458 @@
# @push.rocks/smartmongo # @push.rocks/smartmongo
create a local mongodb for testing A powerful MongoDB toolkit for testing and development — featuring both a real MongoDB memory server (**SmartMongo**) and an ultra-fast, lightweight wire-protocol-compatible in-memory database server (**TsmDB**). 🚀
## Install ## Install
To start using @push.rocks/smartmongo in your project, you first need to install it via npm. You can do this by running the following command in your terminal:
```bash ```bash
npm install @push.rocks/smartmongo --save-dev npm install @push.rocks/smartmongo --save-dev
# or
pnpm add -D @push.rocks/smartmongo
``` ```
This will add `@push.rocks/smartmongo` as a development dependency to your project because it's typically used for testing purposes. ## Issue Reporting and Security
## Usage 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.
The `@push.rocks/smartmongo` package provides a convenient way to spin up a local MongoDB instance, primarily for testing purposes. It's designed to simplify the process of configuring and managing a MongoDB replica set during development or in CI/CD pipelines. Below, we present a comprehensive guide on how to utilize the full feature set of this module, employing ESM syntax and TypeScript. ## Overview
### Setting Up `@push.rocks/smartmongo` provides two powerful approaches for MongoDB in testing and development:
To get started, you must first import the `SmartMongo` class from the package. This class is responsible for handling the MongoDB instances. | Feature | SmartMongo | TsmDB |
|---------|------------|---------|
| **Type** | Real MongoDB (memory server) | Pure TypeScript wire protocol server |
| **Speed** | ~2-5s startup | ⚡ Instant startup (~5ms) |
| **Compatibility** | 100% MongoDB | MongoDB driver compatible |
| **Dependencies** | Downloads MongoDB binary | Zero external dependencies |
| **Replication** | ✅ Full replica set support | Single node emulation |
| **Use Case** | Integration testing | Unit testing, CI/CD |
| **Persistence** | Dump to directory | Optional file/memory persistence |
## 🚀 Quick Start
### Option 1: SmartMongo (Real MongoDB)
Spin up a real MongoDB replica set in memory — perfect for integration tests that need full MongoDB compatibility.
```typescript ```typescript
import { SmartMongo } from '@push.rocks/smartmongo'; 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();
``` ```
### Creating and Starting a MongoDB Instance ### Option 2: TsmDB (Wire Protocol Server)
With `SmartMongo`, you can easily create and start a MongoDB replica set. You can specify the number of replica instances; however, if not specified, it defaults to 1. A lightweight, pure TypeScript MongoDB-compatible server that speaks the wire protocol — use the official `mongodb` driver directly!
```typescript ```typescript
async function setupMongoDB() { import { tsmdb } from '@push.rocks/smartmongo';
const smartMongoInstance = await SmartMongo.createAndStart(1); // Number of replicas is optional import { MongoClient } from 'mongodb';
return smartMongoInstance;
}
const myDbInstance = await setupMongoDB(); // Start TsmDB server
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();
``` ```
After invoking `createAndStart`, an instance of MongoDB is spun up and is ready for use. The `createAndStart` function returns a `SmartMongo` instance which can be interacted with for further operations. ## 📖 SmartMongo API
### Accessing MongoDB Connection Information ### Creating an Instance
After instantiation, you might want to connect your application or test suite to the MongoDB instance. The `getMongoDescriptor` method facilitates this by providing essential connection details.
```typescript ```typescript
const mongoDescriptor = await myDbInstance.getMongoDescriptor(); import { SmartMongo } from '@push.rocks/smartmongo';
console.log(mongoDescriptor.mongoDbUrl); // Use this URL to connect with Mongoose or MongoDB clients.
// Default: single replica
const mongo = await SmartMongo.createAndStart();
// Multiple replicas for testing replication
const mongo = await SmartMongo.createAndStart(3);
``` ```
### Stopping and Cleaning Up ### Getting Connection Details
Once your tests have completed or you're done using the MongoDB instance, its crucial to properly stop and clean up the resources. `@push.rocks/smartmongo` provides two methods for this purpose:
1. **stop()**: Stops the MongoDB instance without persisting any data.
```typescript ```typescript
await myDbInstance.stop(); const descriptor = await mongo.getMongoDescriptor();
// {
// mongoDbName: 'smartmongo_testdatabase',
// mongoDbUrl: 'mongodb://127.0.0.1:xxxxx/?replicaSet=testset'
// }
``` ```
2. **stopAndDumpToDir(dirPath)**: Stops the MongoDB instance and persists the data to the specified directory. This is useful if you need to examine the data post-test or reuse it in subsequent runs. ### Stopping & Cleanup
```typescript ```typescript
await myDbInstance.stopAndDumpToDir('./path/to/dump'); // 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`);
``` ```
### Advanced Usage ## 🔧 TsmDB API
`@push.rocks/smartmongo` also provides advanced features for dumping the database and configuring MongoDB replica sets. These features can be particularly useful for complex testing scenarios or when specific MongoDB behaviors need to be emulated. ### Server Configuration
#### Dumping Data
To dump the MongoDB data for inspection or backup purposes, use the `stopAndDumpToDir` method. This method optionally takes a function to customize the naming scheme of the dumped files based on the document content.
```typescript ```typescript
await myDbInstance.stopAndDumpToDir('./path/to/dump', (doc) => { import { tsmdb } from '@push.rocks/smartmongo';
return `customNameBasedOnDoc-${doc._id}.bson`;
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
});
await server.start();
console.log(server.getConnectionUri()); // mongodb://127.0.0.1:27017
// Server properties
console.log(server.running); // true
console.log(server.getUptime()); // seconds
console.log(server.getConnectionCount()); // active connections
await server.stop();
```
### Supported MongoDB Operations
TsmDB supports the core MongoDB operations via the wire protocol:
#### 🔹 CRUD Operations
```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
const result = await collection.findOneAndUpdate(
{ name: 'Bob' },
{ $inc: { visits: 1 } },
{ returnDocument: 'after' }
);
```
#### 🔹 Query Operators
```typescript
// 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 } }
```
#### 🔹 Update Operators
```typescript
{ $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
```
#### 🔹 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`, and more.
#### 🔹 Index Operations
```typescript
await collection.createIndex({ email: 1 }, { unique: true });
await collection.createIndex({ name: 1, age: -1 });
const indexes = await collection.listIndexes().toArray();
await collection.dropIndex('email_1');
```
#### 🔹 Database Operations
```typescript
// 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();
```
#### 🔹 Count & Distinct
```typescript
// 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
```typescript
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:
```typescript
// 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
const server = new tsmdb.TsmdbServer({
storage: 'file',
storagePath: './data/tsmdb'
}); });
``` ```
Using `@push.rocks/smartmongo` significantly simplifies the process of managing MongoDB instances for local testing environments. It abstracts away the complexity of starting, operating, and tearing down MongoDB replica sets, allowing developers to focus on building and testing their applications. ### 📋 Supported Wire Protocol Commands
### Conclusion | Category | 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` |
`@push.rocks/smartmongo` serves as a powerful tool in a developer's arsenal for efficiently configuring, running, and managing MongoDB instances in testing scenarios. By following the above guide, developers can leverage MongoDB in their projects with minimal setup and gain valuable insights into their applications' data interactions in a controlled and reproducible environment. TsmDB supports MongoDB wire protocol versions 0-21, compatible with MongoDB 3.6 through 7.0 drivers.
## 🧪 Testing Examples
### Jest/Mocha with TsmDB
```typescript
import { tsmdb } from '@push.rocks/smartmongo';
import { MongoClient } from 'mongodb';
let server: tsmdb.TsmdbServer;
let client: MongoClient;
let db: Db;
beforeAll(async () => {
server = new tsmdb.TsmdbServer({ port: 27117 });
await server.start();
client = new MongoClient('mongodb://127.0.0.1:27117');
await client.connect();
db = client.db('test');
});
afterAll(async () => {
await client.close();
await server.stop();
});
beforeEach(async () => {
// Clean slate for each test
await db.dropDatabase();
});
test('should insert and find user', async () => {
const users = db.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
```typescript
import { expect, tap } from '@git.zone/tstest/tapbundle';
import { tsmdb } from '@push.rocks/smartmongo';
import { MongoClient } from 'mongodb';
let server: tsmdb.TsmdbServer;
let client: MongoClient;
tap.test('setup', async () => {
server = new tsmdb.TsmdbServer({ port: 27117 });
await server.start();
client = new MongoClient('mongodb://127.0.0.1:27117');
await client.connect();
});
tap.test('should perform CRUD operations', async () => {
const db = client.db('test');
const col = db.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 server.stop();
});
export default tap.start();
```
## 🏗️ Architecture
### TsmDB Wire Protocol Stack
```
┌─────────────────────────────────────────────────────────────┐
│ Official MongoDB Driver │
│ (mongodb npm) │
└─────────────────────────┬───────────────────────────────────┘
│ TCP + OP_MSG/BSON
┌─────────────────────────────────────────────────────────────┐
│ TsmdbServer │
│ ┌──────────────┐ ┌──────────────┐ ┌──────────────────┐ │
│ │ WireProtocol │→ │CommandRouter │→ │ Handlers │ │
│ │ (OP_MSG) │ │ │ │ (Find, Insert..) │ │
│ └──────────────┘ └──────────────┘ └──────────────────┘ │
└─────────────────────────┬───────────────────────────────────┘
┌─────────────────────────────────────────────────────────────┐
│ Engines │
│ ┌───────────┐ ┌────────────┐ ┌────────────┐ ┌───────────┐ │
│ │ Query │ │ Update │ │Aggregation │ │ Index │ │
│ │ Engine │ │ Engine │ │ Engine │ │ Engine │ │
│ └───────────┘ └────────────┘ └────────────┘ └───────────┘ │
└─────────────────────────┬───────────────────────────────────┘
┌─────────────────────────────────────────────────────────────┐
│ Storage Adapters │
│ ┌──────────────────┐ ┌──────────────────┐ │
│ │ MemoryStorage │ │ FileStorage │ │
│ └──────────────────┘ └──────────────────┘ │
└─────────────────────────────────────────────────────────────┘
```
## License and Legal Information ## License and Legal Information
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. 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. **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 ### 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 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. 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 ### Company Information
Task Venture Capital GmbH 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 if you require further information, please contact us via email at hello@task.vc. 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. 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.

View File

@@ -2,9 +2,9 @@ import { expect, tap } from '@git.zone/tstest/tapbundle';
import * as smartmongo from '../ts/index.js'; import * as smartmongo from '../ts/index.js';
import { MongoClient, Db, Collection } from 'mongodb'; import { MongoClient, Db, Collection } from 'mongodb';
const { congodb } = smartmongo; const { tsmdb } = smartmongo;
let server: smartmongo.congodb.CongoServer; let server: smartmongo.tsmdb.TsmdbServer;
let client: MongoClient; let client: MongoClient;
let db: Db; let db: Db;
@@ -12,13 +12,13 @@ let db: Db;
// Server Startup // Server Startup
// ============================================================================ // ============================================================================
tap.test('congodb: should start the server', async () => { tap.test('tsmdb: should start the server', async () => {
server = new congodb.CongoServer({ port: 27117 }); // Use non-standard port for tests server = new tsmdb.TsmdbServer({ port: 27117 }); // Use non-standard port for tests
await server.start(); await server.start();
expect(server.running).toBeTrue(); expect(server.running).toBeTrue();
}); });
tap.test('congodb: should connect with official MongoClient', async () => { tap.test('tsmdb: should connect with official MongoClient', async () => {
client = new MongoClient('mongodb://127.0.0.1:27117', { client = new MongoClient('mongodb://127.0.0.1:27117', {
directConnection: true, directConnection: true,
serverSelectionTimeoutMS: 5000, serverSelectionTimeoutMS: 5000,
@@ -27,7 +27,7 @@ tap.test('congodb: should connect with official MongoClient', async () => {
expect(client).toBeTruthy(); expect(client).toBeTruthy();
}); });
tap.test('congodb: should get a database instance', async () => { tap.test('tsmdb: should get a database instance', async () => {
db = client.db('testdb'); db = client.db('testdb');
expect(db).toBeTruthy(); expect(db).toBeTruthy();
expect(db.databaseName).toEqual('testdb'); expect(db.databaseName).toEqual('testdb');
@@ -37,7 +37,7 @@ tap.test('congodb: should get a database instance', async () => {
// Basic CRUD Tests // Basic CRUD Tests
// ============================================================================ // ============================================================================
tap.test('congodb: insertOne - should insert a document', async () => { tap.test('tsmdb: insertOne - should insert a document', async () => {
const collection = db.collection('users'); const collection = db.collection('users');
const result = await collection.insertOne({ const result = await collection.insertOne({
name: 'John Doe', name: 'John Doe',
@@ -49,7 +49,7 @@ tap.test('congodb: insertOne - should insert a document', async () => {
expect(result.insertedId).toBeTruthy(); expect(result.insertedId).toBeTruthy();
}); });
tap.test('congodb: insertMany - should insert multiple documents', async () => { tap.test('tsmdb: insertMany - should insert multiple documents', async () => {
const collection = db.collection('users'); const collection = db.collection('users');
const result = await collection.insertMany([ const result = await collection.insertMany([
{ name: 'Jane Doe', email: 'jane@example.com', age: 25 }, { name: 'Jane Doe', email: 'jane@example.com', age: 25 },
@@ -62,7 +62,7 @@ tap.test('congodb: insertMany - should insert multiple documents', async () => {
expect(Object.keys(result.insertedIds).length).toEqual(3); expect(Object.keys(result.insertedIds).length).toEqual(3);
}); });
tap.test('congodb: findOne - should find a single document', async () => { tap.test('tsmdb: findOne - should find a single document', async () => {
const collection = db.collection('users'); const collection = db.collection('users');
const doc = await collection.findOne({ name: 'John Doe' }); const doc = await collection.findOne({ name: 'John Doe' });
@@ -71,14 +71,14 @@ tap.test('congodb: findOne - should find a single document', async () => {
expect(doc!.email).toEqual('john@example.com'); expect(doc!.email).toEqual('john@example.com');
}); });
tap.test('congodb: find - should find multiple documents', async () => { tap.test('tsmdb: find - should find multiple documents', async () => {
const collection = db.collection('users'); const collection = db.collection('users');
const docs = await collection.find({ age: { $gte: 28 } }).toArray(); const docs = await collection.find({ age: { $gte: 28 } }).toArray();
expect(docs.length).toEqual(3); expect(docs.length).toEqual(3);
}); });
tap.test('congodb: updateOne - should update a single document', async () => { tap.test('tsmdb: updateOne - should update a single document', async () => {
const collection = db.collection('users'); const collection = db.collection('users');
const result = await collection.updateOne( const result = await collection.updateOne(
{ name: 'John Doe' }, { name: 'John Doe' },
@@ -93,7 +93,7 @@ tap.test('congodb: updateOne - should update a single document', async () => {
expect(updated!.age).toEqual(31); expect(updated!.age).toEqual(31);
}); });
tap.test('congodb: updateMany - should update multiple documents', async () => { tap.test('tsmdb: updateMany - should update multiple documents', async () => {
const collection = db.collection('users'); const collection = db.collection('users');
const result = await collection.updateMany( const result = await collection.updateMany(
{ age: { $gte: 30 } }, { age: { $gte: 30 } },
@@ -105,7 +105,7 @@ tap.test('congodb: updateMany - should update multiple documents', async () => {
expect(result.modifiedCount).toEqual(2); expect(result.modifiedCount).toEqual(2);
}); });
tap.test('congodb: deleteOne - should delete a single document', async () => { tap.test('tsmdb: deleteOne - should delete a single document', async () => {
const collection = db.collection('users'); const collection = db.collection('users');
const result = await collection.deleteOne({ name: 'Bob Smith' }); const result = await collection.deleteOne({ name: 'Bob Smith' });
@@ -113,7 +113,7 @@ tap.test('congodb: deleteOne - should delete a single document', async () => {
expect(result.deletedCount).toEqual(1); expect(result.deletedCount).toEqual(1);
}); });
tap.test('congodb: deleteMany - should delete multiple documents', async () => { tap.test('tsmdb: deleteMany - should delete multiple documents', async () => {
const collection = db.collection('users'); const collection = db.collection('users');
// First add some test docs to delete // First add some test docs to delete
@@ -132,32 +132,32 @@ tap.test('congodb: deleteMany - should delete multiple documents', async () => {
// Query Operator Tests // Query Operator Tests
// ============================================================================ // ============================================================================
tap.test('congodb: query - $eq operator', async () => { tap.test('tsmdb: query - $eq operator', async () => {
const collection = db.collection('users'); const collection = db.collection('users');
const docs = await collection.find({ name: { $eq: 'Jane Doe' } }).toArray(); const docs = await collection.find({ name: { $eq: 'Jane Doe' } }).toArray();
expect(docs.length).toEqual(1); expect(docs.length).toEqual(1);
expect(docs[0].name).toEqual('Jane Doe'); expect(docs[0].name).toEqual('Jane Doe');
}); });
tap.test('congodb: query - $ne operator', async () => { tap.test('tsmdb: query - $ne operator', async () => {
const collection = db.collection('users'); const collection = db.collection('users');
const docs = await collection.find({ name: { $ne: 'Jane Doe' } }).toArray(); const docs = await collection.find({ name: { $ne: 'Jane Doe' } }).toArray();
expect(docs.every(d => d.name !== 'Jane Doe')).toBeTrue(); expect(docs.every(d => d.name !== 'Jane Doe')).toBeTrue();
}); });
tap.test('congodb: query - $gt and $lt operators', async () => { tap.test('tsmdb: query - $gt and $lt operators', async () => {
const collection = db.collection('users'); const collection = db.collection('users');
const docs = await collection.find({ age: { $gt: 25, $lt: 35 } }).toArray(); const docs = await collection.find({ age: { $gt: 25, $lt: 35 } }).toArray();
expect(docs.every(d => d.age > 25 && d.age < 35)).toBeTrue(); expect(docs.every(d => d.age > 25 && d.age < 35)).toBeTrue();
}); });
tap.test('congodb: query - $in operator', async () => { tap.test('tsmdb: query - $in operator', async () => {
const collection = db.collection('users'); const collection = db.collection('users');
const docs = await collection.find({ name: { $in: ['Jane Doe', 'Alice Johnson'] } }).toArray(); const docs = await collection.find({ name: { $in: ['Jane Doe', 'Alice Johnson'] } }).toArray();
expect(docs.length).toEqual(2); expect(docs.length).toEqual(2);
}); });
tap.test('congodb: query - $or operator', async () => { tap.test('tsmdb: query - $or operator', async () => {
const collection = db.collection('users'); const collection = db.collection('users');
const docs = await collection.find({ const docs = await collection.find({
$or: [ $or: [
@@ -168,7 +168,7 @@ tap.test('congodb: query - $or operator', async () => {
expect(docs.length).toBeGreaterThanOrEqual(1); expect(docs.length).toBeGreaterThanOrEqual(1);
}); });
tap.test('congodb: query - $and operator', async () => { tap.test('tsmdb: query - $and operator', async () => {
const collection = db.collection('users'); const collection = db.collection('users');
const docs = await collection.find({ const docs = await collection.find({
$and: [ $and: [
@@ -179,7 +179,7 @@ tap.test('congodb: query - $and operator', async () => {
expect(docs.every(d => d.age >= 25 && d.age <= 30)).toBeTrue(); expect(docs.every(d => d.age >= 25 && d.age <= 30)).toBeTrue();
}); });
tap.test('congodb: query - $exists operator', async () => { tap.test('tsmdb: query - $exists operator', async () => {
const collection = db.collection('users'); const collection = db.collection('users');
const docs = await collection.find({ senior: { $exists: true } }).toArray(); const docs = await collection.find({ senior: { $exists: true } }).toArray();
expect(docs.every(d => 'senior' in d)).toBeTrue(); expect(docs.every(d => 'senior' in d)).toBeTrue();
@@ -189,7 +189,7 @@ tap.test('congodb: query - $exists operator', async () => {
// Update Operator Tests // Update Operator Tests
// ============================================================================ // ============================================================================
tap.test('congodb: update - $inc operator', async () => { tap.test('tsmdb: update - $inc operator', async () => {
const collection = db.collection('users'); const collection = db.collection('users');
await collection.updateOne( await collection.updateOne(
{ name: 'Jane Doe' }, { name: 'Jane Doe' },
@@ -200,7 +200,7 @@ tap.test('congodb: update - $inc operator', async () => {
expect(updated!.age).toEqual(26); expect(updated!.age).toEqual(26);
}); });
tap.test('congodb: update - $unset operator', async () => { tap.test('tsmdb: update - $unset operator', async () => {
const collection = db.collection('users'); const collection = db.collection('users');
await collection.updateOne( await collection.updateOne(
{ name: 'Jane Doe' }, { name: 'Jane Doe' },
@@ -211,7 +211,7 @@ tap.test('congodb: update - $unset operator', async () => {
expect('senior' in updated!).toBeFalse(); expect('senior' in updated!).toBeFalse();
}); });
tap.test('congodb: update - $push operator', async () => { tap.test('tsmdb: update - $push operator', async () => {
const collection = db.collection('users'); const collection = db.collection('users');
await collection.updateOne( await collection.updateOne(
{ name: 'Jane Doe' }, { name: 'Jane Doe' },
@@ -227,7 +227,7 @@ tap.test('congodb: update - $push operator', async () => {
expect(updated!.tags).toContain('tester'); expect(updated!.tags).toContain('tester');
}); });
tap.test('congodb: update - $pull operator', async () => { tap.test('tsmdb: update - $pull operator', async () => {
const collection = db.collection('users'); const collection = db.collection('users');
await collection.updateOne( await collection.updateOne(
{ name: 'Jane Doe' }, { name: 'Jane Doe' },
@@ -238,7 +238,7 @@ tap.test('congodb: update - $pull operator', async () => {
expect(updated!.tags).not.toContain('tester'); expect(updated!.tags).not.toContain('tester');
}); });
tap.test('congodb: update - upsert creates new document', async () => { tap.test('tsmdb: update - upsert creates new document', async () => {
const collection = db.collection('users'); const collection = db.collection('users');
const result = await collection.updateOne( const result = await collection.updateOne(
{ name: 'New User' }, { name: 'New User' },
@@ -258,7 +258,7 @@ tap.test('congodb: update - upsert creates new document', async () => {
// Cursor Tests // Cursor Tests
// ============================================================================ // ============================================================================
tap.test('congodb: cursor - sort', async () => { tap.test('tsmdb: cursor - sort', async () => {
const collection = db.collection('users'); const collection = db.collection('users');
const docs = await collection.find({}).sort({ age: -1 }).toArray(); const docs = await collection.find({}).sort({ age: -1 }).toArray();
@@ -269,13 +269,13 @@ tap.test('congodb: cursor - sort', async () => {
} }
}); });
tap.test('congodb: cursor - limit', async () => { tap.test('tsmdb: cursor - limit', async () => {
const collection = db.collection('users'); const collection = db.collection('users');
const docs = await collection.find({}).limit(2).toArray(); const docs = await collection.find({}).limit(2).toArray();
expect(docs.length).toBeLessThanOrEqual(2); expect(docs.length).toBeLessThanOrEqual(2);
}); });
tap.test('congodb: cursor - skip', async () => { tap.test('tsmdb: cursor - skip', async () => {
const collection = db.collection('users'); const collection = db.collection('users');
const allDocs = await collection.find({}).toArray(); const allDocs = await collection.find({}).toArray();
const skippedDocs = await collection.find({}).skip(1).toArray(); const skippedDocs = await collection.find({}).skip(1).toArray();
@@ -283,7 +283,7 @@ tap.test('congodb: cursor - skip', async () => {
expect(skippedDocs.length).toEqual(Math.max(0, allDocs.length - 1)); expect(skippedDocs.length).toEqual(Math.max(0, allDocs.length - 1));
}); });
tap.test('congodb: cursor - project', async () => { tap.test('tsmdb: cursor - project', async () => {
const collection = db.collection('users'); const collection = db.collection('users');
const docs = await collection.find({}).project({ name: 1, _id: 0 }).toArray(); const docs = await collection.find({}).project({ name: 1, _id: 0 }).toArray();
@@ -296,7 +296,7 @@ tap.test('congodb: cursor - project', async () => {
// FindOneAnd* Tests // FindOneAnd* Tests
// ============================================================================ // ============================================================================
tap.test('congodb: findOneAndUpdate - returns updated document', async () => { tap.test('tsmdb: findOneAndUpdate - returns updated document', async () => {
const collection = db.collection('users'); const collection = db.collection('users');
const result = await collection.findOneAndUpdate( const result = await collection.findOneAndUpdate(
{ name: 'Jane Doe' }, { name: 'Jane Doe' },
@@ -308,7 +308,7 @@ tap.test('congodb: findOneAndUpdate - returns updated document', async () => {
expect(result!.status).toEqual('active'); expect(result!.status).toEqual('active');
}); });
tap.test('congodb: findOneAndDelete - returns deleted document', async () => { tap.test('tsmdb: findOneAndDelete - returns deleted document', async () => {
const collection = db.collection('users'); const collection = db.collection('users');
// Insert a temp doc to delete // Insert a temp doc to delete
@@ -328,19 +328,19 @@ tap.test('congodb: findOneAndDelete - returns deleted document', async () => {
// Count and Distinct Tests // Count and Distinct Tests
// ============================================================================ // ============================================================================
tap.test('congodb: countDocuments - counts matching documents', async () => { tap.test('tsmdb: countDocuments - counts matching documents', async () => {
const collection = db.collection('users'); const collection = db.collection('users');
const count = await collection.countDocuments({ age: { $gte: 25 } }); const count = await collection.countDocuments({ age: { $gte: 25 } });
expect(count).toBeGreaterThan(0); expect(count).toBeGreaterThan(0);
}); });
tap.test('congodb: estimatedDocumentCount - returns total count', async () => { tap.test('tsmdb: estimatedDocumentCount - returns total count', async () => {
const collection = db.collection('users'); const collection = db.collection('users');
const count = await collection.estimatedDocumentCount(); const count = await collection.estimatedDocumentCount();
expect(count).toBeGreaterThan(0); expect(count).toBeGreaterThan(0);
}); });
tap.test('congodb: distinct - returns unique values', async () => { tap.test('tsmdb: distinct - returns unique values', async () => {
const collection = db.collection('users'); const collection = db.collection('users');
const names = await collection.distinct('name'); const names = await collection.distinct('name');
@@ -353,7 +353,7 @@ tap.test('congodb: distinct - returns unique values', async () => {
// Index Tests // Index Tests
// ============================================================================ // ============================================================================
tap.test('congodb: createIndex - creates a single index', async () => { tap.test('tsmdb: createIndex - creates a single index', async () => {
const collection = db.collection('users'); const collection = db.collection('users');
const indexName = await collection.createIndex({ email: 1 }); const indexName = await collection.createIndex({ email: 1 });
@@ -361,14 +361,14 @@ tap.test('congodb: createIndex - creates a single index', async () => {
expect(indexName).toContain('email'); expect(indexName).toContain('email');
}); });
tap.test('congodb: createIndex - creates compound index', async () => { tap.test('tsmdb: createIndex - creates compound index', async () => {
const collection = db.collection('users'); const collection = db.collection('users');
const indexName = await collection.createIndex({ name: 1, age: -1 }); const indexName = await collection.createIndex({ name: 1, age: -1 });
expect(indexName).toBeTruthy(); expect(indexName).toBeTruthy();
}); });
tap.test('congodb: listIndexes - lists all indexes', async () => { tap.test('tsmdb: listIndexes - lists all indexes', async () => {
const collection = db.collection('users'); const collection = db.collection('users');
const indexes = await collection.listIndexes().toArray(); const indexes = await collection.listIndexes().toArray();
@@ -376,7 +376,7 @@ tap.test('congodb: listIndexes - lists all indexes', async () => {
expect(indexes.some(i => i.name === '_id_')).toBeTrue(); expect(indexes.some(i => i.name === '_id_')).toBeTrue();
}); });
tap.test('congodb: dropIndex - drops an index', async () => { tap.test('tsmdb: dropIndex - drops an index', async () => {
const collection = db.collection('users'); const collection = db.collection('users');
const indexName = await collection.createIndex({ toDropField: 1 }); const indexName = await collection.createIndex({ toDropField: 1 });
@@ -390,7 +390,7 @@ tap.test('congodb: dropIndex - drops an index', async () => {
// Aggregation Tests // Aggregation Tests
// ============================================================================ // ============================================================================
tap.test('congodb: aggregate - $match stage', async () => { tap.test('tsmdb: aggregate - $match stage', async () => {
const collection = db.collection('users'); const collection = db.collection('users');
const results = await collection.aggregate([ const results = await collection.aggregate([
{ $match: { age: { $gte: 25 } } } { $match: { age: { $gte: 25 } } }
@@ -400,7 +400,7 @@ tap.test('congodb: aggregate - $match stage', async () => {
expect(results.every(d => d.age >= 25)).toBeTrue(); expect(results.every(d => d.age >= 25)).toBeTrue();
}); });
tap.test('congodb: aggregate - $project stage', async () => { tap.test('tsmdb: aggregate - $project stage', async () => {
const collection = db.collection('users'); const collection = db.collection('users');
const results = await collection.aggregate([ const results = await collection.aggregate([
{ $project: { name: 1, _id: 0 } } { $project: { name: 1, _id: 0 } }
@@ -411,7 +411,7 @@ tap.test('congodb: aggregate - $project stage', async () => {
expect(results[0].email).toBeUndefined(); expect(results[0].email).toBeUndefined();
}); });
tap.test('congodb: aggregate - $sort stage', async () => { tap.test('tsmdb: aggregate - $sort stage', async () => {
const collection = db.collection('users'); const collection = db.collection('users');
const results = await collection.aggregate([ const results = await collection.aggregate([
{ $match: { age: { $exists: true } } }, { $match: { age: { $exists: true } } },
@@ -423,7 +423,7 @@ tap.test('congodb: aggregate - $sort stage', async () => {
} }
}); });
tap.test('congodb: aggregate - $group stage', async () => { tap.test('tsmdb: aggregate - $group stage', async () => {
const collection = db.collection('users'); const collection = db.collection('users');
// Add some categorized data // Add some categorized data
@@ -445,7 +445,7 @@ tap.test('congodb: aggregate - $group stage', async () => {
expect(groupB!.total).toEqual(30); expect(groupB!.total).toEqual(30);
}); });
tap.test('congodb: aggregate - $limit and $skip stages', async () => { tap.test('tsmdb: aggregate - $limit and $skip stages', async () => {
const collection = db.collection('users'); const collection = db.collection('users');
const results = await collection.aggregate([ const results = await collection.aggregate([
{ $skip: 1 }, { $skip: 1 },
@@ -459,7 +459,7 @@ tap.test('congodb: aggregate - $limit and $skip stages', async () => {
// Bulk Operations Tests // Bulk Operations Tests
// ============================================================================ // ============================================================================
tap.test('congodb: bulkWrite - executes multiple operations', async () => { tap.test('tsmdb: bulkWrite - executes multiple operations', async () => {
const collection = db.collection('bulktest'); const collection = db.collection('bulktest');
const result = await collection.bulkWrite([ const result = await collection.bulkWrite([
@@ -476,18 +476,18 @@ tap.test('congodb: bulkWrite - executes multiple operations', async () => {
// Database Operations Tests // Database Operations Tests
// ============================================================================ // ============================================================================
tap.test('congodb: listCollections - lists all collections', async () => { tap.test('tsmdb: listCollections - lists all collections', async () => {
const collections = await db.listCollections().toArray(); const collections = await db.listCollections().toArray();
expect(collections.length).toBeGreaterThan(0); expect(collections.length).toBeGreaterThan(0);
}); });
tap.test('congodb: createCollection - creates a new collection', async () => { tap.test('tsmdb: createCollection - creates a new collection', async () => {
await db.createCollection('newcollection'); await db.createCollection('newcollection');
const collections = await db.listCollections().toArray(); const collections = await db.listCollections().toArray();
expect(collections.some(c => c.name === 'newcollection')).toBeTrue(); expect(collections.some(c => c.name === 'newcollection')).toBeTrue();
}); });
tap.test('congodb: dropCollection - drops a collection', async () => { tap.test('tsmdb: dropCollection - drops a collection', async () => {
await db.createCollection('todrop'); await db.createCollection('todrop');
await db.dropCollection('todrop'); await db.dropCollection('todrop');
const collections = await db.listCollections().toArray(); const collections = await db.listCollections().toArray();
@@ -498,20 +498,20 @@ tap.test('congodb: dropCollection - drops a collection', async () => {
// Admin Tests // Admin Tests
// ============================================================================ // ============================================================================
tap.test('congodb: admin - listDatabases', async () => { tap.test('tsmdb: admin - listDatabases', async () => {
const admin = client.db().admin(); const admin = client.db().admin();
const result = await admin.listDatabases(); const result = await admin.listDatabases();
expect(result.ok).toEqual(1); expect(result.ok).toEqual(1);
expect(result.databases).toBeArray(); expect(result.databases).toBeArray();
}); });
tap.test('congodb: admin - serverStatus', async () => { tap.test('tsmdb: admin - serverStatus', async () => {
const admin = client.db().admin(); const admin = client.db().admin();
const status = await admin.serverStatus(); const status = await admin.serverStatus();
expect(status.ok).toEqual(1); expect(status.ok).toEqual(1);
}); });
tap.test('congodb: admin - ping', async () => { tap.test('tsmdb: admin - ping', async () => {
const admin = client.db().admin(); const admin = client.db().admin();
const result = await admin.ping(); const result = await admin.ping();
expect(result.ok).toEqual(1); expect(result.ok).toEqual(1);
@@ -521,7 +521,7 @@ tap.test('congodb: admin - ping', async () => {
// Replace Operations Tests // Replace Operations Tests
// ============================================================================ // ============================================================================
tap.test('congodb: replaceOne - replaces entire document', async () => { tap.test('tsmdb: replaceOne - replaces entire document', async () => {
const collection = db.collection('replacetest'); const collection = db.collection('replacetest');
await collection.insertOne({ name: 'Original', field1: 'value1', field2: 'value2' }); await collection.insertOne({ name: 'Original', field1: 'value1', field2: 'value2' });
@@ -540,7 +540,7 @@ tap.test('congodb: replaceOne - replaces entire document', async () => {
expect(replaced!.field2).toBeUndefined(); expect(replaced!.field2).toBeUndefined();
}); });
tap.test('congodb: findOneAndReplace - returns replaced document', async () => { tap.test('tsmdb: findOneAndReplace - returns replaced document', async () => {
const collection = db.collection('replacetest'); const collection = db.collection('replacetest');
await collection.insertOne({ name: 'ToReplace', data: 'old' }); await collection.insertOne({ name: 'ToReplace', data: 'old' });
@@ -558,12 +558,12 @@ tap.test('congodb: findOneAndReplace - returns replaced document', async () => {
// Cleanup // Cleanup
// ============================================================================ // ============================================================================
tap.test('congodb: cleanup - drop database', async () => { tap.test('tsmdb: cleanup - drop database', async () => {
const result = await db.dropDatabase(); const result = await db.dropDatabase();
expect(result).toBeTrue(); expect(result).toBeTrue();
}); });
tap.test('congodb: cleanup - close client and server', async () => { tap.test('tsmdb: cleanup - close client and server', async () => {
await client.close(); await client.close();
await server.stop(); await server.stop();
expect(server.running).toBeFalse(); expect(server.running).toBeFalse();

View File

@@ -3,6 +3,6 @@
*/ */
export const commitinfo = { export const commitinfo = {
name: '@push.rocks/smartmongo', name: '@push.rocks/smartmongo',
version: '2.1.0', version: '3.0.0',
description: 'A module for creating and managing a local MongoDB instance for testing purposes.' description: 'A module for creating and managing a local MongoDB instance for testing purposes.'
} }

View File

@@ -1,8 +1,8 @@
import { commitinfo } from './00_commitinfo_data.js'; import { commitinfo } from './00_commitinfo_data.js';
import * as plugins from './smartmongo.plugins.js'; import * as plugins from './smartmongo.plugins.js';
// Export CongoDB module // Export TsmDB module
export * as congodb from './congodb/index.js'; export * as tsmdb from './tsmdb/index.js';
export class SmartMongo { export class SmartMongo {
// STATIC // STATIC

View File

@@ -1,4 +1,4 @@
import * as plugins from '../congodb.plugins.js'; import * as plugins from '../tsmdb.plugins.js';
import type { Document, IStoredDocument, IAggregateOptions } from '../types/interfaces.js'; import type { Document, IStoredDocument, IAggregateOptions } from '../types/interfaces.js';
// Import mingo Aggregator // Import mingo Aggregator

View File

@@ -1,4 +1,4 @@
import * as plugins from '../congodb.plugins.js'; import * as plugins from '../tsmdb.plugins.js';
import type { IStorageAdapter } from '../storage/IStorageAdapter.js'; import type { IStorageAdapter } from '../storage/IStorageAdapter.js';
import type { import type {
Document, Document,
@@ -7,7 +7,7 @@ import type {
IIndexInfo, IIndexInfo,
ICreateIndexOptions, ICreateIndexOptions,
} from '../types/interfaces.js'; } from '../types/interfaces.js';
import { CongoDuplicateKeyError, CongoIndexError } from '../errors/CongoErrors.js'; import { TsmdbDuplicateKeyError, TsmdbIndexError } from '../errors/TsmdbErrors.js';
import { QueryEngine } from './QueryEngine.js'; import { QueryEngine } from './QueryEngine.js';
/** /**
@@ -116,7 +116,7 @@ export class IndexEngine {
const keyStr = JSON.stringify(keyValue); const keyStr = JSON.stringify(keyValue);
if (indexData.unique && indexData.entries.has(keyStr)) { if (indexData.unique && indexData.entries.has(keyStr)) {
throw new CongoDuplicateKeyError( throw new TsmdbDuplicateKeyError(
`E11000 duplicate key error index: ${this.dbName}.${this.collName}.$${name}`, `E11000 duplicate key error index: ${this.dbName}.${this.collName}.$${name}`,
key as Record<string, 1>, key as Record<string, 1>,
keyValue keyValue
@@ -148,11 +148,11 @@ export class IndexEngine {
await this.initialize(); await this.initialize();
if (name === '_id_') { if (name === '_id_') {
throw new CongoIndexError('cannot drop _id index'); throw new TsmdbIndexError('cannot drop _id index');
} }
if (!this.indexes.has(name)) { if (!this.indexes.has(name)) {
throw new CongoIndexError(`index not found: ${name}`); throw new TsmdbIndexError(`index not found: ${name}`);
} }
this.indexes.delete(name); this.indexes.delete(name);
@@ -215,7 +215,7 @@ export class IndexEngine {
if (indexData.unique) { if (indexData.unique) {
const existing = indexData.entries.get(keyStr); const existing = indexData.entries.get(keyStr);
if (existing && existing.size > 0) { if (existing && existing.size > 0) {
throw new CongoDuplicateKeyError( throw new TsmdbDuplicateKeyError(
`E11000 duplicate key error collection: ${this.dbName}.${this.collName} index: ${name}`, `E11000 duplicate key error collection: ${this.dbName}.${this.collName} index: ${name}`,
indexData.key as Record<string, 1>, indexData.key as Record<string, 1>,
keyValue keyValue
@@ -260,7 +260,7 @@ export class IndexEngine {
if (indexData.unique) { if (indexData.unique) {
const existing = indexData.entries.get(newKeyStr); const existing = indexData.entries.get(newKeyStr);
if (existing && existing.size > 0) { if (existing && existing.size > 0) {
throw new CongoDuplicateKeyError( throw new TsmdbDuplicateKeyError(
`E11000 duplicate key error collection: ${this.dbName}.${this.collName} index: ${name}`, `E11000 duplicate key error collection: ${this.dbName}.${this.collName} index: ${name}`,
indexData.key as Record<string, 1>, indexData.key as Record<string, 1>,
newKeyValue newKeyValue

View File

@@ -1,4 +1,4 @@
import * as plugins from '../congodb.plugins.js'; import * as plugins from '../tsmdb.plugins.js';
import type { Document, IStoredDocument, ISortSpecification, ISortDirection } from '../types/interfaces.js'; import type { Document, IStoredDocument, ISortSpecification, ISortDirection } from '../types/interfaces.js';
// Import mingo Query class // Import mingo Query class

View File

@@ -1,7 +1,7 @@
import * as plugins from '../congodb.plugins.js'; import * as plugins from '../tsmdb.plugins.js';
import type { IStorageAdapter } from '../storage/IStorageAdapter.js'; import type { IStorageAdapter } from '../storage/IStorageAdapter.js';
import type { Document, IStoredDocument, ITransactionOptions } from '../types/interfaces.js'; import type { Document, IStoredDocument, ITransactionOptions } from '../types/interfaces.js';
import { CongoTransactionError, CongoWriteConflictError } from '../errors/CongoErrors.js'; import { TsmdbTransactionError, TsmdbWriteConflictError } from '../errors/TsmdbErrors.js';
/** /**
* Transaction state * Transaction state
@@ -70,7 +70,7 @@ export class TransactionEngine {
async getSnapshot(txnId: string, dbName: string, collName: string): Promise<IStoredDocument[]> { async getSnapshot(txnId: string, dbName: string, collName: string): Promise<IStoredDocument[]> {
const txn = this.transactions.get(txnId); const txn = this.transactions.get(txnId);
if (!txn || txn.status !== 'active') { if (!txn || txn.status !== 'active') {
throw new CongoTransactionError('Transaction is not active'); throw new TsmdbTransactionError('Transaction is not active');
} }
const ns = `${dbName}.${collName}`; const ns = `${dbName}.${collName}`;
@@ -148,7 +148,7 @@ export class TransactionEngine {
recordInsert(txnId: string, dbName: string, collName: string, doc: IStoredDocument): void { recordInsert(txnId: string, dbName: string, collName: string, doc: IStoredDocument): void {
const txn = this.transactions.get(txnId); const txn = this.transactions.get(txnId);
if (!txn || txn.status !== 'active') { if (!txn || txn.status !== 'active') {
throw new CongoTransactionError('Transaction is not active'); throw new TsmdbTransactionError('Transaction is not active');
} }
const ns = `${dbName}.${collName}`; const ns = `${dbName}.${collName}`;
@@ -174,7 +174,7 @@ export class TransactionEngine {
): void { ): void {
const txn = this.transactions.get(txnId); const txn = this.transactions.get(txnId);
if (!txn || txn.status !== 'active') { if (!txn || txn.status !== 'active') {
throw new CongoTransactionError('Transaction is not active'); throw new TsmdbTransactionError('Transaction is not active');
} }
const ns = `${dbName}.${collName}`; const ns = `${dbName}.${collName}`;
@@ -203,7 +203,7 @@ export class TransactionEngine {
recordDelete(txnId: string, dbName: string, collName: string, doc: IStoredDocument): void { recordDelete(txnId: string, dbName: string, collName: string, doc: IStoredDocument): void {
const txn = this.transactions.get(txnId); const txn = this.transactions.get(txnId);
if (!txn || txn.status !== 'active') { if (!txn || txn.status !== 'active') {
throw new CongoTransactionError('Transaction is not active'); throw new TsmdbTransactionError('Transaction is not active');
} }
const ns = `${dbName}.${collName}`; const ns = `${dbName}.${collName}`;
@@ -231,10 +231,10 @@ export class TransactionEngine {
async commitTransaction(txnId: string): Promise<void> { async commitTransaction(txnId: string): Promise<void> {
const txn = this.transactions.get(txnId); const txn = this.transactions.get(txnId);
if (!txn) { if (!txn) {
throw new CongoTransactionError('Transaction not found'); throw new TsmdbTransactionError('Transaction not found');
} }
if (txn.status !== 'active') { if (txn.status !== 'active') {
throw new CongoTransactionError(`Cannot commit transaction in state: ${txn.status}`); throw new TsmdbTransactionError(`Cannot commit transaction in state: ${txn.status}`);
} }
// Check for write conflicts // Check for write conflicts
@@ -245,7 +245,7 @@ export class TransactionEngine {
const hasConflicts = await this.storage.hasConflicts(dbName, collName, ids, txn.startTime); const hasConflicts = await this.storage.hasConflicts(dbName, collName, ids, txn.startTime);
if (hasConflicts) { if (hasConflicts) {
txn.status = 'aborted'; txn.status = 'aborted';
throw new CongoWriteConflictError(); throw new TsmdbWriteConflictError();
} }
} }
@@ -281,7 +281,7 @@ export class TransactionEngine {
async abortTransaction(txnId: string): Promise<void> { async abortTransaction(txnId: string): Promise<void> {
const txn = this.transactions.get(txnId); const txn = this.transactions.get(txnId);
if (!txn) { if (!txn) {
throw new CongoTransactionError('Transaction not found'); throw new TsmdbTransactionError('Transaction not found');
} }
if (txn.status !== 'active') { if (txn.status !== 'active') {
// Already committed or aborted, just return // Already committed or aborted, just return
@@ -336,7 +336,7 @@ export class TransactionEngine {
await this.abortTransaction(txnId); await this.abortTransaction(txnId);
this.endTransaction(txnId); this.endTransaction(txnId);
if (error instanceof CongoWriteConflictError && attempt < maxRetries - 1) { if (error instanceof TsmdbWriteConflictError && attempt < maxRetries - 1) {
// Retry on write conflict // Retry on write conflict
lastError = error; lastError = error;
continue; continue;
@@ -346,6 +346,6 @@ export class TransactionEngine {
} }
} }
throw lastError || new CongoTransactionError('Transaction failed after max retries'); throw lastError || new TsmdbTransactionError('Transaction failed after max retries');
} }
} }

View File

@@ -1,4 +1,4 @@
import * as plugins from '../congodb.plugins.js'; import * as plugins from '../tsmdb.plugins.js';
import type { Document, IStoredDocument } from '../types/interfaces.js'; import type { Document, IStoredDocument } from '../types/interfaces.js';
import { QueryEngine } from './QueryEngine.js'; import { QueryEngine } from './QueryEngine.js';

View File

@@ -1,14 +1,14 @@
/** /**
* Base error class for all CongoDB errors * Base error class for all TsmDB errors
* Mirrors MongoDB driver error hierarchy * Mirrors MongoDB driver error hierarchy
*/ */
export class CongoError extends Error { export class TsmdbError extends Error {
public code?: number; public code?: number;
public codeName?: string; public codeName?: string;
constructor(message: string, code?: number, codeName?: string) { constructor(message: string, code?: number, codeName?: string) {
super(message); super(message);
this.name = 'CongoError'; this.name = 'TsmdbError';
this.code = code; this.code = code;
this.codeName = codeName; this.codeName = codeName;
Object.setPrototypeOf(this, new.target.prototype); Object.setPrototypeOf(this, new.target.prototype);
@@ -18,33 +18,33 @@ export class CongoError extends Error {
/** /**
* Error thrown during connection issues * Error thrown during connection issues
*/ */
export class CongoConnectionError extends CongoError { export class TsmdbConnectionError extends TsmdbError {
constructor(message: string) { constructor(message: string) {
super(message); super(message);
this.name = 'CongoConnectionError'; this.name = 'TsmdbConnectionError';
} }
} }
/** /**
* Error thrown when an operation times out * Error thrown when an operation times out
*/ */
export class CongoTimeoutError extends CongoError { export class TsmdbTimeoutError extends TsmdbError {
constructor(message: string) { constructor(message: string) {
super(message, 50, 'MaxTimeMSExpired'); super(message, 50, 'MaxTimeMSExpired');
this.name = 'CongoTimeoutError'; this.name = 'TsmdbTimeoutError';
} }
} }
/** /**
* Error thrown during write operations * Error thrown during write operations
*/ */
export class CongoWriteError extends CongoError { export class TsmdbWriteError extends TsmdbError {
public writeErrors?: IWriteError[]; public writeErrors?: IWriteError[];
public result?: any; public result?: any;
constructor(message: string, code?: number, writeErrors?: IWriteError[]) { constructor(message: string, code?: number, writeErrors?: IWriteError[]) {
super(message, code); super(message, code);
this.name = 'CongoWriteError'; this.name = 'TsmdbWriteError';
this.writeErrors = writeErrors; this.writeErrors = writeErrors;
} }
} }
@@ -52,13 +52,13 @@ export class CongoWriteError extends CongoError {
/** /**
* Error thrown for duplicate key violations * Error thrown for duplicate key violations
*/ */
export class CongoDuplicateKeyError extends CongoWriteError { export class TsmdbDuplicateKeyError extends TsmdbWriteError {
public keyPattern?: Record<string, 1>; public keyPattern?: Record<string, 1>;
public keyValue?: Record<string, any>; public keyValue?: Record<string, any>;
constructor(message: string, keyPattern?: Record<string, 1>, keyValue?: Record<string, any>) { constructor(message: string, keyPattern?: Record<string, 1>, keyValue?: Record<string, any>) {
super(message, 11000); super(message, 11000);
this.name = 'CongoDuplicateKeyError'; this.name = 'TsmdbDuplicateKeyError';
this.codeName = 'DuplicateKey'; this.codeName = 'DuplicateKey';
this.keyPattern = keyPattern; this.keyPattern = keyPattern;
this.keyValue = keyValue; this.keyValue = keyValue;
@@ -68,13 +68,13 @@ export class CongoDuplicateKeyError extends CongoWriteError {
/** /**
* Error thrown for bulk write failures * Error thrown for bulk write failures
*/ */
export class CongoBulkWriteError extends CongoError { export class TsmdbBulkWriteError extends TsmdbError {
public writeErrors: IWriteError[]; public writeErrors: IWriteError[];
public result: any; public result: any;
constructor(message: string, writeErrors: IWriteError[], result: any) { constructor(message: string, writeErrors: IWriteError[], result: any) {
super(message, 65); super(message, 65);
this.name = 'CongoBulkWriteError'; this.name = 'TsmdbBulkWriteError';
this.writeErrors = writeErrors; this.writeErrors = writeErrors;
this.result = result; this.result = result;
} }
@@ -83,20 +83,20 @@ export class CongoBulkWriteError extends CongoError {
/** /**
* Error thrown during transaction operations * Error thrown during transaction operations
*/ */
export class CongoTransactionError extends CongoError { export class TsmdbTransactionError extends TsmdbError {
constructor(message: string, code?: number) { constructor(message: string, code?: number) {
super(message, code); super(message, code);
this.name = 'CongoTransactionError'; this.name = 'TsmdbTransactionError';
} }
} }
/** /**
* Error thrown when a transaction is aborted due to conflict * Error thrown when a transaction is aborted due to conflict
*/ */
export class CongoWriteConflictError extends CongoTransactionError { export class TsmdbWriteConflictError extends TsmdbTransactionError {
constructor(message: string = 'Write conflict during transaction') { constructor(message: string = 'Write conflict during transaction') {
super(message, 112); super(message, 112);
this.name = 'CongoWriteConflictError'; this.name = 'TsmdbWriteConflictError';
this.codeName = 'WriteConflict'; this.codeName = 'WriteConflict';
} }
} }
@@ -104,20 +104,20 @@ export class CongoWriteConflictError extends CongoTransactionError {
/** /**
* Error thrown for invalid arguments * Error thrown for invalid arguments
*/ */
export class CongoArgumentError extends CongoError { export class TsmdbArgumentError extends TsmdbError {
constructor(message: string) { constructor(message: string) {
super(message); super(message);
this.name = 'CongoArgumentError'; this.name = 'TsmdbArgumentError';
} }
} }
/** /**
* Error thrown when an operation is not supported * Error thrown when an operation is not supported
*/ */
export class CongoNotSupportedError extends CongoError { export class TsmdbNotSupportedError extends TsmdbError {
constructor(message: string) { constructor(message: string) {
super(message, 115); super(message, 115);
this.name = 'CongoNotSupportedError'; this.name = 'TsmdbNotSupportedError';
this.codeName = 'CommandNotSupported'; this.codeName = 'CommandNotSupported';
} }
} }
@@ -125,20 +125,20 @@ export class CongoNotSupportedError extends CongoError {
/** /**
* Error thrown when cursor is exhausted or closed * Error thrown when cursor is exhausted or closed
*/ */
export class CongoCursorError extends CongoError { export class TsmdbCursorError extends TsmdbError {
constructor(message: string) { constructor(message: string) {
super(message); super(message);
this.name = 'CongoCursorError'; this.name = 'TsmdbCursorError';
} }
} }
/** /**
* Error thrown when a namespace (database.collection) is invalid * Error thrown when a namespace (database.collection) is invalid
*/ */
export class CongoNamespaceError extends CongoError { export class TsmdbNamespaceError extends TsmdbError {
constructor(message: string) { constructor(message: string) {
super(message, 73); super(message, 73);
this.name = 'CongoNamespaceError'; this.name = 'TsmdbNamespaceError';
this.codeName = 'InvalidNamespace'; this.codeName = 'InvalidNamespace';
} }
} }
@@ -146,10 +146,10 @@ export class CongoNamespaceError extends CongoError {
/** /**
* Error thrown when an index operation fails * Error thrown when an index operation fails
*/ */
export class CongoIndexError extends CongoError { export class TsmdbIndexError extends TsmdbError {
constructor(message: string, code?: number) { constructor(message: string, code?: number) {
super(message, code || 86); super(message, code || 86);
this.name = 'CongoIndexError'; this.name = 'TsmdbIndexError';
} }
} }
@@ -164,18 +164,18 @@ export interface IWriteError {
} }
/** /**
* Convert any error to a CongoError * Convert any error to a TsmdbError
*/ */
export function toCongoError(error: any): CongoError { export function toTsmdbError(error: any): TsmdbError {
if (error instanceof CongoError) { if (error instanceof TsmdbError) {
return error; return error;
} }
const congoError = new CongoError(error.message || String(error)); const tsmdbError = new TsmdbError(error.message || String(error));
if (error.code) { if (error.code) {
congoError.code = error.code; tsmdbError.code = error.code;
} }
if (error.codeName) { if (error.codeName) {
congoError.codeName = error.codeName; tsmdbError.codeName = error.codeName;
} }
return congoError; return tsmdbError;
} }

View File

@@ -1,8 +1,8 @@
// CongoDB - MongoDB Wire Protocol compatible in-memory database server // TsmDB - MongoDB Wire Protocol compatible in-memory database server
// Use the official MongoDB driver to connect to CongoServer // Use the official MongoDB driver to connect to TsmdbServer
// Re-export plugins for external use // Re-export plugins for external use
import * as plugins from './congodb.plugins.js'; import * as plugins from './tsmdb.plugins.js';
export { plugins }; export { plugins };
// Export BSON types for convenience // Export BSON types for convenience
@@ -12,7 +12,7 @@ export { ObjectId, Binary, Timestamp, Long, Decimal128, UUID } from 'bson';
export * from './types/interfaces.js'; export * from './types/interfaces.js';
// Export errors // Export errors
export * from './errors/CongoErrors.js'; export * from './errors/TsmdbErrors.js';
// Export storage adapters // Export storage adapters
export type { IStorageAdapter } from './storage/IStorageAdapter.js'; export type { IStorageAdapter } from './storage/IStorageAdapter.js';
@@ -27,9 +27,9 @@ export { AggregationEngine } from './engine/AggregationEngine.js';
export { IndexEngine } from './engine/IndexEngine.js'; export { IndexEngine } from './engine/IndexEngine.js';
export { TransactionEngine } from './engine/TransactionEngine.js'; export { TransactionEngine } from './engine/TransactionEngine.js';
// Export server (the main entry point for using CongoDB) // Export server (the main entry point for using TsmDB)
export { CongoServer } from './server/CongoServer.js'; export { TsmdbServer } from './server/TsmdbServer.js';
export type { ICongoServerOptions } from './server/CongoServer.js'; export type { ITsmdbServerOptions } from './server/TsmdbServer.js';
// Export wire protocol utilities (for advanced usage) // Export wire protocol utilities (for advanced usage)
export { WireProtocol } from './server/WireProtocol.js'; export { WireProtocol } from './server/WireProtocol.js';

View File

@@ -1,7 +1,7 @@
import * as plugins from '../congodb.plugins.js'; import * as plugins from '../tsmdb.plugins.js';
import type { IStorageAdapter } from '../storage/IStorageAdapter.js'; import type { IStorageAdapter } from '../storage/IStorageAdapter.js';
import type { IParsedCommand } from './WireProtocol.js'; import type { IParsedCommand } from './WireProtocol.js';
import type { CongoServer } from './CongoServer.js'; import type { TsmdbServer } from './TsmdbServer.js';
// Import handlers // Import handlers
import { HelloHandler } from './handlers/HelloHandler.js'; import { HelloHandler } from './handlers/HelloHandler.js';
@@ -18,7 +18,7 @@ import { AdminHandler } from './handlers/AdminHandler.js';
*/ */
export interface IHandlerContext { export interface IHandlerContext {
storage: IStorageAdapter; storage: IStorageAdapter;
server: CongoServer; server: TsmdbServer;
database: string; database: string;
command: plugins.bson.Document; command: plugins.bson.Document;
documentSequences?: Map<string, plugins.bson.Document[]>; documentSequences?: Map<string, plugins.bson.Document[]>;
@@ -36,14 +36,14 @@ export interface ICommandHandler {
*/ */
export class CommandRouter { export class CommandRouter {
private storage: IStorageAdapter; private storage: IStorageAdapter;
private server: CongoServer; private server: TsmdbServer;
private handlers: Map<string, ICommandHandler> = new Map(); private handlers: Map<string, ICommandHandler> = new Map();
// Cursor state for getMore operations // Cursor state for getMore operations
private cursors: Map<bigint, ICursorState> = new Map(); private cursors: Map<bigint, ICursorState> = new Map();
private cursorIdCounter: bigint = BigInt(1); private cursorIdCounter: bigint = BigInt(1);
constructor(storage: IStorageAdapter, server: CongoServer) { constructor(storage: IStorageAdapter, server: TsmdbServer) {
this.storage = storage; this.storage = storage;
this.server = server; this.server = server;
this.registerHandlers(); this.registerHandlers();

View File

@@ -1,5 +1,5 @@
import * as net from 'net'; import * as net from 'net';
import * as plugins from '../congodb.plugins.js'; import * as plugins from '../tsmdb.plugins.js';
import { WireProtocol, OP_QUERY } from './WireProtocol.js'; import { WireProtocol, OP_QUERY } from './WireProtocol.js';
import { CommandRouter } from './CommandRouter.js'; import { CommandRouter } from './CommandRouter.js';
import { MemoryStorageAdapter } from '../storage/MemoryStorageAdapter.js'; import { MemoryStorageAdapter } from '../storage/MemoryStorageAdapter.js';
@@ -9,7 +9,7 @@ import type { IStorageAdapter } from '../storage/IStorageAdapter.js';
/** /**
* Server configuration options * Server configuration options
*/ */
export interface ICongoServerOptions { export interface ITsmdbServerOptions {
/** Port to listen on (default: 27017) */ /** Port to listen on (default: 27017) */
port?: number; port?: number;
/** Host to bind to (default: 127.0.0.1) */ /** Host to bind to (default: 127.0.0.1) */
@@ -36,25 +36,25 @@ interface IConnectionState {
} }
/** /**
* CongoServer - MongoDB Wire Protocol compatible server * TsmdbServer - MongoDB Wire Protocol compatible server
* *
* This server implements the MongoDB wire protocol (OP_MSG) to allow * This server implements the MongoDB wire protocol (OP_MSG) to allow
* official MongoDB drivers to connect and perform operations. * official MongoDB drivers to connect and perform operations.
* *
* @example * @example
* ```typescript * ```typescript
* import { CongoServer } from '@push.rocks/smartmongo/congodb'; * import { TsmdbServer } from '@push.rocks/smartmongo/tsmdb';
* import { MongoClient } from 'mongodb'; * import { MongoClient } from 'mongodb';
* *
* const server = new CongoServer({ port: 27017 }); * const server = new TsmdbServer({ port: 27017 });
* await server.start(); * await server.start();
* *
* const client = new MongoClient('mongodb://127.0.0.1:27017'); * const client = new MongoClient('mongodb://127.0.0.1:27017');
* await client.connect(); * await client.connect();
* ``` * ```
*/ */
export class CongoServer { export class TsmdbServer {
private options: Required<ICongoServerOptions>; private options: Required<ITsmdbServerOptions>;
private server: net.Server | null = null; private server: net.Server | null = null;
private storage: IStorageAdapter; private storage: IStorageAdapter;
private commandRouter: CommandRouter; private commandRouter: CommandRouter;
@@ -63,7 +63,7 @@ export class CongoServer {
private isRunning = false; private isRunning = false;
private startTime: Date = new Date(); private startTime: Date = new Date();
constructor(options: ICongoServerOptions = {}) { constructor(options: ITsmdbServerOptions = {}) {
this.options = { this.options = {
port: options.port ?? 27017, port: options.port ?? 27017,
host: options.host ?? '127.0.0.1', host: options.host ?? '127.0.0.1',

View File

@@ -1,4 +1,4 @@
import * as plugins from '../congodb.plugins.js'; import * as plugins from '../tsmdb.plugins.js';
/** /**
* MongoDB Wire Protocol Implementation * MongoDB Wire Protocol Implementation

View File

@@ -1,4 +1,4 @@
import * as plugins from '../../congodb.plugins.js'; import * as plugins from '../../tsmdb.plugins.js';
import type { ICommandHandler, IHandlerContext } from '../CommandRouter.js'; import type { ICommandHandler, IHandlerContext } from '../CommandRouter.js';
/** /**
@@ -246,7 +246,7 @@ export class AdminHandler implements ICommandHandler {
ok: 1, ok: 1,
host: `${server.host}:${server.port}`, host: `${server.host}:${server.port}`,
version: '7.0.0', version: '7.0.0',
process: 'congodb', process: 'tsmdb',
pid: process.pid, pid: process.pid,
uptime, uptime,
uptimeMillis: uptime * 1000, uptimeMillis: uptime * 1000,
@@ -269,7 +269,7 @@ export class AdminHandler implements ICommandHandler {
numRequests: 0, numRequests: 0,
}, },
storageEngine: { storageEngine: {
name: 'congodb', name: 'tsmdb',
supportsCommittedReads: true, supportsCommittedReads: true,
persistent: false, persistent: false,
}, },
@@ -283,7 +283,7 @@ export class AdminHandler implements ICommandHandler {
return { return {
ok: 1, ok: 1,
version: '7.0.0', version: '7.0.0',
gitVersion: 'congodb', gitVersion: 'tsmdb',
modules: [], modules: [],
allocator: 'system', allocator: 'system',
javascriptEngine: 'none', javascriptEngine: 'none',
@@ -294,7 +294,7 @@ export class AdminHandler implements ICommandHandler {
compiled: 'disabled', compiled: 'disabled',
}, },
buildEnvironment: { buildEnvironment: {
distmod: 'congodb', distmod: 'tsmdb',
distarch: process.arch, distarch: process.arch,
cc: '', cc: '',
ccflags: '', ccflags: '',
@@ -307,7 +307,7 @@ export class AdminHandler implements ICommandHandler {
bits: 64, bits: 64,
debug: false, debug: false,
maxBsonObjectSize: 16777216, maxBsonObjectSize: 16777216,
storageEngines: ['congodb'], storageEngines: ['tsmdb'],
}; };
} }

View File

@@ -1,4 +1,4 @@
import * as plugins from '../../congodb.plugins.js'; import * as plugins from '../../tsmdb.plugins.js';
import type { ICommandHandler, IHandlerContext, ICursorState } from '../CommandRouter.js'; import type { ICommandHandler, IHandlerContext, ICursorState } from '../CommandRouter.js';
import { AggregationEngine } from '../../engine/AggregationEngine.js'; import { AggregationEngine } from '../../engine/AggregationEngine.js';

View File

@@ -1,4 +1,4 @@
import * as plugins from '../../congodb.plugins.js'; import * as plugins from '../../tsmdb.plugins.js';
import type { ICommandHandler, IHandlerContext } from '../CommandRouter.js'; import type { ICommandHandler, IHandlerContext } from '../CommandRouter.js';
import { QueryEngine } from '../../engine/QueryEngine.js'; import { QueryEngine } from '../../engine/QueryEngine.js';

View File

@@ -1,4 +1,4 @@
import * as plugins from '../../congodb.plugins.js'; import * as plugins from '../../tsmdb.plugins.js';
import type { ICommandHandler, IHandlerContext, ICursorState } from '../CommandRouter.js'; import type { ICommandHandler, IHandlerContext, ICursorState } from '../CommandRouter.js';
import { QueryEngine } from '../../engine/QueryEngine.js'; import { QueryEngine } from '../../engine/QueryEngine.js';

View File

@@ -1,4 +1,4 @@
import * as plugins from '../../congodb.plugins.js'; import * as plugins from '../../tsmdb.plugins.js';
import type { ICommandHandler, IHandlerContext } from '../CommandRouter.js'; import type { ICommandHandler, IHandlerContext } from '../CommandRouter.js';
/** /**

View File

@@ -1,4 +1,4 @@
import * as plugins from '../../congodb.plugins.js'; import * as plugins from '../../tsmdb.plugins.js';
import type { ICommandHandler, IHandlerContext } from '../CommandRouter.js'; import type { ICommandHandler, IHandlerContext } from '../CommandRouter.js';
import { IndexEngine } from '../../engine/IndexEngine.js'; import { IndexEngine } from '../../engine/IndexEngine.js';

View File

@@ -1,4 +1,4 @@
import * as plugins from '../../congodb.plugins.js'; import * as plugins from '../../tsmdb.plugins.js';
import type { ICommandHandler, IHandlerContext } from '../CommandRouter.js'; import type { ICommandHandler, IHandlerContext } from '../CommandRouter.js';
/** /**

View File

@@ -1,4 +1,4 @@
import * as plugins from '../../congodb.plugins.js'; import * as plugins from '../../tsmdb.plugins.js';
import type { ICommandHandler, IHandlerContext } from '../CommandRouter.js'; import type { ICommandHandler, IHandlerContext } from '../CommandRouter.js';
import { QueryEngine } from '../../engine/QueryEngine.js'; import { QueryEngine } from '../../engine/QueryEngine.js';
import { UpdateEngine } from '../../engine/UpdateEngine.js'; import { UpdateEngine } from '../../engine/UpdateEngine.js';

View File

@@ -1,7 +1,7 @@
// Server module exports // Server module exports
export { CongoServer } from './CongoServer.js'; export { TsmdbServer } from './TsmdbServer.js';
export type { ICongoServerOptions } from './CongoServer.js'; export type { ITsmdbServerOptions } from './TsmdbServer.js';
export { WireProtocol } from './WireProtocol.js'; export { WireProtocol } from './WireProtocol.js';
export { CommandRouter } from './CommandRouter.js'; export { CommandRouter } from './CommandRouter.js';
export type { ICommandHandler, IHandlerContext, ICursorState } from './CommandRouter.js'; export type { ICommandHandler, IHandlerContext, ICursorState } from './CommandRouter.js';

View File

@@ -1,9 +1,9 @@
import * as plugins from '../congodb.plugins.js'; import * as plugins from '../tsmdb.plugins.js';
import type { IStorageAdapter } from './IStorageAdapter.js'; import type { IStorageAdapter } from './IStorageAdapter.js';
import type { IStoredDocument, IOpLogEntry, Document } from '../types/interfaces.js'; import type { IStoredDocument, IOpLogEntry, Document } from '../types/interfaces.js';
/** /**
* File-based storage adapter for CongoDB * File-based storage adapter for TsmDB
* Stores data in JSON files on disk for persistence * Stores data in JSON files on disk for persistence
*/ */
export class FileStorageAdapter implements IStorageAdapter { export class FileStorageAdapter implements IStorageAdapter {

View File

@@ -1,8 +1,8 @@
import type * as plugins from '../congodb.plugins.js'; import type * as plugins from '../tsmdb.plugins.js';
import type { IStoredDocument, IOpLogEntry, Document } from '../types/interfaces.js'; import type { IStoredDocument, IOpLogEntry, Document } from '../types/interfaces.js';
/** /**
* Storage adapter interface for CongoDB * Storage adapter interface for TsmDB
* Implementations can provide different storage backends (memory, file, etc.) * Implementations can provide different storage backends (memory, file, etc.)
*/ */
export interface IStorageAdapter { export interface IStorageAdapter {

View File

@@ -1,9 +1,9 @@
import * as plugins from '../congodb.plugins.js'; import * as plugins from '../tsmdb.plugins.js';
import type { IStorageAdapter } from './IStorageAdapter.js'; import type { IStorageAdapter } from './IStorageAdapter.js';
import type { IStoredDocument, IOpLogEntry, Document } from '../types/interfaces.js'; import type { IStoredDocument, IOpLogEntry, Document } from '../types/interfaces.js';
/** /**
* In-memory storage adapter for CongoDB * In-memory storage adapter for TsmDB
* Optionally supports persistence to a file * Optionally supports persistence to a file
*/ */
export class MemoryStorageAdapter implements IStorageAdapter { export class MemoryStorageAdapter implements IStorageAdapter {

View File

@@ -1,4 +1,4 @@
import * as plugins from '../congodb.plugins.js'; import * as plugins from '../tsmdb.plugins.js';
import type { IStorageAdapter } from './IStorageAdapter.js'; import type { IStorageAdapter } from './IStorageAdapter.js';
import type { IOpLogEntry, Document, IResumeToken, ChangeStreamOperationType } from '../types/interfaces.js'; import type { IOpLogEntry, Document, IResumeToken, ChangeStreamOperationType } from '../types/interfaces.js';

View File

@@ -1,4 +1,4 @@
import type * as plugins from '../congodb.plugins.js'; import type * as plugins from '../tsmdb.plugins.js';
// ============================================================================ // ============================================================================
// Document Types // Document Types
@@ -14,7 +14,7 @@ export interface WithId<TSchema> {
// Client Options // Client Options
// ============================================================================ // ============================================================================
export interface ICongoClientOptions { export interface ITsmdbClientOptions {
/** Storage adapter type: 'memory' or 'file' */ /** Storage adapter type: 'memory' or 'file' */
storageType?: 'memory' | 'file'; storageType?: 'memory' | 'file';
/** Path for file-based storage */ /** Path for file-based storage */
@@ -30,7 +30,7 @@ export interface ICongoClientOptions {
// ============================================================================ // ============================================================================
export interface IParsedConnectionString { export interface IParsedConnectionString {
protocol: 'congo'; protocol: 'tsmdb';
storageType: 'memory' | 'file'; storageType: 'memory' | 'file';
options: { options: {
persist?: string; persist?: string;