I'm currently building a NodeJS express API to run with a ReactJS front end. Coming from mongodb where everything is a document I'm a little bit lost about how to gather data from SQL. Let me explain.
To build a ReactJS application working with an API Rest I need proper Objects structure in front end.
Taking the example of a basic application where a user can place bets on a match, I would have
TABLE users (
_id SERIAL PRIMARY KEY,
username varchar(255) UNIQUE,
password varchar(255)
);
TABLE matches (
_id SERIAL PRIMARY KEY,
EQA varchar(255), // These should be ids to teams but
EQB varchar(255), // we try to keep it simple
EQA_score INTEGER,
EQB_score INTEGER,
);
TABLE bets (
_id SERIAL PRIMARY KEY,
user_id INTEGER REFERENCES users(_id),
match_id INTEGER REFERENCES matches(_id),
EQA_bet INTEGER,
EQB_bet INTEGER,
);
Then to display the history of the pronostics in ReactJS it would be very simple with datas structured this way:
{
username: 'Random name',
bets: [
{
EQA_bet: 1,
EQB_bet: 0,
match: {
EQA: 'Paris',
EQB: 'London',
EQA_score: 1,
EQB_score: 0,
}
},
{
...
}
]
}
To build this object I don't know what would be the best:
- One query for user, bets and matches and build my object in JS
- One query with joins then some long code to build my object
- Some other tools / strategies I don't know yet
Thanks for understanding my issue. This is all about not having a flat object to interpret in front end