mongoose connection pool code example

Example 1: mongoose database pooling

import mongoose from 'mongoose'

	mongoose.connect(process.env.MONGO_URI, {
		minPoolSize: 1,
		maxPoolSize: 20,
		compression: { compressors: ['zlib'] },
		useUnifiedTopology: true,
		useNewUrlParser: true,
		useFindAndModify: false,
		socketTimeoutMS: 60000,
		serverSelectionTimeoutMS: 60000,
		loggerLevel: 'error'
	})

	mongoose.connection.on('connecting', () => consola.info('database connecting'))
	mongoose.connection.on('connected', () => consola.success('database connected'))
	mongoose.connection.on('disconnecting', () => consola.info('database disconnecting'))
	mongoose.connection.on('disconnected', () => consola.info('database disconnected'))
	mongoose.connection.on('error', () => consola.error('database error'))

Example 2: mongoose connection node

mongoose.connect('mongodb://localhost/myapp', { useNewUrlParser: true, useUnifiedTopology: true });

Example 3: mongoose.connect

mongoose.connect('mongodb://localhost:27017/myapp', {useNewUrlParser: true});