Skip to content

Commit

Permalink
refactor: entity relation
Browse files Browse the repository at this point in the history
  • Loading branch information
kuizuo committed Dec 1, 2023
1 parent 8df3127 commit faee140
Show file tree
Hide file tree
Showing 11 changed files with 36 additions and 81 deletions.
42 changes: 10 additions & 32 deletions apps/api/src/modules/auth/entities/access-token.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,60 +3,38 @@ import {
Column,
CreateDateColumn,
Entity,
JoinColumn,
ManyToOne,
OneToOne,
PrimaryGeneratedColumn,
Relation,
} from 'typeorm'

import { UserEntity } from '~/modules/user/entities/user.entity'

import { RefreshTokenEntity } from './refresh-token.entity'

/**
* 用户认证token模型
*/
@Entity('user_access_tokens')
export class AccessTokenEntity extends BaseEntity {
@PrimaryGeneratedColumn('uuid')
id!: string

/**
* @description 令牌字符串
* @type {string}
*/
@Column({ length: 500 })
value!: string

@Column({
comment: '令牌过期时间',
})
@Column({ comment: '令牌过期时间' })
expired_at!: Date

@CreateDateColumn({
comment: '令牌创建时间',
})
@CreateDateColumn({ comment: '令牌创建时间' })
created_at!: Date

/**
* @description 关联的刷新令牌
* @type {RefreshTokenEntity}
*/
@OneToOne(
() => RefreshTokenEntity,
refreshToken => refreshToken.accessToken,
{
cascade: true,
},
)
refreshToken!: Relation<RefreshTokenEntity>

/**
* @description 所属用户
* @type {UserEntity}
*/
@OneToOne(() => RefreshTokenEntity, refreshToken => refreshToken.accessToken, {
cascade: true,
})
refreshToken!: RefreshTokenEntity

@ManyToOne(() => UserEntity, user => user.accessTokens, {
onDelete: 'CASCADE',
})
user!: Relation<UserEntity>
@JoinColumn({ name: 'user_id' })
user!: UserEntity
}
34 changes: 7 additions & 27 deletions apps/api/src/modules/auth/entities/refresh-token.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,47 +6,27 @@ import {
JoinColumn,
OneToOne,
PrimaryGeneratedColumn,
Relation,
} from 'typeorm'

import { AccessTokenEntity } from './access-token.entity'

