Skip to content

Instantly share code, notes, and snippets.

@jonathangaldino
Created February 1, 2020 20:20
Show Gist options
  • Save jonathangaldino/c2a024e7122729f63dcb75b7c80b227d to your computer and use it in GitHub Desktop.
Save jonathangaldino/c2a024e7122729f63dcb75b7c80b227d to your computer and use it in GitHub Desktop.
Usando o mongodb-memory-server
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