| 15 | import mongoose from 'mongoose'; |
| 16 | |
| 17 | export default function database(envConfig) { |
| 18 | const db_options = {}; |
| 19 | |
| 20 | if (Object.hasOwn(envConfig, 'mongodbSSLCA') && envConfig.mongodbSSLCA !== "") { |
| 21 | db_options['tlsCAFile'] = envConfig.mongodbSSLCA; |
| 22 | } |
| 23 | |
| 24 | // connect to the database |
| 25 | mongoose.connect(envConfig.database, db_options); |
| 26 | |
| 27 | // Use a better Promise provider |
| 28 | mongoose.Promise = global.Promise; |
| 29 | |
| 30 | // Preparing for Mongoose 7.0 |
| 31 | mongoose.set('strictQuery', false); |
| 32 | |
| 33 | // Acknowledge a successful connection to the console |
| 34 | mongoose.connection.on('connected', function () { |
| 35 | console.log('Mongoose default connection open'); |
| 36 | }); |
| 37 | |
| 38 | // Event handler for when the connection throws an error |
| 39 | mongoose.connection.on('error', function (err) { |
| 40 | console.log('Mongoose default connection error: ' + err); |
| 41 | }); |
| 42 | |
| 43 | // Event handler for when the connection is disconnected |
| 44 | mongoose.connection.on('disconnected', function () { |
| 45 | console.log('Mongoose default connection disconnected'); |
| 46 | }); |
| 47 | |
| 48 | // If the Node process ends, close the Mongoose connection |
| 49 | process.on('SIGINT', function () { |
| 50 | mongoose.connection.close().then(function () { |
| 51 | console.log('Mongoose default connection disconnected through app termination'); |
| 52 | process.exit(0); |
| 53 | }); |
| 54 | }); |
| 55 | }; |