Database transaction with bulk insert


How to bulk insert when using db transaction ?

const firstUserId = await Database
  .insert([{username: 'foo'}, {username: 'bar'}])
const trx = await Database.beginTransaction()
await trx.insert([{username: 'foo'}, {username: 'bar'}])).into('users')

What is the proper way to do that


I think there are two way to achieve this.

  1. Using Database instance (knex.js) -
  2. Using Lucid Model -

if you are using lucid model, just pass the trx you declare to the second argument.

const insertData = [
const dbTrx = await Database.beginTransaction()
await YourModel.createMany(insertData, dbTrx)

hope its help:grinning: