@uecsio/users-api
v2.0.0
Published
A reusable NestJS API Users module for user management
Maintainers
Readme
@uecsio/users-api
A reusable NestJS API Users module for user management, built with TypeORM and following NestJS best practices.
Features
- Complete CRUD Operations: Full Create, Read, Update, Delete functionality for users
- TypeORM Integration: Built-in database entity and migration support
- Validation: Input validation using class-validator
- Swagger Documentation: Auto-generated API documentation
- Modular Design: Easy to integrate into existing NestJS applications
- Migration Support: Database schema management with TypeORM migrations
Installation
npm install @uecsio/users-apiQuick Start
1. Import the Module
import { Module } from '@nestjs/common';
import { UsersModule } from '@uecsio/users-api';
@Module({
imports: [UsersModule],
})
export class AppModule {}2. Configure TypeORM
The package automatically provides the User entity and migrations. Make sure your TypeORM configuration includes:
import { UsersMigrationConfig } from '@uecsio/users-api';
// In your TypeORM config
{
entities: [...UsersMigrationConfig.entities],
migrations: [...UsersMigrationConfig.migrations],
migrationsTableName: UsersMigrationConfig.migrationsTableName,
}3. Use the Service
import { UsersService } from '@uecsio/users-api';
@Injectable()
export class MyService {
constructor(private readonly usersService: UsersService) {}
async createUser(userData: CreateUserDto) {
return this.usersService.create(userData);
}
}API Endpoints
The package automatically provides the following REST endpoints:
POST /users- Create a new userGET /users- Get all users (with pagination)GET /users/:id- Get a specific userPUT /users/:id- Update a userDELETE /users/:id- Delete a user
DTOs
CreateUserDto
{
username: string; // Required, unique, max 128 chars
password: string; // Required, max 128 chars
role: string; // Required
status: number; // Required (0: inactive, 1: active)
name?: string; // Optional, max 255 chars
town: string; // Required, max 255 chars
address: string; // Required
email?: string; // Optional, max 128 chars, email format
phone: string; // Required, max 50 chars
profile?: string; // Optional
}UpdateUserDto
Extends CreateUserDto with partial support, allowing you to update only specific fields.
Entity
The User entity includes:
id: Auto-generated primary keyusername: Unique username (128 chars max)password: User password (128 chars max)role: User rolestatus: User status (0: inactive, 1: active)name: Full name (optional, 255 chars max)town: Town/city (255 chars max)address: Full addressemail: Email address (optional, 128 chars max)phone: Phone number (50 chars max)profile: Additional profile info (optional)createdAt: Timestamp of creation
Database Indexes
The package automatically creates the following database indexes for optimal performance:
IDX_USERS_USERNAME: Unique index on usernameIDX_USERS_STATUS: Index on status for filteringIDX_USERS_ROLE: Index on role for filteringIDX_USERS_EMAIL: Index on email for lookups
Development
Building
npm run buildTesting
npm test
npm run test:watch
npm run test:covLinting
npm run lint
npm run lint:fixDependencies
Peer Dependencies
@nestjs/common: ^11.0.0@nestjs/core: ^11.0.0@nestjs/typeorm: ^11.0.0typeorm: ^0.3.0class-validator: ^0.14.0class-transformer: ^0.5.1@nestjs-library/crud: ^0.13.1
License
MIT
Contributing
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests
- Submit a pull request
Support
For issues and questions, please use the GitHub issue tracker.