/**
* 刷新Token的Token模型
*/
@Entity('user_refresh_tokens')
export class RefreshTokenEntity extends BaseEntity {
@PrimaryGeneratedColumn('uuid')
id!: string

/**
* @description 令牌字符串
* @type {string}
*/
@Column({ length: 500 })
value!: string

@Column({
comment: '令牌过期时间',
})
@Column({ comment: '令牌过期时间' })
expired_at!: Date

@CreateDateColumn({
comment: '令牌创建时间',
})
createdAt!: Date
@CreateDateColumn({ comment: '令牌创建时间' })
created_at!: Date

/**
* @description 关联的登录令牌
* @type {AccessTokenEntity}
*/
@OneToOne(
() => AccessTokenEntity,
accessToken => accessToken.refreshToken,
{
onDelete: 'CASCADE',
},
)
@OneToOne(() => AccessTokenEntity, accessToken => accessToken.refreshToken, {
onDelete: 'CASCADE',
})
@JoinColumn()
accessToken!: Relation<AccessTokenEntity>
accessToken!: AccessTokenEntity
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,4 @@ export class CaptchaLogEntity extends AbstractEntity {
@Column({ nullable: true })
@ApiProperty({ description: '验证码提供方' })
provider: 'sms' | 'email'

@Column({ type: 'datetime', default: () => 'CURRENT_TIMESTAMP' })
@ApiProperty({ description: '发送时间' })
time: Date
}
6 changes: 1 addition & 5 deletions apps/api/src/modules/system/log/entities/login-log.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,11 @@ export class LoginLogEntity extends AbstractEntity {
@ApiProperty({ description: '登录方式' })
provider: string

@Column({ type: 'datetime', nullable: true })
@ApiProperty({ description: '登录时间' })
time: Date

@Column({ length: 500, nullable: true })
@ApiProperty({ description: '浏览器ua' })
ua: string

@ManyToOne(() => UserEntity)
@JoinColumn()
@JoinColumn({ name: 'user_id' })
user: Relation<UserEntity>
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ export class TaskLogEntity extends AbstractEntity {
consumeTime: number

@ManyToOne(() => TaskEntity)
@JoinColumn()
@JoinColumn({ name: 'task_id' })
task: Relation<TaskEntity>
}
3 changes: 0 additions & 3 deletions apps/api/src/modules/system/log/models/log.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@ export class LoginLogInfo {
@ApiProperty({ description: '浏览器', example: 'Chrome' })
browser: string

@ApiProperty({ description: '时间', example: '2022-01-01 00:00:00' })
time: string

@ApiProperty({ description: '登录用户名', example: 'admin' })
username: string
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ async function parseLoginLog(e: any, parser: UAParser): Promise<LoginLogInfo> {
address: e.login_log_address,
os: `${`${uaResult.os.name ?? ''} `}${uaResult.os.version}`,
browser: `${`${uaResult.browser.name ?? ''} `}${uaResult.browser.version}`,
time: e.login_log_created_at,
username: e.user_username,
}
}
Expand Down
3 changes: 0 additions & 3 deletions apps/api/src/modules/system/online/online.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@ export class OnlineUserInfo {
@ApiProperty({ description: '是否当前' })
isCurrent: boolean

@ApiProperty({ description: '登陆时间' })
time: string

@ApiProperty({ description: '系统' })
os: string

Expand Down
10 changes: 9 additions & 1 deletion apps/api/src/modules/system/role/role.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,18 @@ export class RoleEntity extends AbstractEntity {
@ApiProperty({ description: '状态:1启用,0禁用' })
status: number

@Column({ nullable: true })
@ApiProperty({ description: '是否默认用户' })
default: boolean

@ManyToMany(() => UserEntity, user => user.roles)
users: Relation<UserEntity[]>

@ManyToMany(() => MenuEntity, menu => menu.roles, {})
@JoinTable()
@JoinTable({
name: 'sys_role_menus',
joinColumn: { name: 'role_id', referencedColumnName: 'id' },
inverseJoinColumn: { name: 'menu_id', referencedColumnName: 'id' },
})
menus: Relation<MenuEntity[]>
}
2 changes: 1 addition & 1 deletion apps/api/src/modules/todo/todo.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ export class TodoEntity extends AbstractEntity {
status: boolean

@ManyToOne(() => UserEntity)
@JoinColumn()
@JoinColumn({ name: 'user_id' })
user: Relation<UserEntity>
}
10 changes: 7 additions & 3 deletions apps/api/src/modules/user/entities/user.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export class UserEntity extends AbstractEntity {
@Column({ length: 32 })
psalt: string

@Column({ name: 'nick_name', nullable: true })
@Column({ nullable: true })
nickname: string

@Column({ name: 'avatar', nullable: true })
Expand All @@ -51,11 +51,15 @@ export class UserEntity extends AbstractEntity {
status: number

@ManyToMany(() => RoleEntity, role => role.users)
@JoinTable()
@JoinTable({
name: 'sys_user_roles',
joinColumn: { name: 'user_id', referencedColumnName: 'id' },
inverseJoinColumn: { name: 'role_id', referencedColumnName: 'id' },
})
roles: Relation<RoleEntity[]>

@ManyToOne(() => DeptEntity, dept => dept.users)
@JoinColumn()
@JoinColumn({ name: 'dept_id' })
dept: Relation<DeptEntity>

@OneToMany(() => AccessTokenEntity, accessToken => accessToken.user, {
Expand Down

0 comments on commit faee140

Please sign in to comment.