role.model.js 1.68 KB
Newer Older
1
2
3
4
import Sequelize from "sequelize";

const { DataTypes } = Sequelize;

5
6
7
8
9
10
export const ROLE_NAME = {
    USER : "user",
    MEMBER : "member",
    ADMIN : "admin",
    ROOT : "root",
}
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

const RoleModel = (sequelize) => {
    const Role = sequelize.define(
        "role",
        {
            id: {
                type: DataTypes.INTEGER,
                primaryKey: true,
                autoIncrement: true,
            },
            name: {
                type: DataTypes.ENUM({
                    values: Object.values(ROLE_NAME),
                }),
                defaultValue: ROLE_NAME.USER,
                set(value) {
                    const lowerCaseName = value.toLowerCase();
                    this.setDataValue("name", lowerCaseName);
                    switch (lowerCaseName) {
                        case ROLE_NAME.USER:
                            this.priority = 1000;
                            break;
                        case ROLE_NAME.MEMBER:
                            this.priority = 500;
                            break;
                        case ROLE_NAME.ADMIN:
                            this.priority = 50;
                            break;
                        case ROLE_NAME.ROOT:
                            this.priority = 1;
                            break;
                        default:
                            this.priority = 1000;
                            break;
                    }
                },
            },
            priority: {
                type: DataTypes.INTEGER,
                defaultValue: 1000,
            },
        },
        {
            timestamps: true,
        }
    );
    return Role;
};

export default RoleModel;