How to migrate some route DTO object in NestJ?

I use NestJS and my app have some changes in the DTO objects that is expects to receive in the controller. The client side is a mobile app and I can’t force the users to update version, so I might get DTO objects that might not be in the updated version that my server side expects to receive.

What I want to do is to take the object I get and if I see the object is received from older version of the client side then I want this object to be migrated to the updated object type.

What is the best approach to do this?

Thanks in advance!

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

A simple solution can be adding a field that indicates that the recived object is the DTO’s updated version, check if that field exist or not, then apply the consequent logic.

Another way is to use the API versioning, you can find how to use it in the NestJS official documentation. This one I think is a better approach, this because if in the future you want to update again the DTO, you have only to create another controller version.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x