postgres add rows from array of objects


This Content is from Stack Overflow. Question asked by Brandon-Perry

So what I’m trying to do is take an array of objects that are similar to the rows stored in the db (minus the id since that’s auto-generated). However, I can’t seem to figure it out. I’m using node-postgres. It would go something like this

const fooObj:Omit<Food, fooId> = {
   // foodId omitted
   a: 'a',
   b: 'b',
   c: 'c'

const fooArr = [...] // array of above Foo objects

const {rows} = pool.query(format(
 INSERT INTO "foo" ("a","b","c")
 VALUES // insert properties into column from each obj in fooArr //


This question is not yet answered, be the first one who answer using the comment. Later the confirmed answer will be published as the solution.

This Question and Answer are collected from stackoverflow and tested by JTuto community, is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?