import mongoose from "mongoose"; import { MongoMemoryServer } from "mongodb-memory-server"; let mongo: MongoMemoryServer; /** * Connect to the in-memory database. */ export const connect = async () => { mongo = await MongoMemoryServer.create(); const uri = mongo.getUri(); const mongooseOpts = { useNewUrlParser: true, useUnifiedTopology: true, }; await mongoose.connect(uri, mongooseOpts); } /** * Drop database, close the connection and stop mongod. */ export const closeDatabase = async () => { await mongoose.connection.dropDatabase(); await mongoose.connection.close(); await mongo.stop(); } /** * Remove all the data for all db collections. */ export const clearDatabase = async () => { const collections = mongoose.connection.collections; for (const key in collections) { const collection = collections[key]; await collection.deleteMany({}); } }
of course, simply move MongoMemoryServer.create into the connect method, I should have find this.... Thanks a lot!
Thanks Emma!
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
of course, simply move MongoMemoryServer.create into the connect method, I should have find this.... Thanks a lot!
Thanks Emma!