I am developing a backend route for my application and it needs to add multiple rows (one row for each item in an array received from the client client). My first thought is to just do a for
loop through my data and do a database query for each array item as shown below:
router.get('api/postGuests', (req, res) => {
const arrayOfPeople = req.body;
// arrayOfPeople looks like this for example: [{name: Jason, age: 24}, {name: Tyler, age: 34}]
for (let i=0; i<arrayOfPeople.length; i++) {
db.query('INSERT INTO people (name, age) VALUES (?, ?)', [arrayofPeople[i].name, arrayOfPeople[i].age])
}
})
The problem is I don't think it is efficient to make a separate query for each row, and given that these queries are asynchronous, I can see there being a lot of issues arising as I scale.
Can someone give me some advice on how to best tackle this situation? Thank you!