DEV Community

Cover image for FastNest: Bringing NestJS Modular Architecture to FastAPI
hamza el mouddane
hamza el mouddane

Posted on

FastNest: Bringing NestJS Modular Architecture to FastAPI

The Problem: FastAPI is Great, but Scaling is Hard

FastAPI is undoubtedly one of the fastest and most developer-friendly frameworks in Python. However, as projects grow into enterprise-level applications, maintaining a clean structure can become a challenge. Many developers coming from the Node.js ecosystem miss the Modular Architecture and Dependency Injection patterns of NestJS.
The Solution: Introducing FastNest

I built FastNest to bridge this gap. It’s a progressive Python framework built on top of FastAPI that enforces a structured approach without sacrificing performance.

Key Features 🌟

Modular System: Organize your code into isolated, reusable modules using the @Module decorator.

Powerful Dependency Injection: A built-in IoC container that manages service lifecycles and injections automatically.

Real-time Ready: Native WebSocket Gateways with event-based decorators (@SubscribeMessage).

Enterprise Patterns: Standardized interfaces for Guards (Authentication/Authorization), Interceptors, and Pipes (Validation).

πŸ› οΈ Quick Start

Want to see it in action? Here is how you can set up a modular application in seconds.

  1. Install FastNest Bash

pip install fastnest

  1. Create your first Module Python
from fastnest.core import Module, Controller, Get

@Controller('/hello')
class AppController:
    @Get('/')
    def get_hello(self):
        return {"message": "Hello from FastNest!"}

@Module(
    controllers=[AppController],
    providers=[]
)
class AppModule:
    pass
Enter fullscreen mode Exit fullscreen mode
  1. Bootstrap and Run Python
from fastnest.core import FastNestFactory
from .app_module import AppModule

app = FastNestFactory.create(AppModule)
Enter fullscreen mode Exit fullscreen mode

πŸ›°οΈ Real-time WebSockets

One of the most exciting features of FastNest is how it handles WebSockets. You no longer need to manage complex connection loops manually.

Python


@WebSocketGateway("/chat")
class ChatGateway(OnGatewayConnection):
    async def on_connection(self, websocket: WebSocket):
        await websocket.accept()

    @SubscribeMessage("events")
    async def handle_event(self, client: WebSocket, data: any):
        return {"event": "reply", "data": data}
Enter fullscreen mode Exit fullscreen mode

πŸ“‚ Project Structure

FastNest encourages a clean, professional layout:

core/: The DI engine and decorators.  

common/: Reusable Guards, Pipes, and Interceptors.  

example/: Fully working implementations to get you started.  
Enter fullscreen mode Exit fullscreen mode

🀝 Roadmap & Contributing

FastNest is currently in version 0.1.1. I’m planning to add more built-in decorators for database integration and enhanced CLI tools.

If you like the project, feel free to give it a ⭐️ on GitHub or open an issue for suggestions!

GitHub: hamza-elmoudden/fastnest



PyPI: fastnest 0.1.1
Enter fullscreen mode Exit fullscreen mode

Top comments (0)