0

I am having challenge on how to setup model for table object with arrays of responses in Sequelize ORM. I use Postgres DB. I have a table say foo. Foo has columns

A

B

C
> C1_SN

  C1_Name

  C1_Address

  C1_Phone

D

E

The column C has a boolean question, if the user select true, he will need to provide array of responses for C1. Such as we now have:

  C1_SN1

  C1_Name1

  C1_Address1

  C1_Phone1
------
  C1_SN2

  C1_Name2

  C1_Address2

  C1_Phone2
-----
  C1_SN3

  C1_Name3

  C1_Address3

  C1_Phone3

I expect multiple teams to be filling this table. How do I setup the model in sequelize? I have two options in mind.

Option 1

The first option I think of is to create an extra 1:1 table between Foo and C1. But going with this option, I don't know how to bulkCreate the array of C1 responses in the C1 table.

Option 2

I think it's also possible to make C1 column in Foo table have a nested array of values. Such that if userA submit his data, it will have the nested array of C1. But I don't know how to go about this method as well.

Medico
  • 45
  • 1
  • 2
  • 11

1 Answers1

1

You need to create separate table for C if user select true then need pass array of object and then pass in bulkCreate like.

C1_SN AutoIncrement
C1_NAME
C1_Address
C1_Phone

value=[{"C1_NAME":"HELLo","C1_Address":"HELLo","C1_Phone":"987456321"},{"C1_NAME":"HELLo1","C1_Address1":"HELLo","C1_Phone":"987456321s"}]

foo.bulkCreate(value).then(result=>{
console.log(result)
}).catch(error=>{
console.log(error)
})

From the official you can check this link: Sequelize bulkCreate

aum trivedi
  • 129
  • 4