@appxdigital/appx-core-cli
v1.0.11
Published
<p align="center"> <a href="https://appx-digital.com/" target="_blank"> <img src="./assets/logo_appx.svg" width="300" alt="Appx Logo" /> </a> </p>
Readme
Description
Installation
- Open your terminal and run the following command:
npm install @appxdigital/appx-core-cli -g appx-core create - Follow the prompts provided by the setup wizard. This will scaffold a new NestJS project with
appx-corepre-integrated. - Open created project in your favorite IDE.
Configure Database Schema
- Open the
prisma/schema.prismafile. - Modify this file to define your application's specific data models. You can add new models, fields, and relations as needed.
Note: If you already have a database created and want to import the structure, use the following command:
npx prisma db pullThis command will override the schema.prisma file, please ensure that the original configurations and models are kept (you can adapt User and Session model, but it's advised to keep all default attributes)
Generate Modules and Prisma Client
- After editing your schema, generate the modules for each model by running:
appx-core generate - This command also updates the
@prisma/clientlibrary within yournode_modulesto match your defined schema, providing type-safe database access. This command should be run everytime you make a change to your prisma schema.
Apply Database Migrations
- To create the necessary database tables and apply your schema changes, run:
npx prisma migrate dev - This will:
- Create a new SQL migration file reflecting the changes between your current schema and the last migration.
- Apply the migration to your database.
Run the Application
- Start your application in development mode using:
npm run start:dev - The application will start on
http://localhost:3000or the port specified during your setup, you can change this in the.envfile.
Register Your First User
- To create a user account, send an HTTP
POSTrequest to the/auth/registerendpoint. - The request body must be JSON and include the
emailandpassword:{ "email": "[email protected]", "password": "password" } - On success, you'll receive a JSON response confirming registration and containing the new user's details.
Log In
- To log in with the registered user, send an HTTP
POSTrequest to the/auth/loginendpoint. - The request body must be JSON and include the same fields used for registration:
{ "email": "[email protected]", "password": "password" } - On success, you'll receive a JSON response confirming login and containing the user's details.
Permissions
This project uses RBAC to control user access. Permissions are defined in a configuration file and checked automatically before controller actions and during database queries.
Configuration
Permissions are defined in the
src/config/permissions.config.tsfile.The configuration structure is hierarchical:
ModelName->RoleName->ActionName->Rule.export const PermissionsConfig: PermissionsConfigType = { ModelName: { // e.g., 'User'; links to controller's entityName RoleName: { // e.g., 'ADMIN', 'CLIENT'; matches user roles ActionName: Rule, // e.g., 'findUnique', 'update' // ... other actions for this role/model }, // ... other roles for this model }, // ... other models };
Permission Rules & Effects
The Rule assigned to an action determines if a user with that role can perform the action:
'ALL': Grants unrestricted access for the specific action.- Object
{ conditions: { ... } }: Grants access only if the specified conditions match the data being accessed.- These
conditions(e.g.,{ id: '$USER_ID' }) are automatically added asWHEREclauses to the underlying database query by thePrismaService. - Placeholders like
$USER_IDare replaced with the current user's actual data (e.g.,req.user.id).
- These
- Missing Rule: If no rule (
'ALL'or an object) is defined for an action under the user's role, access is denied.
Applying Permissions
- Protect controller methods by decorating them with
@Permission('actionName'). This links the method to anActionNamein yourpermissions.config.ts.Methods without the
@Permissiondecorator are not subject to specific action checks by theRbacGuardand are allowed access.@Controller('users') export class UserController extends CoreController<User> { @Get(':id') @Permission('findUnique') // Checks config for User -> Role -> findUnique async findOne(@Param('id') id: string) { /* ... */ } @Get('/public/info') // No @Permission decorator - RbacGuard allows by default access for this route async getPublicInfo() { /* ... */ } static get entityName(): string { return 'User'; } }
Adding Permissions for a New Action
Decorate Method & Choose Name: Add the
@Permission('yourActionName')decorator to the relevant controller method, choosing a unique string for'yourActionName'(e.g.,'publishItem','resetPassword').Update Config: Edit
permissions.config.ts. Add theactionNameunder the appropriateModelName->RoleName(s). Set the rule:- Use
'ALL'for full access. - Use an object like
{ conditions: { field: '$USER_ID' } }for conditional access (enforced by the database query).
// Example: Adding 'publishItem' rule to Article model config Article: { ADMIN: { /*...,*/ publishItem: 'ALL' }, EDITOR: { /*...,*/ publishItem: { conditions: { authorId: '$USER_ID' } } }, },- Use
