简体   繁体   中英

How to Bulk insert JSON array into mysql database with sequelize without knowing its model?

I have a JSON array of more than 50000+ records. is there any way I can use sequelize to bulk insert the dynamic data into the database as there is no model for the sequelize object?

If I understood correctly, you have a large array of json objects that you want to insert into mysql. Assuming the JSON structure is the same for all elements of the array, you can use sequelize.query after manipulating the objects into proper arrays for column names and values:

const colNames = Object.keys(bigArrayOfObjects[0]).map(elem => '`' + elem + '`').join(', ')

const rowValues = bigArrayOfObjects.map(row => Object.values(obj))

await sequelize.query(`
      INSERT INTO company (${colNames}) VALUES ${values.map(() => '(?)').join(',')}
    `, {
    replacements: values,
    type: Sequelize.QueryTypes.INSERT,
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM