create.admin.ts 815 Bytes
Newer Older
Yoon, Daeki's avatar
Yoon, Daeki committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import { connect } from "mongoose";
import { mongoUri } from "../src/config";
import { Role, User } from "../src/models";
import { userDb } from "../src/db";

const roles = [
  ["admin", 1],
  ["manager", 10],
  ["staff", 100],
  ["user", 1000],
  ["guest", 10000],
];

connect(mongoUri)
  .then(async (mongoose) => {
    const adminRole = await Role.findOne({ name: "admin" });
Yoon, Daeki's avatar
Yoon, Daeki committed
17
18
19
    if (!adminRole) {
      throw new Error("admin role이 없습니다. 먼저 role 테이블을 만드세요.");
    }
Yoon, Daeki's avatar
Yoon, Daeki committed
20
21
22
23
24
25
    await userDb.createUser({
      email: "admin@example.com",
      name: "admin",
      role: adminRole?._id,
      password: "asdfasdf",
    });
Yoon, Daeki's avatar
Yoon, Daeki committed
26
    console.log("admin 계정이 만들어졌습니다.");
Yoon, Daeki's avatar
Yoon, Daeki committed
27
28
29
    await mongoose.disconnect();
  })
  .catch((error) => console.log("롤 초기 생성 에러", error));