[{"data":1,"prerenderedAt":795},["ShallowReactive",2],{"blog-en-\u002Fblog\u002Fnestjs-modulos-arquitetura-modular":3},{"id":4,"title":5,"body":6,"date":784,"description":785,"extension":786,"meta":787,"navigation":154,"path":788,"seo":789,"stem":790,"tags":791,"__hash__":794},"blog_en\u002Fblog\u002Fnestjs-modulos-arquitetura-modular.md","NestJS: modules don't guarantee a modular architecture",{"type":7,"value":8,"toc":778},"minimark",[9,13,18,21,25,28,31,336,342,738,757,760,764,767,771,774],[10,11,12],"p",{},"Why splitting files into modules doesn't solve coupling and how to think about the boundaries of the application.",[14,15,17],"h2",{"id":16},"the-initial-ease","The initial ease",[10,19,20],{},"NestJS encourages controllers, providers and modules. That structure helps a lot, but it can also give the feeling that any project organized into folders already has architectural boundaries.",[14,22,24],{"id":23},"dependency-matters-more-than-folder","Dependency matters more than folder",[10,26,27],{},"If the orders module directly accesses internal repositories of users, payments and inventory, the boundaries are only visual. I prefer to expose use cases or small contracts and keep domain entities independent of decorators when that brings a real benefit in testing and portability.",[10,29,30],{},"A common symptom of this problem is a service directly accessing other domains' repositories:",[32,33,38],"pre",{"className":34,"code":35,"language":36,"meta":37,"style":37},"language-typescript shiki shiki-themes darcula","@Injectable()\nexport class OrdersService {\n  constructor(\n    private readonly ordersRepository: OrdersRepository,\n    private readonly usersRepository: UsersRepository,\n    private readonly paymentsRepository: PaymentsRepository,\n    private readonly inventoryRepository: InventoryRepository,\n  ) {}\n\n  async createOrder(dto: CreateOrderDto) {\n    const user = await this.usersRepository.findById(dto.userId);\n    const inStock = await this.inventoryRepository.hasStock(dto.productId);\n    if (!inStock) throw new BadRequestException('out of stock');\n\n    await this.paymentsRepository.charge(user.id, dto.amount);\n    return this.ordersRepository.save(dto);\n  }\n}\n","typescript","",[39,40,41,57,70,79,98,113,128,143,149,156,174,210,239,266,271,303,324,330],"code",{"__ignoreMap":37},[42,43,46,50,54],"span",{"class":44,"line":45},"line",1,[42,47,49],{"class":48},"sE2CK","@",[42,51,53],{"class":52},"s2lgc","Injectable",[42,55,56],{"class":48},"()\n",[42,58,60,64,67],{"class":44,"line":59},2,[42,61,63],{"class":62},"sYMgc","export",[42,65,66],{"class":62}," class",[42,68,69],{"class":48}," OrdersService {\n",[42,71,73,76],{"class":44,"line":72},3,[42,74,75],{"class":62},"  constructor",[42,77,78],{"class":48},"(\n",[42,80,82,85,88,91,95],{"class":44,"line":81},4,[42,83,84],{"class":62},"    private",[42,86,87],{"class":62}," readonly",[42,89,90],{"class":48}," ordersRepository:",[42,92,94],{"class":93},"sZM-y"," OrdersRepository",[42,96,97],{"class":48},",\n",[42,99,101,103,105,108,111],{"class":44,"line":100},5,[42,102,84],{"class":62},[42,104,87],{"class":62},[42,106,107],{"class":48}," usersRepository:",[42,109,110],{"class":93}," UsersRepository",[42,112,97],{"class":48},[42,114,116,118,120,123,126],{"class":44,"line":115},6,[42,117,84],{"class":62},[42,119,87],{"class":62},[42,121,122],{"class":48}," paymentsRepository:",[42,124,125],{"class":93}," PaymentsRepository",[42,127,97],{"class":48},[42,129,131,133,135,138,141],{"class":44,"line":130},7,[42,132,84],{"class":62},[42,134,87],{"class":62},[42,136,137],{"class":48}," inventoryRepository:",[42,139,140],{"class":93}," InventoryRepository",[42,142,97],{"class":48},[42,144,146],{"class":44,"line":145},8,[42,147,148],{"class":48},"  ) {}\n",[42,150,152],{"class":44,"line":151},9,[42,153,155],{"emptyLinePlaceholder":154},true,"\n",[42,157,159,162,165,168,171],{"class":44,"line":158},10,[42,160,161],{"class":62},"  async",[42,163,164],{"class":52}," createOrder",[42,166,167],{"class":48},"(dto:",[42,169,170],{"class":93}," CreateOrderDto",[42,172,173],{"class":48},") {\n",[42,175,177,180,183,186,189,192,196,198,201,204,207],{"class":44,"line":176},11,[42,178,179],{"class":62},"    const",[42,181,182],{"class":48}," user = ",[42,184,185],{"class":62},"await",[42,187,188],{"class":62}," this",[42,190,191],{"class":48},".",[42,193,195],{"class":194},"sk7qV","usersRepository",[42,197,191],{"class":48},[42,199,200],{"class":52},"findById",[42,202,203],{"class":48},"(dto.",[42,205,206],{"class":194},"userId",[42,208,209],{"class":48},");\n",[42,211,213,215,218,220,222,224,227,229,232,234,237],{"class":44,"line":212},12,[42,214,179],{"class":62},[42,216,217],{"class":48}," inStock = ",[42,219,185],{"class":62},[42,221,188],{"class":62},[42,223,191],{"class":48},[42,225,226],{"class":194},"inventoryRepository",[42,228,191],{"class":48},[42,230,231],{"class":52},"hasStock",[42,233,203],{"class":48},[42,235,236],{"class":194},"productId",[42,238,209],{"class":48},[42,240,242,245,248,251,254,257,260,264],{"class":44,"line":241},13,[42,243,244],{"class":62},"    if",[42,246,247],{"class":48}," (!inStock) ",[42,249,250],{"class":62},"throw",[42,252,253],{"class":62}," new",[42,255,256],{"class":52}," BadRequestException",[42,258,259],{"class":48},"(",[42,261,263],{"class":262},"sZ3PO","'out of stock'",[42,265,209],{"class":48},[42,267,269],{"class":44,"line":268},14,[42,270,155],{"emptyLinePlaceholder":154},[42,272,274,277,279,281,284,286,289,292,295,298,301],{"class":44,"line":273},15,[42,275,276],{"class":62},"    await",[42,278,188],{"class":62},[42,280,191],{"class":48},[42,282,283],{"class":194},"paymentsRepository",[42,285,191],{"class":48},[42,287,288],{"class":52},"charge",[42,290,291],{"class":48},"(user.",[42,293,294],{"class":194},"id",[42,296,297],{"class":48},", dto.",[42,299,300],{"class":194},"amount",[42,302,209],{"class":48},[42,304,306,309,311,313,316,318,321],{"class":44,"line":305},16,[42,307,308],{"class":62},"    return",[42,310,188],{"class":62},[42,312,191],{"class":48},[42,314,315],{"class":194},"ordersRepository",[42,317,191],{"class":48},[42,319,320],{"class":52},"save",[42,322,323],{"class":48},"(dto);\n",[42,325,327],{"class":44,"line":326},17,[42,328,329],{"class":48},"  }\n",[42,331,333],{"class":44,"line":332},18,[42,334,335],{"class":48},"}\n",[10,337,338,341],{},[39,339,340],{},"OrdersService"," knows the internal schema of the users, payments and inventory modules — any change to them ripples back here. The path I prefer is to expose a use case with small contracts, keeping the domain entity free of decorators:",[32,343,345],{"className":34,"code":344,"language":36,"meta":37,"style":37},"interface FindUser {\n  findById(userId: string): Promise\u003CUser>;\n}\n\ninterface CheckInventory {\n  hasStock(productId: string): Promise\u003Cboolean>;\n}\n\ninterface ChargeCustomer {\n  charge(userId: string, amount: number): Promise\u003Cvoid>;\n}\n\n@Injectable()\nexport class CreateOrder {\n  constructor(\n    private readonly findUser: FindUser,\n    private readonly checkInventory: CheckInventory,\n    private readonly chargeCustomer: ChargeCustomer,\n    private readonly ordersRepository: OrdersRepository,\n  ) {}\n\n  async execute(input: CreateOrderInput): Promise\u003COrder> {\n    const user = await this.findUser.findById(input.userId);\n    const inStock = await this.checkInventory.hasStock(input.productId);\n    if (!inStock) throw new OutOfStockError(input.productId);\n\n    await this.chargeCustomer.charge(user.id, input.amount);\n\n    const order = Order.create(input);\n    return this.ordersRepository.save(order);\n  }\n}\n",[39,346,347,358,384,388,392,401,422,426,430,439,465,469,473,481,490,496,509,522,535,548,553,558,586,613,639,659,664,691,696,710,728,733],{"__ignoreMap":37},[42,348,349,352,355],{"class":44,"line":45},[42,350,351],{"class":62},"interface",[42,353,354],{"class":93}," FindUser",[42,356,357],{"class":48}," {\n",[42,359,360,363,366,369,372,375,378,381],{"class":44,"line":59},[42,361,362],{"class":52},"  findById",[42,364,365],{"class":48},"(userId:",[42,367,368],{"class":93}," string",[42,370,371],{"class":48},"):",[42,373,374],{"class":93}," Promise",[42,376,377],{"class":48},"\u003C",[42,379,380],{"class":93},"User",[42,382,383],{"class":48},">;\n",[42,385,386],{"class":44,"line":72},[42,387,335],{"class":48},[42,389,390],{"class":44,"line":81},[42,391,155],{"emptyLinePlaceholder":154},[42,393,394,396,399],{"class":44,"line":100},[42,395,351],{"class":62},[42,397,398],{"class":93}," CheckInventory",[42,400,357],{"class":48},[42,402,403,406,409,411,413,415,417,420],{"class":44,"line":115},[42,404,405],{"class":52},"  hasStock",[42,407,408],{"class":48},"(productId:",[42,410,368],{"class":93},[42,412,371],{"class":48},[42,414,374],{"class":93},[42,416,377],{"class":48},[42,418,419],{"class":93},"boolean",[42,421,383],{"class":48},[42,423,424],{"class":44,"line":130},[42,425,335],{"class":48},[42,427,428],{"class":44,"line":145},[42,429,155],{"emptyLinePlaceholder":154},[42,431,432,434,437],{"class":44,"line":151},[42,433,351],{"class":62},[42,435,436],{"class":93}," ChargeCustomer",[42,438,357],{"class":48},[42,440,441,444,446,448,451,454,456,458,460,463],{"class":44,"line":158},[42,442,443],{"class":52},"  charge",[42,445,365],{"class":48},[42,447,368],{"class":93},[42,449,450],{"class":48},", amount:",[42,452,453],{"class":93}," number",[42,455,371],{"class":48},[42,457,374],{"class":93},[42,459,377],{"class":48},[42,461,462],{"class":93},"void",[42,464,383],{"class":48},[42,466,467],{"class":44,"line":176},[42,468,335],{"class":48},[42,470,471],{"class":44,"line":212},[42,472,155],{"emptyLinePlaceholder":154},[42,474,475,477,479],{"class":44,"line":241},[42,476,49],{"class":48},[42,478,53],{"class":52},[42,480,56],{"class":48},[42,482,483,485,487],{"class":44,"line":268},[42,484,63],{"class":62},[42,486,66],{"class":62},[42,488,489],{"class":48}," CreateOrder {\n",[42,491,492,494],{"class":44,"line":273},[42,493,75],{"class":62},[42,495,78],{"class":48},[42,497,498,500,502,505,507],{"class":44,"line":305},[42,499,84],{"class":62},[42,501,87],{"class":62},[42,503,504],{"class":48}," findUser:",[42,506,354],{"class":93},[42,508,97],{"class":48},[42,510,511,513,515,518,520],{"class":44,"line":326},[42,512,84],{"class":62},[42,514,87],{"class":62},[42,516,517],{"class":48}," checkInventory:",[42,519,398],{"class":93},[42,521,97],{"class":48},[42,523,524,526,528,531,533],{"class":44,"line":332},[42,525,84],{"class":62},[42,527,87],{"class":62},[42,529,530],{"class":48}," chargeCustomer:",[42,532,436],{"class":93},[42,534,97],{"class":48},[42,536,538,540,542,544,546],{"class":44,"line":537},19,[42,539,84],{"class":62},[42,541,87],{"class":62},[42,543,90],{"class":48},[42,545,94],{"class":93},[42,547,97],{"class":48},[42,549,551],{"class":44,"line":550},20,[42,552,148],{"class":48},[42,554,556],{"class":44,"line":555},21,[42,557,155],{"emptyLinePlaceholder":154},[42,559,561,563,566,569,572,574,576,578,581,584],{"class":44,"line":560},22,[42,562,161],{"class":62},[42,564,565],{"class":52}," execute",[42,567,568],{"class":48},"(input:",[42,570,571],{"class":93}," CreateOrderInput",[42,573,371],{"class":48},[42,575,374],{"class":93},[42,577,377],{"class":48},[42,579,580],{"class":93},"Order",[42,582,583],{"class":48},">",[42,585,357],{"class":48},[42,587,589,591,593,595,597,599,602,604,606,609,611],{"class":44,"line":588},23,[42,590,179],{"class":62},[42,592,182],{"class":48},[42,594,185],{"class":62},[42,596,188],{"class":62},[42,598,191],{"class":48},[42,600,601],{"class":194},"findUser",[42,603,191],{"class":48},[42,605,200],{"class":52},[42,607,608],{"class":48},"(input.",[42,610,206],{"class":194},[42,612,209],{"class":48},[42,614,616,618,620,622,624,626,629,631,633,635,637],{"class":44,"line":615},24,[42,617,179],{"class":62},[42,619,217],{"class":48},[42,621,185],{"class":62},[42,623,188],{"class":62},[42,625,191],{"class":48},[42,627,628],{"class":194},"checkInventory",[42,630,191],{"class":48},[42,632,231],{"class":52},[42,634,608],{"class":48},[42,636,236],{"class":194},[42,638,209],{"class":48},[42,640,642,644,646,648,650,653,655,657],{"class":44,"line":641},25,[42,643,244],{"class":62},[42,645,247],{"class":48},[42,647,250],{"class":62},[42,649,253],{"class":62},[42,651,652],{"class":52}," OutOfStockError",[42,654,608],{"class":48},[42,656,236],{"class":194},[42,658,209],{"class":48},[42,660,662],{"class":44,"line":661},26,[42,663,155],{"emptyLinePlaceholder":154},[42,665,667,669,671,673,676,678,680,682,684,687,689],{"class":44,"line":666},27,[42,668,276],{"class":62},[42,670,188],{"class":62},[42,672,191],{"class":48},[42,674,675],{"class":194},"chargeCustomer",[42,677,191],{"class":48},[42,679,288],{"class":52},[42,681,291],{"class":48},[42,683,294],{"class":194},[42,685,686],{"class":48},", input.",[42,688,300],{"class":194},[42,690,209],{"class":48},[42,692,694],{"class":44,"line":693},28,[42,695,155],{"emptyLinePlaceholder":154},[42,697,699,701,704,707],{"class":44,"line":698},29,[42,700,179],{"class":62},[42,702,703],{"class":48}," order = Order.",[42,705,706],{"class":52},"create",[42,708,709],{"class":48},"(input);\n",[42,711,713,715,717,719,721,723,725],{"class":44,"line":712},30,[42,714,308],{"class":62},[42,716,188],{"class":62},[42,718,191],{"class":48},[42,720,315],{"class":194},[42,722,191],{"class":48},[42,724,320],{"class":52},[42,726,727],{"class":48},"(order);\n",[42,729,731],{"class":44,"line":730},31,[42,732,329],{"class":48},[42,734,736],{"class":44,"line":735},32,[42,737,335],{"class":48},[10,739,740,741,744,745,748,749,752,753,756],{},"The concrete implementations of ",[39,742,743],{},"FindUser",", ",[39,746,747],{},"CheckInventory"," and ",[39,750,751],{},"ChargeCustomer"," are still resolved by NestJS's injection container — behind a token provider, for example —, but ",[39,754,755],{},"CreateOrder"," only sees the contracts, not the users, inventory or payments modules behind them.",[10,758,759],{},"The injection container should assemble the dependencies. It doesn't need to appear in every rule. Global providers and circular imports are signs that responsibilities may be crossing the modules without a clear contract.",[14,761,763],{"id":762},"pragmatism","Pragmatism",[10,765,766],{},"Not every API needs multiple layers. For simple operations, controller, service and repository may be enough. The architecture should grow as there are invariants, integrations and different rhythms of change, not to fulfill an abstract design.",[14,768,770],{"id":769},"conclusion-for-now","Conclusion for now",[10,772,773],{},"NestJS offers good organization tools, but modularity comes from the dependencies that are allowed. A module is useful when it can explain what it offers and what it needs without exposing all of its details.",[775,776,777],"style",{},"html pre.shiki code .sE2CK, html code.shiki .sE2CK{--shiki-default:#A9B7C6}html pre.shiki code .s2lgc, html code.shiki .s2lgc{--shiki-default:#FFC66D}html pre.shiki code .sYMgc, html code.shiki .sYMgc{--shiki-default:#CC7832}html pre.shiki code .sZM-y, html code.shiki .sZM-y{--shiki-default:#6897BB}html pre.shiki code .sk7qV, html code.shiki .sk7qV{--shiki-default:#9876AA}html pre.shiki code .sZ3PO, html code.shiki .sZ3PO{--shiki-default:#6A8759}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}",{"title":37,"searchDepth":59,"depth":59,"links":779},[780,781,782,783],{"id":16,"depth":59,"text":17},{"id":23,"depth":59,"text":24},{"id":762,"depth":59,"text":763},{"id":769,"depth":59,"text":770},"2025-01-16","Splitting files into modules doesn't solve coupling: modularity comes from the dependencies you allow, not from the folder structure.","md",{},"\u002Fblog\u002Fnestjs-modulos-arquitetura-modular",{"title":5,"description":785},"blog\u002Fnestjs-modulos-arquitetura-modular",[792,36,793],"nestjs","architecture","dSbPElRyG4aAnYI7bLFxvYcre_hb6sk_IX5M5oKHYzY",1794928449668]