refactor:替换自增id为cuid

This commit is contained in:
2024-11-27 15:25:57 +08:00
parent b02e2d2160
commit 5b382a124a
27 changed files with 161 additions and 114 deletions

View File

@@ -1,2 +0,0 @@
-- AlterTable
ALTER TABLE "sys_menu" ADD COLUMN "sort" INTEGER NOT NULL DEFAULT 0;

View File

@@ -1,22 +1,24 @@
-- CreateTable
CREATE TABLE "sys_user" (
"id" SERIAL NOT NULL,
"id" TEXT NOT NULL,
"created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updated_at" TIMESTAMP(3) NOT NULL,
"name" TEXT,
"account" TEXT NOT NULL,
"password" TEXT NOT NULL,
"password_salt" TEXT NOT NULL,
"status" SMALLINT NOT NULL DEFAULT 0,
"avatar_id" INTEGER,
"avatar_id" TEXT,
"gender" SMALLINT NOT NULL DEFAULT 0,
"created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updated_at" TIMESTAMP(3) NOT NULL,
CONSTRAINT "sys_user_pkey" PRIMARY KEY ("id")
);
-- CreateTable
CREATE TABLE "sys_role" (
"id" SERIAL NOT NULL,
"id" TEXT NOT NULL,
"created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updated_at" TIMESTAMP(3) NOT NULL,
"code" TEXT NOT NULL,
"name" TEXT NOT NULL,
"status" SMALLINT NOT NULL DEFAULT 0,
@@ -26,26 +28,31 @@ CREATE TABLE "sys_role" (
-- CreateTable
CREATE TABLE "sys_user_role" (
"id" SERIAL NOT NULL,
"user_id" INTEGER NOT NULL,
"role_id" INTEGER NOT NULL,
"id" TEXT NOT NULL,
"created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updated_at" TIMESTAMP(3) NOT NULL,
"user_id" TEXT NOT NULL,
"role_id" TEXT NOT NULL,
CONSTRAINT "sys_user_role_pkey" PRIMARY KEY ("id")
);
-- CreateTable
CREATE TABLE "files" (
"id" SERIAL NOT NULL,
CREATE TABLE "file" (
"id" TEXT NOT NULL,
"created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updated_at" TIMESTAMP(3) NOT NULL,
"name" TEXT NOT NULL,
"path" TEXT NOT NULL,
"created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
CONSTRAINT "files_pkey" PRIMARY KEY ("id")
CONSTRAINT "file_pkey" PRIMARY KEY ("id")
);
-- CreateTable
CREATE TABLE "sys_permission" (
"id" SERIAL NOT NULL,
"id" TEXT NOT NULL,
"created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updated_at" TIMESTAMP(3) NOT NULL,
"type" INTEGER NOT NULL,
CONSTRAINT "sys_permission_pkey" PRIMARY KEY ("id")
@@ -53,37 +60,49 @@ CREATE TABLE "sys_permission" (
-- CreateTable
CREATE TABLE "sys_menu" (
"id" SERIAL NOT NULL,
"id" TEXT NOT NULL,
"created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updated_at" TIMESTAMP(3) NOT NULL,
"name" TEXT NOT NULL,
"url" TEXT NOT NULL,
"parent_id" INTEGER NOT NULL DEFAULT 0,
"parent_id" TEXT,
"sort" INTEGER NOT NULL DEFAULT 0,
CONSTRAINT "sys_menu_pkey" PRIMARY KEY ("id")
);
-- CreateTable
CREATE TABLE "sys_menu_permission" (
"id" SERIAL NOT NULL,
"menu_id" INTEGER NOT NULL,
"permission_id" INTEGER NOT NULL,
"id" TEXT NOT NULL,
"created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updated_at" TIMESTAMP(3) NOT NULL,
"menu_id" TEXT NOT NULL,
"permission_id" TEXT NOT NULL,
CONSTRAINT "sys_menu_permission_pkey" PRIMARY KEY ("id")
);
-- CreateTable
CREATE TABLE "sys_role_permission" (
"id" SERIAL NOT NULL,
"role_id" INTEGER NOT NULL,
"permission_id" INTEGER NOT NULL,
"id" TEXT NOT NULL,
"created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updated_at" TIMESTAMP(3) NOT NULL,
"role_id" TEXT NOT NULL,
"permission_id" TEXT NOT NULL,
CONSTRAINT "sys_role_permission_pkey" PRIMARY KEY ("id")
);
-- CreateTable
CREATE TABLE "sys_config" (
"id" TEXT NOT NULL,
"created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updated_at" TIMESTAMP(3) NOT NULL,
"key" TEXT NOT NULL,
"value" TEXT NOT NULL,
"remark" TEXT
"remark" TEXT,
CONSTRAINT "sys_config_pkey" PRIMARY KEY ("id")
);
-- CreateIndex

View File

@@ -13,17 +13,17 @@ datasource db {
// 系统用户表
model SysUser {
id Int @id @default(autoincrement()) /// 主键id
id String @id @default(cuid()) /// 主键id
createdAt DateTime @default(now()) @map("created_at") /// 创建时间
updatedAt DateTime @updatedAt @map("updated_at") /// 修改时间
name String? /// 用户姓名
account String @unique /// 账户
password String /// 密码
passwordSalt String @map("password_salt") /// 密码盐
status Int @default(0) @db.SmallInt /// 用户状态
avatarId Int? @map("avatar_id") /// 用户头像id
avatar Files? @relation(fields: [avatarId], references: [id])
avatarId String? @map("avatar_id") /// 用户头像id
avatar File? @relation(fields: [avatarId], references: [id])
gender Int @default(0) @db.SmallInt /// 用户性别 0女 1男 2未知
createdAt DateTime @default(now()) @map("created_at") /// 创建时间
updatedAt DateTime @updatedAt @map("updated_at") /// 修改时间
userRole SysUserRole[]
@@index([avatarId])
@@ -32,7 +32,9 @@ model SysUser {
// 系统角色表
model SysRole {
id Int @id @default(autoincrement())
id String @id @default(cuid()) /// 主键id
createdAt DateTime @default(now()) @map("created_at") /// 创建时间
updatedAt DateTime @updatedAt @map("updated_at") /// 修改时间
code String @unique
name String
status Int @default(0) @db.SmallInt /// 角色状态
@@ -44,11 +46,13 @@ model SysRole {
// 用户角色关联表
model SysUserRole {
id Int @id @default(autoincrement())
userId Int @map("user_id")
roleId Int @map("role_id")
user SysUser @relation(fields: [userId], references: [id], onDelete: Cascade)
role SysRole @relation(fields: [roleId], references: [id], onDelete: Cascade)
id String @id @default(cuid()) /// 主键id
createdAt DateTime @default(now()) @map("created_at") /// 创建时间
updatedAt DateTime @updatedAt @map("updated_at") /// 修改时间
userId String @map("user_id")
roleId String @map("role_id")
user SysUser @relation(fields: [userId], references: [id], onDelete: Cascade)
role SysRole @relation(fields: [roleId], references: [id], onDelete: Cascade)
@@index([userId])
@@index([roleId])
@@ -56,19 +60,22 @@ model SysUserRole {
}
// 文件表
model Files {
id Int @id @default(autoincrement())
model File {
id String @id @default(cuid()) /// 主键id
createdAt DateTime @default(now()) @map("created_at") /// 创建时间
updatedAt DateTime @updatedAt @map("updated_at") /// 修改时间
name String /// 文件名称
path String
createdAt DateTime @default(now()) @map("created_at") /// 创建时间
sysUser SysUser[]
@@map("files")
@@map("file")
}
// 权限表
model SysPermission {
id Int @id @default(autoincrement())
id String @id @default(cuid()) /// 主键id
createdAt DateTime @default(now()) @map("created_at") /// 创建时间
updatedAt DateTime @updatedAt @map("updated_at") /// 修改时间
type Int
sysRolePermission SysRolePermission[]
sysMenuPermission SysMenuPermission[]
@@ -78,10 +85,12 @@ model SysPermission {
// 系统菜单表
model SysMenu {
id Int @id @default(autoincrement())
id String @id @default(cuid()) /// 主键id
createdAt DateTime @default(now()) @map("created_at") /// 创建时间
updatedAt DateTime @updatedAt @map("updated_at") /// 修改时间
name String
url String
parentId Int @default(0) @map("parent_id")
parentId String? @map("parent_id")
sort Int @default(0)
sysMenuPermission SysMenuPermission[]
@@ -90,9 +99,11 @@ model SysMenu {
// 菜单权限关联表
model SysMenuPermission {
id Int @id @default(autoincrement())
menuId Int @map("menu_id")
permissionId Int @map("permission_id")
id String @id @default(cuid()) /// 主键id
createdAt DateTime @default(now()) @map("created_at") /// 创建时间
updatedAt DateTime @updatedAt @map("updated_at") /// 修改时间
menuId String @map("menu_id")
permissionId String @map("permission_id")
menu SysMenu @relation(fields: [menuId], references: [id], onDelete: Cascade)
permission SysPermission @relation(fields: [permissionId], references: [id], onDelete: Cascade)
@@ -103,9 +114,11 @@ model SysMenuPermission {
// 角色权限关联表
model SysRolePermission {
id Int @id @default(autoincrement())
roleId Int @map("role_id")
permissionId Int @map("permission_id")
id String @id @default(cuid()) /// 主键id
createdAt DateTime @default(now()) @map("created_at") /// 创建时间
updatedAt DateTime @updatedAt @map("updated_at") /// 修改时间
roleId String @map("role_id")
permissionId String @map("permission_id")
role SysRole @relation(fields: [roleId], references: [id], onDelete: Cascade)
permission SysPermission @relation(fields: [permissionId], references: [id], onDelete: Cascade)
@@ -116,9 +129,12 @@ model SysRolePermission {
// 系统配置
model SysConfig {
key String @unique
value String
remark String?
id String @id @default(cuid()) /// 主键id
createdAt DateTime @default(now()) @map("created_at") /// 创建时间
updatedAt DateTime @updatedAt @map("updated_at") /// 修改时间
key String @unique
value String
remark String?
@@map("sys_config")
}

View File

@@ -18,9 +18,9 @@ async function main() {
},
});
const menus = [
{ name: '用户管理', url: 'user', parentId: 0 },
{ name: '角色管理', url: 'role', parentId: 0 },
{ name: '菜单管理', url: 'menu', parentId: 0 },
{ name: '用户管理', url: 'user' },
{ name: '角色管理', url: 'role' },
{ name: '菜单管理', url: 'menu' },
];
for (const menu of menus) {

View File

@@ -19,12 +19,12 @@ export class AuthController {
// 授权用户角色
@Post('userRole/:id')
grantUserRole(@Param('id') id: string, @Body() dto: UserRoleDto) {
return this.authService.grantUserRole(+id, dto);
return this.authService.grantUserRole(id, dto);
}
// 授权角色权限
@Post('rolePermission/:id')
grantRolePermission(@Param('id') id: string, @Body() dto: RolePermissionDto) {
return this.authService.grantRolePermission(+id, dto);
return this.authService.grantRolePermission(id, dto);
}
}

View File

@@ -62,7 +62,7 @@ export class AuthService {
return { accessToken };
}
async grantUserRole(id: number, { roleIds }: UserRoleDto) {
async grantUserRole(id: string, { roleIds }: UserRoleDto) {
const user = await this.prisma.sysUser.findUnique({
where: { id },
});
@@ -96,7 +96,7 @@ export class AuthService {
});
}
async grantRolePermission(id: number, { permissionIds }: RolePermissionDto) {
async grantRolePermission(id: string, { permissionIds }: RolePermissionDto) {
const role = await this.prisma.sysRole.findUnique({
where: { id },
});

View File

@@ -1,8 +1,8 @@
import { ArrayUnique, IsInt, IsNotEmpty } from 'class-validator';
import { ArrayUnique, IsNotEmpty, IsString } from 'class-validator';
export class RolePermissionDto {
@IsNotEmpty()
@IsInt({ each: true })
@IsString({ each: true })
@ArrayUnique()
permissionIds: number[];
permissionIds: string[];
}

View File

@@ -1,8 +1,8 @@
import { ArrayUnique, IsInt, IsNotEmpty } from 'class-validator';
import { ArrayUnique, IsNotEmpty, IsString } from 'class-validator';
export class UserRoleDto {
@IsNotEmpty()
@IsInt({ each: true })
@IsString({ each: true })
@ArrayUnique()
roleIds: number[];
roleIds: string[];
}

View File

@@ -19,7 +19,7 @@ export class FilesService {
const filePath = await this.minioService.uploadFile(file, fileName);
const res = await this.prisma.files.create({
const res = await this.prisma.file.create({
data: { name: fileName, path: filePath },
});

View File

@@ -10,8 +10,8 @@ export class CreateMenuDto {
url: string;
@IsOptional()
@IsInt()
parentId?: number;
@IsString()
parentId?: string;
@IsOptional()
@IsInt()

View File

@@ -32,11 +32,11 @@ export class MenuController {
@Patch(':id')
update(@Param('id') id: string, @Body() updateMenuDto: UpdateMenuDto) {
return this.menuService.update(+id, updateMenuDto);
return this.menuService.update(id, updateMenuDto);
}
@Delete(':id')
remove(@Param('id') id: number) {
remove(@Param('id') id: string) {
return this.menuService.remove(id);
}
}

View File

@@ -9,7 +9,7 @@ import { PermissionType } from '@/common/enum';
export class MenuService {
constructor(private readonly prisma: PrismaService) {}
findOne(params: { id?: number; excludeId?: number }) {
findOne(params: { id?: string; excludeId?: string }) {
const { id, excludeId } = params;
return this.prisma.sysMenu.findFirst({
where: {
@@ -19,7 +19,7 @@ export class MenuService {
});
}
findOneById(id: number) {
findOneById(id: string) {
return this.prisma.sysMenu.findUnique({ where: { id } });
}
@@ -35,15 +35,20 @@ export class MenuService {
});
}
generatorMenu(data, parentId = 0) {
generatorMenu(data, parentId?: string) {
const menu = [];
for (const item of data) {
if (item.parentId === parentId) {
const children = this.generatorMenu(data, item.id);
if (children.length) {
item.children = children;
}
// 不存在父id 代表是顶级菜单
if (!parentId) {
menu.push(item);
} else {
if (item.parentId === parentId) {
const children = this.generatorMenu(data, item.id);
if (children.length) {
item.children = children;
}
menu.push(item);
}
}
}
return menu;
@@ -69,7 +74,7 @@ export class MenuService {
return this.prisma.sysMenu.findMany(query);
}
async update(id: number, data: UpdateMenuDto) {
async update(id: string, data: UpdateMenuDto) {
const doesExists = await this.findOne({ id });
if (!doesExists) {
throw new HttpException('菜单不存在!', HttpStatus.NOT_FOUND);
@@ -85,7 +90,7 @@ export class MenuService {
await this.prisma.sysMenu.update({ data, where: { id } });
}
async remove(menuId: number) {
async remove(menuId: string) {
const doesExists = await this.findOneById(menuId);
if (!doesExists) {
throw new HttpException('菜单不存在!', HttpStatus.NOT_FOUND);

View File

@@ -1,6 +1,6 @@
import { IsNumber } from 'class-validator';
import { IsString } from 'class-validator';
export class GrantMenuDto {
@IsNumber({}, { each: true })
menuIds: number[];
@IsString({ each: true })
menuIds: string[];
}

View File

@@ -8,12 +8,12 @@ export class PermissionController {
constructor(private readonly permissionService: PermissionService) {}
@Get(':id/menu')
getMenu(@Param('id') id: number) {
getMenu(@Param('id') id: string) {
return this.permissionService.getMenu(id);
}
@Patch(':id')
grantMenu(@Param('id') id: number, @Body() dto: GrantMenuDto) {
grantMenu(@Param('id') id: string, @Body() dto: GrantMenuDto) {
return this.permissionService.grantMenu(id, dto);
}
}

View File

@@ -6,7 +6,7 @@ import { PrismaService } from '@/prisma/prisma.service';
export class PermissionService {
constructor(private readonly prisma: PrismaService) {}
async getMenu(id: number) {
async getMenu(id: string) {
const menuIds = await this.prisma.sysMenuPermission.findMany({
select: {
menuId: true,
@@ -23,7 +23,7 @@ export class PermissionService {
return menuIds.map((item) => item.menuId);
}
async grantMenu(id: number, { menuIds }: GrantMenuDto) {
async grantMenu(id: string, { menuIds }: GrantMenuDto) {
await this.prisma.$transaction(async (prisma) => {
// 删除角色对应的权限
await prisma.sysRolePermission.deleteMany({

View File

@@ -34,11 +34,11 @@ export class RoleController {
@Patch(':id')
update(@Param('id') id: string, @Body() dto: UpdateRoleDto) {
return this.roleService.update(+id, dto);
return this.roleService.update(id, dto);
}
@Delete(':id')
remove(@Param('id') id: string) {
return this.roleService.remove(+id);
return this.roleService.remove(id);
}
}

View File

@@ -60,7 +60,7 @@ export class RoleService {
});
}
async update(id: number, data: UpdateRoleDto) {
async update(id: string, data: UpdateRoleDto) {
const role = await this.findOne({ id });
if (!role)
throw new HttpException('角色信息不存在!', HttpStatus.NOT_FOUND);
@@ -71,7 +71,7 @@ export class RoleService {
});
}
async remove(id: number) {
async remove(id: string) {
const role = await this.findOne({ id });
if (!role)
throw new HttpException('角色信息不存在!', HttpStatus.NOT_FOUND);

View File

@@ -2,6 +2,7 @@ import {
IsNotEmpty,
IsNumber,
IsOptional,
IsString,
MaxLength,
MinLength,
} from 'class-validator';
@@ -17,8 +18,8 @@ export class CreateUserDto {
account: string;
@IsOptional()
@IsNumber()
avatarId: number;
@IsString()
avatarId: string;
@IsNotEmpty()
@IsNumber()

View File

@@ -37,22 +37,22 @@ export class UserController {
}
@Get(':id')
findOneById(@Param('id') id: number) {
findOneById(@Param('id') id: string) {
return this.userService.findOneById(id);
}
@Patch(':id/password')
updatePassword(@Param('id') id: string, @Body() dto: UpdatePasswordDto) {
return this.userService.updatePassword(+id, dto);
return this.userService.updatePassword(id, dto);
}
@Patch(':id')
update(@Param('id') id: string, @Body() dto: UpdateUserDto) {
return this.userService.update(+id, dto);
return this.userService.update(id, dto);
}
@Delete(':id')
remove(@Param('id') id: string) {
return this.userService.remove(+id);
return this.userService.remove(id);
}
}

View File

@@ -91,7 +91,7 @@ export class UserService {
};
}
async findOneById(id: number) {
async findOneById(id: string) {
return this.prisma.sysUser.findUnique({
select: {
id: true,
@@ -112,7 +112,7 @@ export class UserService {
});
}
async getUserInfo(id: number, isAdmin: boolean) {
async getUserInfo(id: string, isAdmin: boolean) {
const user = await this.prisma.sysUser.findUnique({
select: {
name: true,
@@ -154,7 +154,7 @@ export class UserService {
};
}
async update(id: number, data: UpdateUserDto) {
async update(id: string, data: UpdateUserDto) {
const user = await this.findOne({ id });
if (!user)
throw new HttpException('修改失败,用户不存在!', HttpStatus.NOT_FOUND);
@@ -178,7 +178,7 @@ export class UserService {
}
async updatePassword(
id: number,
id: string,
{ password, newPassword }: UpdatePasswordDto,
) {
const user = await this.findOne({ id });
@@ -203,7 +203,7 @@ export class UserService {
});
}
async remove(id: number) {
async remove(id: string) {
const user = await this.findOne({ id });
if (!user)