I attempted to construct a database schema involving users, groups, documents, and permissions.
- Users can be part of multiple groups
- Groups can have multiple users
- Users can possess permissions for documents
- Groups can have permissions for documents
- Permissions can encompass any type of data, not solely documents
I endeavored to create a simplistic visual representation of this concept
https://i.sstatic.net/5BjXh.png
I initiated the process by designing the entities
User
@Entity('User')
export class UserEntity {
@PrimaryGeneratedColumn('uuid')
id: string;
@ManyToMany((type: any) => GroupEntity, (group: GroupEntity) => group.users)
@JoinTable()
groups: GroupEntity[];
@ManyToMany((type: any) => DocumentEntity, (document: DocumentEntity) => document.users)
@JoinTable()
documents: DocumentEntity[];
}
Group
@Entity('Group')
export class GroupEntity {
@PrimaryGeneratedColumn('uuid')
id: string;
@ManyToMany((type: any) => UserEntity, (user: UserEntity) => user.groups)
users: UserEntity[];
@ManyToMany((type: any) => DocumentEntity, (document: DocumentEntity) => document.groups)
@JoinTable()
documents: DocumentEntity[];
}
Document
@Entity('Document')
export class DocumentEntity {
@PrimaryGeneratedColumn('uuid')
id: string;
@ManyToMany((type: any) => UserEntity, (user: UserEntity) => user.documents)
users: UserEntity[];
@ManyToMany((type: any) => GroupEntity, (group: GroupEntity) => group.documents)
groups: GroupEntity[];
}
Regarding permissions for documents, it's apparent that there is a relational connection among three tables: users/groups, documents, and permissions.
I utilize TypeORM for developing REST APIs with NestJs, but I am uncertain if a permission should be considered an Entity. Since I am structuring REST endpoints, this permission entity would likely serve as a shared entity across various endpoints.
How should I modify my entities to accommodate these permissions?
... perhaps you could propose an improved database design :)