Skip to Content
πŸš€ APSO is now in public beta. Get started β†’
GuidesFrameworksTypeScript (NestJS)Overview

TypeScript (NestJS)

The TypeScript template uses NestJS, a progressive Node.js framework for building efficient and scalable server-side applications.

Overview

ComponentTechnology
FrameworkNestJS 10
ORMTypeORM
Validationclass-validator
DocumentationSwagger/OpenAPI
TestingJest

Quick Start

# Create a new TypeScript service apso server new my-api --template typescript # Navigate to project cd my-api # Install dependencies npm install # Start development server npm run dev

Project Structure

my-api/ β”œβ”€β”€ src/ β”‚ β”œβ”€β”€ entities/ # TypeORM entities β”‚ β”‚ β”œβ”€β”€ project.entity.ts β”‚ β”‚ └── task.entity.ts β”‚ β”œβ”€β”€ modules/ # NestJS modules β”‚ β”‚ β”œβ”€β”€ projects/ β”‚ β”‚ β”‚ β”œβ”€β”€ projects.module.ts β”‚ β”‚ β”‚ β”œβ”€β”€ projects.controller.ts β”‚ β”‚ β”‚ β”œβ”€β”€ projects.service.ts β”‚ β”‚ β”‚ └── dto/ β”‚ β”‚ └── tasks/ β”‚ β”œβ”€β”€ extensions/ # Your custom code β”‚ β”‚ β”œβ”€β”€ controllers/ β”‚ β”‚ β”œβ”€β”€ services/ β”‚ β”‚ └── modules/ β”‚ β”œβ”€β”€ common/ # Shared utilities β”‚ β”‚ β”œβ”€β”€ decorators/ β”‚ β”‚ β”œβ”€β”€ guards/ β”‚ β”‚ └── interceptors/ β”‚ β”œβ”€β”€ config/ # Configuration β”‚ β”œβ”€β”€ app.module.ts β”‚ └── main.ts β”œβ”€β”€ test/ # E2E tests β”œβ”€β”€ .apsorc # APSO schema β”œβ”€β”€ package.json └── tsconfig.json

Key Technologies

NestJS

NestJS provides a robust architecture with:

  • Modules for code organization
  • Controllers for HTTP handling
  • Services for business logic
  • Dependency injection
  • Middleware and guards

TypeORM

Type-safe database access:

  • Entity definitions
  • Migrations
  • Query builder
  • Repository pattern

Class-Validator

Request validation with decorators:

import { IsString, IsNotEmpty, MaxLength } from 'class-validator'; export class CreateProjectDto { @IsString() @IsNotEmpty() @MaxLength(200) name: string; @IsString() @IsOptional() description?: string; }

Configuration

Environment Variables

# .env DATABASE_URL=postgresql://user:pass@localhost:5432/db JWT_SECRET=your-secret-key PORT=3001 NODE_ENV=development

TypeORM Configuration

// src/config/database.config.ts export const databaseConfig = { type: 'postgres', url: process.env.DATABASE_URL, entities: [__dirname + '/../entities/*.entity{.ts,.js}'], migrations: [__dirname + '/../migrations/*{.ts,.js}'], synchronize: process.env.NODE_ENV === 'development', };

Commands

CommandDescription
npm run devStart with hot reload
npm run buildBuild for production
npm startStart production server
npm testRun unit tests
npm run test:e2eRun E2E tests
npm run lintLint code

Features

Auto-Generated

  • Entity definitions with TypeORM decorators
  • CRUD controllers with validation
  • Services with repository injection
  • DTOs for create/update operations
  • Swagger documentation
  • Basic authentication guards

Customizable

  • Add custom endpoints in extensions
  • Override generated services
  • Add middleware and guards
  • Integrate external services

Next Steps

Last updated on