I’m building an API with Express.js, TypeScript, and Mongoose, and I’m aiming for industry best practices. I have a UserEntity with required fields like id, but during creation, Mongoose automatically generates the id. I don’t want to make a workaround by marking the id optional, and creating separate entities for each operation (e.g. UserCreateEntity, UserUpdateEntity, etc.) seems excessive.
How do I handle this situation in a clean and maintainable way?
Here’s my current data flow:
Express controller receives the request and converts it to CreateUserRequestDto
Passes it to CreateUserUseCase, which converts the dto to UserEntity to trigger business rules and validation
Then passes it to the repository to create the Mongoose document.
Any guidance or advice would be greatly appreciated!
Raff B is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.