2023-08-08 16:59:47 +02:00
# @push.rocks/smartmongo
2025-04-06 19:00:35 +00:00
2026-03-26 14:51:17 +00:00
A MongoDB memory server toolkit for testing and development — spin up real MongoDB replica sets on the fly with zero configuration. 🚀
2021-12-20 15:58:23 +01:00
2024-04-14 18:00:24 +02:00
## Install
```bash
2026-01-31 12:13:59 +00:00
pnpm add -D @push .rocks/smartmongo
2026-03-26 14:51:17 +00:00
# or
npm install @push .rocks/smartmongo --save-dev
2026-01-31 12:13:59 +00:00
```
## 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.
2026-03-26 14:51:17 +00:00
## What It Does
2026-01-31 12:13:59 +00:00
2026-03-26 14:51:17 +00:00
`@push.rocks/smartmongo` wraps [mongodb-memory-server ](https://github.com/nodkz/mongodb-memory-server ) to give you a **real MongoDB replica set ** running entirely in memory. It downloads and manages the MongoDB binary for you — just `createAndStart()` and you're good to go.
2026-02-03 16:42:49 +00:00
2026-03-26 14:51:17 +00:00
Perfect for:
2026-01-31 12:13:59 +00:00
2026-03-26 14:51:17 +00:00
- ⚡ **Integration tests ** that need 100% MongoDB compatibility
- 🧪 **CI/CD pipelines ** where you can't install MongoDB system-wide
- 🔬 **Development environments ** that need a disposable database
- 📦 **Data dump/export ** with built-in support for dumping collections to disk
2021-12-20 15:58:23 +01:00
2026-03-26 14:51:17 +00:00
> 💡 **Looking for a lightweight, pure-TypeScript alternative?** Check out [`@push.rocks/smartdb`](https://code.foss.global/push.rocks/smartdb) — a wire-protocol-compatible MongoDB server with zero binary dependencies, instant startup, and file-based persistence.
2026-01-31 12:13:59 +00:00
2026-03-26 14:51:17 +00:00
## Quick Start
2026-02-03 08:14:58 +00:00
```typescript
import { SmartMongo } from '@push .rocks/smartmongo';
2026-03-26 14:51:17 +00:00
// Start a MongoDB replica set (downloads binary automatically on first run)
2026-02-03 08:14:58 +00:00
const mongo = await SmartMongo.createAndStart();
2026-03-26 14:51:17 +00:00
// Get connection details for your app or ORM
2026-02-03 08:14:58 +00:00
const descriptor = await mongo.getMongoDescriptor();
2026-03-26 14:51:17 +00:00
console.log(descriptor.mongoDbUrl);
// => mongodb://127.0.0.1:xxxxx/?replicaSet=testset
2026-02-03 08:14:58 +00:00
2026-03-26 14:51:17 +00:00
// Use with any MongoDB client
2026-02-03 16:42:49 +00:00
import { MongoClient } from 'mongodb';
2026-03-26 14:51:17 +00:00
const client = new MongoClient(descriptor.mongoDbUrl);
2026-02-03 16:42:49 +00:00
await client.connect();
2026-03-26 14:51:17 +00:00
const db = client.db(descriptor.mongoDbName);
await db.collection('users').insertOne({ name: 'Alice', role: 'admin' });
2026-02-03 08:14:58 +00:00
2026-03-26 14:51:17 +00:00
const user = await db.collection('users').findOne({ name: 'Alice' });
console.log(user); // { _id: ObjectId(...), name: 'Alice', role: 'admin' }
2026-02-03 08:14:58 +00:00
2026-03-26 14:51:17 +00:00
// Clean up
2026-02-03 16:42:49 +00:00
await client.close();
2026-03-26 14:51:17 +00:00
await mongo.stop();
2026-02-03 08:14:58 +00:00
```
2026-03-26 14:51:17 +00:00
## API
2026-02-03 16:48:50 +00:00
2026-03-26 14:51:17 +00:00
### `SmartMongo.createAndStart(replCount?: number)`
2024-04-14 18:00:24 +02:00
2026-03-26 14:51:17 +00:00
Static factory method that creates and starts a SmartMongo instance.
2024-04-14 18:00:24 +02:00
```typescript
2026-03-26 14:51:17 +00:00
// Single replica (default)
2026-01-31 12:13:59 +00:00
const mongo = await SmartMongo.createAndStart();
2026-03-26 14:51:17 +00:00
// Multi-replica for testing replication scenarios
2026-01-31 12:13:59 +00:00
const mongo = await SmartMongo.createAndStart(3);
```
2026-03-26 14:51:17 +00:00
### `getMongoDescriptor()`
Returns an `IMongoDescriptor` with the connection URL and database name, compatible with `@push.rocks/smartdata` and other push.rocks modules.
2026-01-31 12:13:59 +00:00
```typescript
const descriptor = await mongo.getMongoDescriptor();
// {
// mongoDbName: 'smartmongo_testdatabase',
// mongoDbUrl: 'mongodb://127.0.0.1:xxxxx/?replicaSet=testset'
// }
```
2026-03-26 14:51:17 +00:00
### `stop()`
2026-01-31 12:13:59 +00:00
2026-03-26 14:51:17 +00:00
Stops the replica set and cleans up all resources (temporary files, processes).
2026-02-03 16:48:50 +00:00
2026-01-31 12:13:59 +00:00
```typescript
2026-03-26 14:51:17 +00:00
await mongo.stop();
2026-02-01 16:05:00 +00:00
```
2026-03-26 14:51:17 +00:00
### `stopAndDumpToDir(dir, nameFunction?, emptyDir?)`
2026-02-01 16:05:00 +00:00
2026-03-26 14:51:17 +00:00
Stops the replica set **and ** dumps all collections to a directory on disk before cleanup. Useful for debugging or archiving test data.
2026-02-01 16:05:00 +00:00
```typescript
2026-03-26 14:51:17 +00:00
// Dump all collections with default naming
await mongo.stopAndDumpToDir('./test-output');
2026-02-01 16:05:00 +00:00
2026-03-26 14:51:17 +00:00
// With custom file naming
await mongo.stopAndDumpToDir('./test-output', (doc) => `${doc.collection}-${doc._id}.bson` );
2026-02-01 16:05:00 +00:00
2026-03-26 14:51:17 +00:00
// Keep existing files in the directory (don't empty it first)
await mongo.stopAndDumpToDir('./test-output', undefined, false);
2026-02-01 16:05:00 +00:00
```
2026-03-26 14:51:17 +00:00
### `readyPromise`
2026-02-01 16:05:00 +00:00
2026-03-26 14:51:17 +00:00
A promise that resolves when the replica set is fully started and ready to accept connections.
2026-02-01 16:05:00 +00:00
```typescript
2026-03-26 14:51:17 +00:00
const mongo = new SmartMongo();
mongo.start(2); // non-blocking
await mongo.readyPromise; // wait for startup
2026-02-01 16:05:00 +00:00
```
2026-03-26 08:02:04 +00:00
## Testing Examples
2024-04-14 18:00:24 +02:00
2026-03-26 14:51:17 +00:00
### With @git.zone/tstest (tapbundle)
2024-04-14 18:00:24 +02:00
2026-01-31 12:13:59 +00:00
```typescript
import { expect, tap } from '@git .zone/tstest/tapbundle';
2026-03-26 14:51:17 +00:00
import { SmartMongo } from '@push .rocks/smartmongo';
2026-02-03 16:42:49 +00:00
import { MongoClient } from 'mongodb';
2026-01-31 12:13:59 +00:00
2026-03-26 14:51:17 +00:00
let mongo: SmartMongo;
2026-02-03 16:42:49 +00:00
let client: MongoClient;
2026-01-31 12:13:59 +00:00
tap.test('setup', async () => {
2026-03-26 14:51:17 +00:00
mongo = await SmartMongo.createAndStart();
const { mongoDbUrl, mongoDbName } = await mongo.getMongoDescriptor();
client = new MongoClient(mongoDbUrl);
2026-02-03 16:42:49 +00:00
await client.connect();
2026-01-31 12:13:59 +00:00
});
2026-03-26 14:51:17 +00:00
tap.test('should insert and query documents', async () => {
2026-02-03 08:14:58 +00:00
const col = client.db('test').collection('items');
2026-03-26 14:51:17 +00:00
await col.insertOne({ name: 'Widget', price: 9.99 });
2024-04-14 18:00:24 +02:00
2026-01-31 12:13:59 +00:00
const item = await col.findOne({ name: 'Widget' });
expect(item?.price).toEqual(9.99);
});
tap.test('teardown', async () => {
2026-02-03 16:42:49 +00:00
await client.close();
2026-03-26 14:51:17 +00:00
await mongo.stop();
2026-01-31 12:13:59 +00:00
});
export default tap.start();
```
2026-03-26 14:51:17 +00:00
### With @push.rocks/smartdata
2026-02-03 16:48:50 +00:00
2026-03-26 14:51:17 +00:00
```typescript
import { SmartMongo } from '@push .rocks/smartmongo';
import { SmartdataDb } from '@push .rocks/smartdata';
2026-01-31 12:13:59 +00:00
2026-03-26 14:51:17 +00:00
const mongo = await SmartMongo.createAndStart();
const descriptor = await mongo.getMongoDescriptor();
2026-02-03 08:14:58 +00:00
2026-03-26 14:51:17 +00:00
const db = new SmartdataDb(descriptor);
await db.init();
2026-02-03 08:14:58 +00:00
2026-03-26 14:51:17 +00:00
// Use smartdata models against the memory server...
2026-01-31 12:13:59 +00:00
2026-03-26 14:51:17 +00:00
await db.close();
await mongo.stop();
2026-01-31 12:13:59 +00:00
```
2026-02-03 16:48:50 +00:00
2024-04-14 18:00:24 +02:00
## License and Legal Information
2026-03-26 14:51:17 +00:00
This repository contains open-source code licensed under the MIT License. A copy of the license can be found in the [LICENSE ](./LICENSE ) file.
2024-04-14 18:00:24 +02:00
**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
2026-03-26 14:51:17 +00:00
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.
2021-12-20 15:58:23 +01:00
2024-04-14 18:00:24 +02:00
### Company Information
2021-12-20 15:58:23 +01:00
2026-01-31 12:13:59 +00:00
Task Venture Capital GmbH
2026-03-26 14:51:17 +00:00
Registered at District Court Bremen HRB 35230 HB, Germany
2021-12-20 15:58:23 +01:00
2026-03-26 14:51:17 +00:00
For any legal inquiries or further information, please contact us via email at hello@task .vc.
2021-12-20 15:58:23 +01:00
2024-04-14 18:00:24 +02:00
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.