I tried to create a database schema with users, groups, documents and permissions.
- users can join multiple groups
- groups can have multiple users
- users can have permissions for documents
- groups can have permissions for documents
- permissions can be anything, not for documents only
I tried to create a small graphic for that
I started 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[];
}
When it comes to the persmissions for documents you will see that the relation exists between 3 tables, users/groups, documents and permissions.
I use TypeORM for REST APIs (NestJs) and I'm not sure if a permission is an Entity. Due to the fact I'm designing REST endpoints this permission entity would be a shared entity along multiple endpoints.
How do I have to extend my entities for these permissions?
... and maybe you might come up with a better database design :)
