I have an array of values like this (restaurant_id
is not a primary key):
[
{restaurant_id:1, day_of_week:0, from_time: "12:00", to_time: "14:00", is_open:false },
{restaurant_id:1, day_of_week:1, from_time: "12:00", to_time: "14:00", is_open:true },
{restaurant_id:1, day_of_week:2, from_time: "12:00", to_time: "14:00", is_open:true },
...
]
One entry for every day.
I would like to save each of them as a new row in a PostgreSQL
database.
I have this query for one insert:
INSERT INTO schedules (restaurant_id, day_of_week, from_time, to_time, is_open) VALUES ($1, $2, $3, $4, $5) RETURNING schedules;
Should I do 7 INSERT
statements or can I loop and save all in one statement?
What would be the query with the loop?
EDIT:
So I could do in one query something like this, as suggested:
VALUES (?, ?, ?, ? ?),
(?, ?, ?, ? ?),
(?, ?, ?, ? ?),
(?, ?, ?, ? ?),
(?, ?, ?, ? ?),
(?, ?, ?, ? ?),
(?, ?, ?, ? ?)
but is there a better way of doing this?