role.entity.ts 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import { Entity, PrimaryGeneratedColumn, Column } from 'typeorm';
  2. import { z } from '@hono/zod-openapi';
  3. export type Permission = string;
  4. export const RoleSchema = z.object({
  5. id: z.number().int().positive().openapi({
  6. description: '角色ID',
  7. example: 1
  8. }),
  9. name: z.string().max(50).openapi({
  10. description: '角色名称,唯一标识',
  11. example: 'admin'
  12. }),
  13. description: z.string().max(500).nullable().openapi({
  14. description: '角色描述',
  15. example: '系统管理员角色'
  16. }),
  17. permissions: z.array(z.string()).min(1).openapi({
  18. description: '角色权限列表',
  19. example: ['user:create', 'user:delete']
  20. })
  21. });
  22. export const CreateRoleDto = RoleSchema.omit({ id: true });
  23. export const UpdateRoleDto = RoleSchema.partial();
  24. @Entity({ name: 'role' })
  25. export class Role {
  26. @PrimaryGeneratedColumn()
  27. id!: number;
  28. @Column({ type: 'varchar', length: 50, unique: true })
  29. name!: string;
  30. @Column({ type: 'text', nullable: true })
  31. description!: string | null;
  32. @Column({ type: 'simple-array', nullable: false })
  33. permissions: Permission[] = [];
  34. constructor(partial?: Partial<Role>) {
  35. Object.assign(this, partial);
  36. if (!this.permissions) {
  37. this.permissions = [];
  38. }
  39. }
  40. }