DEV Community

Brandon Jared Molina Vazquez
Brandon Jared Molina Vazquez

Posted on

Nest-Py: Experimenting with Decorators, DI, and Controllers in Python (FastAPI + NestJS style)

Nest-Py: A Tiny NestJS-Like Framework in Python Powered by FastAPI

I’ve been experimenting with metaprogramming in Python and decided to build a tiny prototype called Nest-Py.

It’s a small framework inspired by NestJS but built on top of FastAPI.

The goal?

To explore how we can bring concepts like controllers, decorators, and dependency injection into Python — similar to how NestJS structures applications in the Node.js ecosystem.

Disclaimer: This is just an experiment and learning project, not production-ready. But it shows how Python’s flexibility lets us play with framework design.


Why Nest-Py?

FastAPI is already amazing, but it leaves architecture decisions up to you.

NestJS, on the other hand, enforces a modular structure (controllers, services, modules, DI).

Nest-Py tries to combine both worlds:

  • Keep the speed and type hints of FastAPI.
  • Add structure with decorators, controllers, and a minimal DI context.

Example: Users and Employees

Here’s a small demo with two services (UserService, EmployeeService) and two controllers (UserController, EmployeeController).

from nest_py import get, controller, ctx # --- Mock Data --- users = { 1: {"username": "alice", "email": "alice@example.com"}, 2: {"username": "bob", "email": "bob@example.com"}, 3: {"username": "charlie", "email": "charlie@example.com"}, } employees = { 101: {"name": "John Doe", "role": "Manager", "salary": 55000}, 102: {"name": "Jane Smith", "role": "Developer", "salary": 48000}, 103: {"name": "Mark Lee", "role": "Designer", "salary": 45000}, } # --- Services --- class UserService: def get_users(self): return users def get_user(self, id: int): return users.get(id, None) class EmployeeService: def get_employees(self): return employees def get_employee(self, id: int): return employees.get(id, None) # --- Controllers --- @controller("/users") class UserController: service: UserService = None @get("/") async def get_users(): return ctx.service.get_users() @get("/{id}") async def get_user(id: int): return ctx.service.get_user(id) @controller("/employees") class EmployeeController: service: EmployeeService = None @get("/") async def get_employees(): return ctx.service.get_employees() @get("/{id}") async def get_employee(id: int): return ctx.service.get_employee(id) from nest_py.core import factory # minimal bootstrap 
Enter fullscreen mode Exit fullscreen mode

Top comments (0)