-
Notifications
You must be signed in to change notification settings - Fork 0
/
startup_script.js
79 lines (61 loc) · 1.6 KB
/
startup_script.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
const Sequelize = require('sequelize');
require('dotenv').config();
const mongoose = require('mongoose');
const User = require('./models/users');
const connHost = process.env.SQL_LOCAL_CONN_URL;
const connUser = process.env.SQL_USERNAME;
const connPass = process.env.SQL_PASSWORD;
const connUri = process.env.MONGO_LOCAL_CONN_URL;
const sequelize = new Sequelize('dvws_sqldb', connUser, connPass, {
host: connHost,
dialect: 'mysql'
});
console.log('[+] Creating MySQL database for DVWS....');
sequelize.query("DROP DATABASE IF EXISTS dvws_sqldb;")
.then(() => {
console.log("[+] Old SQL Database deleted");
return sequelize.query("CREATE DATABASE dvws_sqldb;");
})
.then(() => {
console.log("[+] SQL Database created");
sequelize.close();
createAdmin();
})
.catch(err => {
console.error(err);
sequelize.close();
});
function createAdmin() {
mongoose.connect(connUri, { useNewUrlParser : true, useUnifiedTopology: true }, (err) => {
let result = {};
const user = new User({
username: "admin",
password: "letmein",
admin: true
});
user.save((err, user) => {
if (!err) {
console.log(user);
} else {
result.error = err;
console.log(result.error);
}
// Close the connection after saving
});
const user2 = new User({
username: "test",
password: "test",
admin: false
});
user2.save((err, user2) => {
if (!err) {
console.log(user2);
} else {
result.error = err;
console.log(result.error);
}
// Close the connection after saving
mongoose.disconnect();
});
});
}