0

I am new to node-postgres and I want to insert a row in a table using node-postgres, where one column value is an array of strings and one an array of integers.

Earlier I used to use sequelize and now I am not using any ORMS, want to use the native library for postgres in Node.

let names =['abc', 'qwe', 'aaa'];
let type = [2, 3, 4, 5];

let query = 'INSERT INTO  sample(names, type) VALUES ($1, $2)' ;
db.query(query, [names, type], (err, res) => {
   if (err) {
   return (err)
 }
 return res.send(res.rows[0])
})

I am thrown an error invalid syntax near "["

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Rohan Kumar
  • 181
  • 1
  • 2
  • 16
  • You want raw query ? – Ramesh S Apr 16 '19 at 10:00
  • @RameshS yes, I want a raw query using stored proceudres/parameterized query – Rohan Kumar Apr 16 '19 at 10:01
  • Use sequelize ORM it is better then raw query – Ramesh S Apr 16 '19 at 10:02
  • If these are `text` fields, you can do `JSON.stringify` and store the output. If I understood your question correctly... – Shaharyar Apr 16 '19 at 10:04
  • 1
    if you are using raw query then make it as per insert format https://stackoverflow.com/questions/34990186/how-do-i-properly-insert-multiple-rows-into-pg-with-node-postgres – Muhammad Ali Apr 16 '19 at 10:10
  • @RameshS, ORMs cause a performance lag when dealing with large scale of data because ORMs first manipulate the query and then the query is executed which takes a bit of time which is crucial when dealing with large scale of data. – Rohan Kumar Apr 16 '19 at 10:32
  • For the fastest and most scalable approach, see [multi-row inserts](https://stackoverflow.com/questions/37300997/multi-row-insert-with-pg-promise). – vitaly-t May 09 '19 at 23:17

0 Answers0