Tuesday, 5 February 2019

What’s the best way to transfer data in a Node Restful API?

Hi all, It might be a silly question but it comes from the age old problem for me that all the tutorials are so basic and it’s quite impossible to find reliable info on how to create a production ready NodeJs backend API.I recently started a project with Node (Typescript) + Express, using TypeORM for persisting data to MySQL database.Coming from an OOP background, I am used to having model entities separate from DTOs.All the tutorials use the model classes for accepting data from post requests and to return data with get requests. But i need something more advanced.For example, with TypeORM I also have relationships between two entities. So if I want the Book sql table to have AuthorId column, I have to declare corresponding ManyToOne and OneToMany relationships and properties for both model classes.So, when I query the book (or use post request to create one), I want to receive only the author ID, not the whole object.So that means that for the data transport, I would need a separate class/type/interface for that but I haven’t found many solutions or best practices for this in Node.Right now it seems that creating those manual mappings is the only choice, but maybe I am missing something.Thanks in advance!

Submitted February 05, 2019 at 09:26PM by vsamma

No comments:

Post a Comment