本文以RBAC權限管理中的用戶和角色舉例,兩個實體存在多對多的關係,一個用戶擁有多個角色,一個角色屬於多個用戶。typeorm的中文文檔沒有對自定義中間表的說明,發現英文有相關說明,但示例代碼貌似有問題。javascript
1、中間表有自定義字段的處理java
在用戶user和role實體以外,新建一個roleUse的實體(記錄用戶和角色的關聯關係),如此原本是多對多的關係,變成了user和role與roleUser二組一對多的關係,這樣雖然會多定義一個實體,但好處是能夠中間表能夠擴展定義其它額外的數據web
用戶實體 user.entity.tsspa
import { Entity, Column, Index, ManyToMany, JoinColumn, OneToMany, JoinTable, PrimaryGeneratedColumn, CreateDateColumn, UpdateDateColumn } from 'typeorm'; import { Role } from '../../admin/role/role.entity'; import { UserRole } from '../../admin/role/roleUser.entity'; @Entity() export class User { @PrimaryGeneratedColumn() id: number; @Column({ length: 100 }) @Index({ unique: true }) name: string; @OneToMany(type => UserRole, userRole => userRole.user) userRoles: UserRole[]; }
角色實體role.entity.tscode
import { Entity, Column, ManyToMany, OneToMany, JoinTable, JoinColumn } from 'typeorm'; import { BaseEntity } from '../../../common/base/base.entity'; import { UserRole } from './roleUser.entity'; @Entity('role') export class Role extends BaseEntity { @Column({ length: 100 }) role_name: string; @OneToMany(type => UserRole, userRole => userRole.role) userRoles: UserRole[]; }
中間表實體 roleUser.entity.ts (主健)orm
import { Entity, PrimaryGeneratedColumn, JoinColumn, Column, CreateDateColumn, UpdateDateColumn, ManyToOne } from 'typeorm'; import { BaseEntity } from '../../../common/base/base.entity'; import { User } from '../../web/user/user.entity'; import { Role } from './role.entity'; @Entity() export class UserRole { @PrimaryGeneratedColumn() public id: number; @CreateDateColumn() created_time: Date; @UpdateDateColumn() updated_time: Date; @ManyToOne(type => User, user => user.userRoles) @JoinColumn({name: 'user_id'}) user: User; @ManyToOne(type => Role, role => role.userRoles) @JoinColumn({name: 'role_id'}) role: Role; }
中間表定義roleUser.entity.ts(複合主鍵)blog
import { Entity, PrimaryGeneratedColumn, JoinColumn, Column, CreateDateColumn, UpdateDateColumn, ManyToOne } from 'typeorm'; import { BaseEntity } from '../../../common/base/base.entity'; import { User } from '../../web/user/user.entity'; import { Role } from './role.entity'; @Entity() export class UserRole { @CreateDateColumn() created_time: Date; @UpdateDateColumn() updated_time: Date; @ManyToOne(type => User, user => user.userRoles, { primary: true }) @JoinColumn({name: 'user_id'}) user: User; @ManyToOne(type => Role, role => role.userRoles, { primary: true }) @JoinColumn({name: 'role_id'}) role: Role; }
2、無中間表的多對多實體定義,又分爲單向和雙向,單向是僅在一側與關係裝飾器的關係, 雙向是與關係兩側的裝飾者的關係。具體官網文檔:https://typeorm.io/#/relations,此處主要講雙向ip
角色實體 rem
import { Entity, Column, ManyToMany, OneToMany, JoinTable, JoinColumn } from 'typeorm'; import { BaseEntity } from '../../../common/base/base.entity'; import { User } from '../../web/user/user.entity'; @Entity('role') export class Role extends BaseEntity { @Column({ length: 100 }) role_name: string; @Column({ length: 100 }) remark: string; // 無中間實體表的配置 @ManyToMany(type => User, user => user.roles, { cascade: true }) users: User[]; }
用戶實體文檔
import { Entity, Column, Index, ManyToMany, JoinColumn, OneToMany, JoinTable, PrimaryGeneratedColumn, CreateDateColumn, UpdateDateColumn } from 'typeorm'; import { Role } from '../../admin/role/role.entity'; @Entity() export class User { @PrimaryGeneratedColumn() id: number; @Column({ length: 100 }) @Index({ unique: true }) name: string; // 無中間實體表的配置 @ManyToMany(type => Role, role => role.users) @JoinTable({ name: 'user_role', joinColumns: [ {name: 'user_id'} ], inverseJoinColumns: [ {name: 'role_id'} ] }) // 下面的定義也能實現 // @JoinTable({ // name: 'user_role', // joinColumn: { name: 'user_id' }, // inverseJoinColumn: { name: 'role_id' }, // }) roles: Role[]; }