Quick Start Guide
This Guide will show how to setup this package for basic use
note
The Examples on this page assume Top-Level async-await for readability.
If Top-Level async-await is not available for your case, the code can in most cases be wrapped like:
(async () => {
// async code in where
})();
Requirements
- NodeJS: 16.20.1+
- Typescript: 5.3+ (if used)
When on Linux, one of the following are required:
- having
lsb-core
installed (or any that provides thelsb_release
command) - having an
/etc/os-release
file that is compliant to the OS-Release Spec - having an
/etc/*-release
file that is compliant to the OS-Release Spec (and does not includelsb
) - manually specify which version & system should be used
On Linux, you will also need libcurl
(or libcurl3
on some older distro versions). This will probably only be an issue on "slim" Docker images.
Choose the right package
There are multiple packages for this project, here are the differences:
mongodb-memory-server-core
is the main package, which does not have any hooks on installmongodb-memory-server
adds hooks to install onyarn install
ornpm install
to install the latest package locallymongodb-memory-server-global
adds hooks to install onyarn install
ornpm install
to install the latest package globally (into $HOME)mongodb-memory-server-global-X.X
adds hooks to install onyarn install
ornpm install
to install MongoDB with versionX.X
globally (into $HOME)
Normal Server
A Normal Server can be easily started with:
import { MongoMemoryServer } from 'mongodb-memory-server';
// This will create an new instance of "MongoMemoryServer" and automatically start it
const mongod = await MongoMemoryServer.create();
const uri = mongod.getUri();
// The Server can be stopped again with
await mongod.stop();
ReplicaSet
A ReplicaSet can be easily started with:
import { MongoMemoryReplSet } from 'mongodb-memory-server';
// This will create an new instance of "MongoMemoryReplSet" and automatically start all Servers
const replset = await MongoMemoryReplSet.create({ replSet: { count: 4 } }); // This will create an ReplSet with 4 members
// To use Transactions, the "storageEngine" needs to be changed to `wiredTiger`
const replset = await MongoMemoryReplSet.create({ replSet: { count: 4, storageEngine: 'wiredTiger' } }); // This will create an ReplSet with 4 members and storage-engine "wiredTiger"
const uri = replset.getUri();
// The ReplSet can be stopped again with
await replset.stop();
Final Notes
danger
When you are not using a global instance for tests and run multi-threaded tests, it can cause Race Conditions.