r/nextjs • u/Easy-Shelter-5140 • 8d ago
Domain driven design
Hi all!
I'm building an app with next.js and payload.
This application is growing and I would go for DDD.
Any suggestions on project structure?
I'm eager to read any wise advice.
2
Upvotes
4
u/Old-Register-4255 8d ago
Since you asked for a folder example, here's a rough one: src/modules/<domain>/ with its own service.ts (business logic), schema.ts (validation), queries.ts (db access) and types.ts, and for Payload put that domain's collection config and hooks in the same folder. Then src/app only holds thin routes and pages that call the module's service, and src/shared has what every domain uses, like auth, the db client and utils. One rule that keeps it honest: a module can import from shared, but not reach into another module's internals, only through its exported functions. And don't add layers like repositories and entities until a domain is actually complicated enough to need them.
3
u/BrownCarter 8d ago
Don't just over do the DDD
1
u/Slow-Bake-9603 7d ago
Cute!!!
2
u/SecureComfortable259 6d ago
I'd organize by feature first (modules/billing, modules/catalog) with domain/application/infrastructure inside each, and keep Payload collections in infrastructure so its types never leak into your domain.
2
u/phiger78 6d ago
I've followed DDD, or what I call "DDD Lite", within a monorepo architecture.
There are two main parts to Domain-Driven Design:
However, you don't necessarily need to adopt all the tactical patterns. For example, repositories may introduce unnecessary abstraction when you already have API clients and TanStack Query managing data retrieval and mutations.
In my usecase i adopted specific domain libraries that had enfroced rules to keep that bounded context. Inside of these domain libraries i used DTO's, mappers. Within each domain i adopted other folders
domain/data-access - Domain-specific API calls
domain/featyre - Domain logic, smart containers, and domain-specific hooks
domain/utils - Domain specific helpers or shared utilities
dmain/ui - Domain specific dumb UI components
I used a combinationof turborepo boundaries rules with eslint imports to enforce domain rules - eg you can't import other domains + specific rules around each folder- data-access must only contain data-access calls. It can't import from shared UI
1
u/Discordbothost 7d ago
The tricky bit with Payload is that the admin UI and your Next.js app can both change the same record. I'd pick one rule, like which status changes are allowed, then write a test that tries an invalid change through each path. That gives you a concrete reason to keep the rule in one place as you split things into modules.
2
u/[deleted] 8d ago
[removed] — view removed comment
0
u/[deleted] 5d ago
[removed] — view removed comment