Yoooomi
Yoooomi

Reputation: 31

Build NodeJS Object from SQL

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:

Thanks for understanding my issue. This is all about not having a flat object to interpret in front end

Upvotes: 0

Views: 771

Answers (1)

S.Mishra
S.Mishra

Reputation: 3664

@Yooomi, you can use sequelize NPM Package to create your object models and can define relationships between different models. You can do all C.R.U.D. operations through this. The response you get through this package will be in JSON format. You can change it as per your need.

You can go through different blogs and find the usage of the sequelize. I search on google and found this article which seems a good start.

Let me know if that is useful.

Upvotes: 1

Related Questions