typegoose / mongodb-memory-server

Manage & spin up mongodb server binaries with zero(or slight) configuration for tests.
https://typegoose.github.io/mongodb-memory-server/
MIT License
2.56k stars 185 forks source link

How to initialize data/collections/indexes on startup? #875

Closed thardy closed 3 months ago

thardy commented 3 months ago

I want to initialize my mongodb-memory-server with the some collections, indexes, and initial data for testing. I'd like to use the same file full of shell commands I use with my Docker deployment if possible. Many of my tests involve unique indexes and testing for proper responses to duplicate inserts, etc.

I saw the answers to this question - https://github.com/nodkz/mongodb-memory-server/issues/309, but they did not mention how to programmatically execute mongo commands from a file against the newly created MongoMemoryServer.

Any help will be greatly appreciated.

hasezoey commented 3 months ago

I saw the answers to this question - https://github.com/nodkz/mongodb-memory-server/issues/309, but they did not mention how to programmatically execute mongo commands from a file against the newly created MongoMemoryServer.

its the same way as you would against a normal manually run binary, the only difference is that you need to get the url / port from instance.getUri() as the port is random (unless set specifically).

please note that MMS only installs the server binary (mongod) not other binaries like mongo or mongoimport / mongoexport (or other binaries that may exist).

instead of mongo you could use the mongo nodejs driver instead.