Skip to content

Instantly share code, notes, and snippets.

@tarzak
Forked from emilioriosvz/mongoose-connection.js
Created August 19, 2019 18:15
Show Gist options
  • Save tarzak/ddef662ff026297f34a0585a8553b647 to your computer and use it in GitHub Desktop.
Save tarzak/ddef662ff026297f34a0585a8553b647 to your computer and use it in GitHub Desktop.
connect with mongoose using async/await
const mongoose = require('mongoose')
mongoose.Promise = Promise
mongoose.connection.on('connected', () => {
console.log('Connection Established')
})
mongoose.connection.on('reconnected', () => {
console.log('Connection Reestablished')
})
mongoose.connection.on('disconnected', () => {
console.log('Connection Disconnected')
})
mongoose.connection.on('close', () => {
console.log('Connection Closed')
})
mongoose.connection.on('error', (error) => {
console.log('ERROR: ' + error)
})
const run = async () => {
await mongoose.connect('mongodb://localhost:27017/emilioriosvz', {
autoReconnect: true,
reconnectTries: 1000000,
reconnectInterval: 3000
})
}
run().catch(error => console.error(error))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment