-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.js
More file actions
42 lines (33 loc) · 889 Bytes
/
database.js
File metadata and controls
42 lines (33 loc) · 889 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
const mongoose = require('mongoose')
const url = 'mongodb://localhost:27017/mobitech'
let conectado = false
const conectar = async () => {
if (!conectado) {
try {
await mongoose.connect(url)
conectado = true
console.log("MongoDB conectado")
return true
} catch (error) {
console.log(error)
if (error.code = 8000) {
console.log("Erro de autenticação")
} else {
console.log(error)
}
}
}
}
const desconectar = async () => {
if (conectado) {
try {
await mongoose.disconnect(url)
conectado = false
console.log("MongoDB desconectado")
return true
} catch (error) {
console.log(error)
}
}
}
module.exports = { conectar, desconectar }