Answer the question
In order to leave comments, you need to log in
node+pg. How to insert multiple rows into a table?
Tell me, I can’t insert many lines from an object into the database.
Server on Node + express, to connect to the database I use pg
Data for example.
const data = [
{ id: 1, status: 'ok', type: 'def', time: '22:15' },
{ id: 2, status: 'error', type: 'error', time: '22:15' },
{ id: 3, status: 'ok', type: 'def', time: '22:15' },
]
await pool.query(
`INSERT INTO public.orders(id, status, type, time) \
VALUES ($1, $2, $3, $4) RETURNING *`,
data.map((item) => [item.id, item.status, item.type, item.time])
)
Answer the question
In order to leave comments, you need to log in
something like this
await pool.query(
`INSERT INTO public.orders(id, status, type, time) \
SELECT unnest(array[%1]), unnest(array[%2]), unnest(array[%3]), unnest(array[%4])`,
[
data.map(item => item.id),
data.map(item => item.status),
data.map(item => item.type),
data.map(item => item.time),
]
)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question