Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to setup initial data in mongodb-memory-server?

I am looking into using mongodb-memory-server: https://github.com/nodkz/mongodb-memory-server for integration/unit tests in a node application.

For a lot of the tests it would be neat (or even necessary) to have some existing data in the database before running the test. One way to populate the database would be to make a general script or function, that could create all the test documents, and call that script before each test case, but I am afraid the script will be messy, and it will be difficult to create all references between documents etc.

Therefore, I am trying to figure out if I can create a complete dump of a sandbox database (maybe a subset of our already existing production database) that I can initialize the mongodb-memory-server with before each test case?

Is that possible? and is there maybe a better way of doing this?

like image 340
Jolle Avatar asked Jul 30 '26 11:07

Jolle


1 Answers

Got an answer on github: https://github.com/nodkz/mongodb-memory-server/issues/309

It turns out that there's no build in way to initialize the database with a given dbdump. It is suggested to make a script.

One hack though is to use the dbPath option, to control which folder the database is stored in, and then overwrite the folder.

like image 77
Jolle Avatar answered Aug 02 '26 01:08

Jolle



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!