Role

Defines a user's permissions.

We'll store the possible roles for a user in an enum. So first, create the file auth -> roles -> enums -> role.enum with following content.

export enum Role {
  ADMIN = 'ADMIN',
  MANAGER = 'MANAGER',
  USER = 'USER',
}

Now, let's go back to the User entity and declare a role for it. This is how we can map an enum to the database. Also notice the default value.

@Column({
  type: 'enum',
  enum: Role,
  enumName: 'role_enum',
  default: Role.USER,
})
role: Role;

The enumName option replaces the default name, which follows the pattern "className"_"fieldName"_enum. It would be user_role_enum in this case.

Now, generate and run the migration add-role-to-user. Existing users will receive the USER role.

Commit - Creating role enum

Last updated