Package Exports
- @uecsio/pages-api
- @uecsio/pages-api/dist/index.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@uecsio/pages-api) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
@uecsio/pages-api
A reusable NestJS Pages module for content management with full CRUD operations, validation, and Swagger documentation.
๐ Features
- Full CRUD Operations: Create, Read, Update, Delete pages
- TypeORM Integration: Built-in database support
- Validation: Class-validator integration for DTOs
- Swagger Documentation: Auto-generated API documentation
- Flexible Configuration: Easy to customize and extend
- TypeScript Support: Full type safety
๐ฆ Installation
npm install @uecsio/pages-api๐ง Dependencies
This package requires the following peer dependencies in your NestJS application:
npm install @nestjs/common @nestjs/typeorm typeorm class-validator class-transformer @nestjs-library/crud๐ Usage
1. Import the Module
import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { PagesModule } from '@uecsio/pages-api';
@Module({
imports: [
TypeOrmModule.forRoot({
type: 'postgres',
host: 'localhost',
port: 5432,
username: 'postgres',
password: 'password',
database: 'your_database',
entities: [__dirname + '/**/*.entity{.ts,.js}'],
synchronize: true, // Only in development
}),
PagesModule,
],
})
export class AppModule {}2. Use the Service
import { Injectable } from '@nestjs/common';
import { PagesService, CreatePageDto, Page } from '@uecsio/pages-api';
@Injectable()
export class YourService {
constructor(private readonly pagesService: PagesService) {}
async createPage(data: CreatePageDto): Promise<Page> {
return this.pagesService.create(data);
}
async getAllPages(): Promise<Page[]> {
return this.pagesService.findAll();
}
async getPageById(id: number): Promise<Page> {
return this.pagesService.findOne(id);
}
}3. Use the Controller
The package automatically provides a REST API with the following endpoints:
POST /pages- Create a new pageGET /pages- Get all pagesGET /pages/:id- Get a specific pagePUT /pages/:id- Update a pageDELETE /pages/:id- Delete a page
4. Database Migrations
The package includes database migrations that are automatically discovered by the consuming application. The migrations will be executed externally via your TypeORM settings.
Automatic Discovery (Recommended)
The consuming application automatically discovers all @uecsio packages and their migrations:
// No manual configuration needed - migrations are auto-discovered!
@Module({
imports: [
TypeOrmModule.forRoot({
type: 'postgres',
host: 'localhost',
port: 5432,
username: 'postgres',
password: 'password',
database: 'your_database',
// Migrations and entities are automatically discovered from @uecsio packages
synchronize: false, // Always false for production
}),
PagesModule,
],
})
export class AppModule {}Manual Integration (Alternative)
If you prefer manual control, you can still integrate manually:
import { PagesMigrationConfig } from '@uecsio/pages-api';
@Module({
imports: [
TypeOrmModule.forRoot({
type: 'postgres',
host: 'localhost',
port: 5432,
username: 'postgres',
password: 'password',
database: 'your_database',
entities: [...PagesMigrationConfig.entities],
migrations: [...PagesMigrationConfig.migrations],
migrationsTableName: 'migrations', // Standard migration table
synchronize: false, // Always false for production
}),
PagesModule,
],
})
export class AppModule {}Note: This module uses the standard migrations table name, so it will work seamlessly with other modules that also use the same migration table.
Available migrations
The package provides the following migration:
CreatePagesTable1700000000000- Creates the pages table with all required columns and indexes
Migration execution
Migrations will be executed automatically by TypeORM when you run:
# Generate new migrations
npm run typeorm migration:generate
# Run pending migrations
npm run typeorm migration:run
# Revert last migration
npm run typeorm migration:revert5. Customize the Page Entity
You can extend the Page entity for your specific needs:
import { Entity } from 'typeorm';
import { Page } from '@uecsio/pages-api';
@Entity()
export class CustomPage extends Page {
// Add your custom fields here
@Column()
customField: string;
}๐๏ธ Architecture
src/
โโโ entities/
โ โโโ page.entity.ts # Page database entity
โโโ dto/
โ โโโ create-page.dto.ts # Create page DTO
โ โโโ update-page.dto.ts # Update page DTO
โโโ services/
โ โโโ pages.service.ts # Business logic service
โโโ controllers/
โ โโโ pages.controller.ts # REST API controller
โโโ pages.module.ts # NestJS module
โโโ index.ts # Package exports๐ API Documentation
Once the module is imported, you can access the Swagger documentation at:
http://localhost:3000/api๐งช Testing
# Run tests
npm test
# Run tests with coverage
npm run test:cov
# Run tests in watch mode
npm run test:watch๐ Configuration
Environment Variables
The module uses the following environment variables (with defaults):
DB_HOST=localhost
DB_PORT=5432
DB_USERNAME=postgres
DB_PASSWORD=password
DB_NAME=your_databaseCustom Database Configuration
You can override the database configuration:
import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { PagesModule } from '@uecsio/pages-api';
@Module({
imports: [
TypeOrmModule.forRoot({
// Your custom database configuration
type: 'mysql',
host: 'your-host',
// ... other options
}),
PagesModule,
],
})
export class AppModule {}๐ Extending the Module
Custom Service Methods
import { Injectable } from '@nestjs/common';
import { PagesService } from '@uecsio/pages-api';
@Injectable()
export class CustomPagesService extends PagesService {
async findByStatus(status: number) {
return this.repository.find({ where: { status } });
}
}Custom Controller Methods
import { Controller } from '@nestjs/common';
import { PagesController } from '@uecsio/pages-api';
@Controller('custom-pages')
export class CustomPagesController extends PagesController {
// Add your custom endpoints here
}๐ค Contributing
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
๐ License
This project is licensed under the MIT License - see the LICENSE file for details.
๐ Support
If you encounter any problems or have questions, please:
- Check the documentation
- Search existing issues
- Create a new issue
๐ Changelog
v1.0.0
- Initial release
- Full CRUD operations
- TypeORM integration
- Swagger documentation
- Validation support