Thursday, 1 March 2018

Best practices for working with DTOs in Node?

To represent the DTOs (the objects that get sent to the client in API responses), I build them right in my controllers (either manually or using some helper functions):app.get('/api/user/:id', (req, res) => { var user = getUserById(req.params.id); var postsCount = getPostsCount(user); res.json({ name: user.name, email: user.email, postsCount: postsCount }); }); However, in other frameworks (like Spring) they are often defined declaratively as classes with annotated fields, and the framework takes care of converting ORM models or domain objects to DTOs.Is my example a state-of-the-art way of working with DTOs in Node, or is there a better approach?Thanks!P.S. I'm talking about big apps here. For small ones, I'm sure building DTOs manually is the simplest and reasonably convenient approach.P.P.S. I'm using TypeScript, but I'm interested in approaches for regular JS as well.

Submitted March 01, 2018 at 02:14PM by smthamazing

No comments:

Post a Comment