forked from RishiDesai17/T040_Winsoft
-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.js
34 lines (33 loc) · 1.11 KB
/
db.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
const mongoose = require('mongoose');
const bcrypt = require('bcryptjs');
const Map = require('./models/map');
const User = require('./models/user');
const map = require('./sample_map.json');
exports.dbInit = async() => {
try{
await mongoose.connect("mongodb+srv://newUser_123:test123@t040-winsoft.ifd0h.mongodb.net/test?retryWrites=true&w=majority", {
useUnifiedTopology: true,
useNewUrlParser: true
});
const usersCount = await User.countDocuments()
if(usersCount === 0){
console.log("added sample user")
const hash = await bcrypt.hash('1234', 10)
await new User({
username: 'indian army',
password: hash
}).save()
}
const count = await Map.countDocuments()
if(count === 0){
console.log("Seeded the database with sample map")
await new Map({
title: "Map 1",
map
}).save() // One database entry for a default map with title Map 1
}
}
catch(error){
console.log(error)
}
}