-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreateAdmin.js
45 lines (41 loc) · 981 Bytes
/
createAdmin.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
import prompt from "prompt";
import UserModel from "./models/User.js";
import dbConnect from "./utils/dbConnection.js";
const adminModel = [
{
name: "username",
description: "Enter your username",
validator: /^[a-zA-Z\s-]+$/,
warning: "Username must be only letters, spaces, or dashes",
required: true,
type: "string",
},
{
name: "password",
description: "Enter your password",
hidden: true,
required: true,
replace: "*",
},
];
dbConnect();
prompt.start();
prompt.get(adminModel, async (err, result) => {
if (err) {
return onErr(err);
}
try {
const { username, password } = result;
const role = "admin";
const created = await UserModel.create({ username, password, role });
console.log(created);
if(created) console.log({username, id:created._id})
} catch (error) {
console.log({ error : error.message });
}
process.exit(0);
});
function onErr(err) {
console.log(err);
return 1;
}