-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdatabase.js
More file actions
29 lines (25 loc) · 851 Bytes
/
database.js
File metadata and controls
29 lines (25 loc) · 851 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
const mongoose = require("mongoose");
const Admin = require("./models/admin");
const bcrypt = require("bcrypt");
mongoose
.connect(process.env.MONGO_URI, { useNewUrlParser: true })
.then(() => mongoose.connection.db.listCollections().toArray())
.then(collections => collections.map(col => col.name).length)
.then(numOfCollections => !numOfCollections && createDefaultUser())
.catch(console.log);
mongoose.set("useFindAndModify", false);
mongoose.set("useCreateIndex", true);
process.on("SIGINT", () => {
mongoose.connection.close().then(() => process.exit(0));
});
function createDefaultUser() {
const defaultAdmin = new Admin({
firstName: "Default",
lastName: "Admin",
email: "admin@example.com",
password: bcrypt.hashSync("password", 10),
active: true,
allowEdit: true
});
return defaultAdmin.save();
}