skip to Main Content

I’m developing a full-stack application (React, Node, Postgres). When receiving the form data from the frontend, I expect certain variable names sush as firstName and I map firstName to first_name on the backend server to insert it into the postgres db. Now my question is regarding the GET requests and sending the data back to the front end:
Should I map the names back to the frontend naming convention on the server before I send the json (map first_name back to firstName) or handle the mapping on the frontend?
What is the best practice regarding such situations?

2

Answers


  1. In your SQL queries you can get the data renamed:

    SELECT first_name as "firstName"
    

    The object returned from the query will have the properties named correctly.

    Login or Signup to reply.
  2. Yes, the best practice is to keep the code for the mapping in a single place: either in the backend, or in the frontend, not both. Keep the naming convention within each API consistent.

    If your backend accepts firstName as input and does the mapping internally when inserting in the database, it should also do the mapping internally when reading from the database and provide firstName as output.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search