create-tables.ts 1.55 KB
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
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
import mongoose from "mongoose";
import { mongoUri } from "../config";
import { userDb, roleDb } from "../db";
import { Role, User } from "../models";

mongoose
  .connect(mongoUri)
  .then(async (mongoose) => {
    // 기존 역할들 모두 삭제
    // console.log("clearing existing all roles");
    // await Role.deleteMany();
    // 기본 역할 생성
    console.log("creating default roles");
    const roles = [
      { name: "admin", priority: 1 },
      { name: "manager", priority: 10 },
      { name: "staff", priority: 100 },
      { name: "user", priority: 1000 },
      { name: "guest", priority: 10000 },
    ];
    // await Role.create(roles);
    const asyncRoles = roles.map(async (role) => {
      console.log("creating role:", role.name, "...");
      await Role.findOneAndUpdate({ name: role.name }, role, { upsert: true });
    });

    await Promise.all(asyncRoles);

    // 기존 사용자 모두 삭제
    // console.log("clearing all users");
    // await User.deleteMany();
    // admin 유저 생성
    console.log("creating the admin user");
    const adminRole = await roleDb.findRoleByName("admin");
    if (!adminRole) {
      throw new Error("admin role not exists");
    }
    const adminUser = {
      email: "admin@example.com",
      password: "asdfasdf",
      name: "Administrator",
      role: adminRole._id,
    };
    await User.findOneAndUpdate({ email: adminUser.email }, adminUser, {
      upsert: true,
    });

    mongoose.connection.close();
  })
  .catch((err) => console.log("mongoose connection error:", err));

export default {};