Home >Web Front-end >JS Tutorial >Designing RBAC Permission System with Nest.js: A Step-by-Step Guide
For backend management systems, features like access control and personalized user interfaces are essential. For instance, a super administrator can view all pages, regular users can access pages A and B, and VIP users can view pages A, B, C, and D. The logic behind these functionalities is based on the design of three key concepts:
The relationship can be illustrated with the following diagram:
Next, we’ll use Nest to implement the foundation of such a system from scratch — the permission design.
First, we need to create the database. We’ll use the MySQL database and execute the following command to create it:
CREATE DATABASE `nest-database` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
We’ll start a new Nest project by running the following command:
nest new nest-project
Then, install the necessary database dependencies, primarily typeorm and mysql2:
npm install --save @nestjs/typeorm typeorm mysql2
Next, configure typeorm in app.module.ts:
import { Module } from '@nestjs/common'; import { AppController } from './app.controller'; import { AppService } from './app.service'; import { TypeOrmModule } from '@nestjs/typeorm'; @Module({ imports: [ TypeOrmModule.forRoot({ type: 'mysql', host: 'localhost', port: 3306, username: 'root', password: 'password', database: 'nest-database', synchronize: true, logging: true, entities: [__dirname + '/**/*.entity{.ts,.js}'], poolSize: 10, connectorPackage: 'mysql2', }), ], controllers: [AppController], providers: [AppService], }) export class AppModule {}
Typically, an RBAC (Role-Based Access Control) system will have 5 tables as follows:
The domain model can be visualized as follows:
Next, we’ll create three non-relation tables in Nest and define their relationships.
CREATE DATABASE `nest-database` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
In the User table, the roles field is defined to connect with the user_role_relation table. The relationship logic is: user.id === userRoleRelation.userId and role.id === userRoleRelation.roleId. Matching Role records are automatically linked to User.
nest new nest-project
The permissions field in the Role table works similarly. It connects with the role_permission_relation table using the logic: role.id === rolePermissionRelation.roleId and permission.id === rolePermissionRelation.permissionId.
npm install --save @nestjs/typeorm typeorm mysql2
The Permission table doesn’t have relationships; it simply records available permissions.
Here’s a service to initialize some test data:
import { Module } from '@nestjs/common'; import { AppController } from './app.controller'; import { AppService } from './app.service'; import { TypeOrmModule } from '@nestjs/typeorm'; @Module({ imports: [ TypeOrmModule.forRoot({ type: 'mysql', host: 'localhost', port: 3306, username: 'root', password: 'password', database: 'nest-database', synchronize: true, logging: true, entities: [__dirname + '/**/*.entity{.ts,.js}'], poolSize: 10, connectorPackage: 'mysql2', }), ], controllers: [AppController], providers: [AppService], }) export class AppModule {}
Run the initData service via a browser or Postman, and the data will populate the database.
With the basic permission structure set up, you can now implement features like registration, login, and JWT-based authentication.
Now it's your turn!
Leapcell is the Next-Gen Serverless Platform for Web Hosting, Async Tasks, and Redis:
Multi-Language Support
Deploy unlimited projects for free
Unbeatable Cost Efficiency
Streamlined Developer Experience
Effortless Scalability and High Performance
Explore more in the Documentation!
Follow us on X: @LeapcellHQ
Read on our blog
The above is the detailed content of Designing RBAC Permission System with Nest.js: A Step-by-Step Guide. For more information, please follow other related articles on the PHP Chinese website!