Created
February 1, 2020 20:20
-
-
Save jonathangaldino/c2a024e7122729f63dcb75b7c80b227d to your computer and use it in GitHub Desktop.
Usando o mongodb-memory-server
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const mongoose = require("mongoose"); | |
const { MongoMemoryServer } = require("mongodb-memory-server"); | |
let mongoServer = null; | |
const connect = async () => { | |
mongoose.Promise = global.Promise; | |
mongoServer = new MongoMemoryServer(); | |
const mongoUri = await mongoServer.getUri(); | |
await mongoose.connect(mongoUri, { | |
useNewUrlParser: true, | |
useUnifiedTopology: true, | |
useFindAndModify: false, | |
useCreateIndex: true | |
}); | |
}; | |
const disconnect = async () => { | |
await mongoose.disconnect(); | |
await mongoServer.stop(); | |
}; | |
// mongoose.connection | |
// .on("open", () => console.log("Connection opened")) | |
// .on("error", error => console.log(error)) | |
// .on("close", () => console.log("Connection closed")) | |
// .on("connected", () => console.log("Database connected")) | |
// .on("disconnected", () => console.log("Database disconnected")); | |
module.exports = { connect, disconnect }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment