-
Notifications
You must be signed in to change notification settings - Fork 10
/
initDatabase.js
41 lines (37 loc) · 966 Bytes
/
initDatabase.js
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
const mongoose = require('mongoose')
const User = require('./models/User')
const dbConfig = require('./config/db.js')
const connectUri = dbConfig.getConnectionURI()
mongoose.connect(
connectUri,
{ useNewUrlParser: true },
{ useUnifiedTopology: true }
)
.then(
() => {
console.log('Connection with database established')
},
err => {
throw new Error(err)
}
)
const adminUser = {
name: 'SuperUser',
email: 'admin@admin.com',
status: 'enabled',
password: 'adminadmin'
}
User.findOne({ email: adminUser.email })
.then(user => {
if (!user) return User.create(adminUser)
else console.log('Skipping admin user creation, since is already created')
})
.then(createdUser => {
if (createdUser) console.log(`User ${createdUser.name} created successfully`)
})
.catch(err => {
console.log('There was an error creating the Superuser')
throw new Error(err)
}).finally(() => {
process.exit(0)
})