Answer the question
In order to leave comments, you need to log in
How is it more competent to return database SQL entities with relationships within the REST-api?
I am writing a food delivery app.
On the server side, there is a table of orders - Orders of the following form:
CREATE TABLE orders(
id INTEGER PRIMARY KEY,
created_time DATETIME
);
CREATE TABLE order_statuses(
id INTEGER PRIMARY KEY,
created_time DATETIME,
status_code INTEGER,
order_id INTEGER,
FOREIGN KEY(order_id) REFERENCES orders(id)
);
/orders - для заказов
/orders/{order_id}/statuses - для статусов конкретного заказа
Answer the question
In order to leave comments, you need to log in
rest is a fuzzy thing, there are no rules carved in granite.
if you need a status in an order, then give it along with the order.
https://fractal.thephpleague.com
allows you to include related entities like
/orders?include=client
but this is usually done for one-to-one, many-to-one relationships
relationships for one to many relationships, I usually do /orders/{order_id}/ statuses
if statuses depends hard on order does not exist without it.
You can do JOIN with pagination (batch request).
For example, "return a list of orders and statuses for X-records of orders in the amount of N".
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question