Files
asan-service/server/database.js
T
Mr Swift 71e1e4b7da set
2024-07-12 23:20:47 +03:30

34 lines
1.0 KiB
JavaScript

/* eslint-disable no-unused-vars */
const mongoose = require('mongoose')
// mongodb database connection string. change it as per your needs. here "mydb" is the name of the database. You don't need to create DB from mongodb terminal. mongoose create the database automatically.
const inHostUrl = 'mongodb://root:KPTt76tImNBBMm4Kor8QA9gB@asan-service:27017/asanserv_database?authSource=admin'
const init = ()=>{
try {
mongoose.connect(inHostUrl, {
useNewUrlParser: true,
useUnifiedTopology: true,
useFindAndModify: false,
useCreateIndex: true
})
} catch (error) {
init()
}
}
init()
mongoose.plugin(schema => {
schema.set('toObject', { getters: true })
schema.set('toJSON', { getters: true })
schema.set('timestamps', {
createdAt: 'created_at',
updatedAt: 'updated_at'
})
})
const database = mongoose.connection
database.on('error', console.error.bind(console, 'connection error:'))
database.once('open', function callback() {
console.log('🟠 ~ MongoDB Connected...')
})
module.exports = database