Query increment db field value with transaction

I am trying to increment a field in the database by a certain number using strapis query. I also want to do this using transaction.

I have found many articles and documentation that seem to be stale. I will paste them below.

// 'transacting' does not exist on query
await strapi.query('api::ticket.ticket').transacting(transacting).where('id', id).increment('value', order_ticket.quantity)

// 'where' does not exist on query
await strapi.query('api::ticket.ticket').where('id', id).increment('quantity', order_ticket.quantity)

// Expected a valid Number, got `quantity` + 2
await strapi.query('api::ticket.ticket').update({ 
  where: {
    id: order_ticket.ticket_id, 
  },
  data: {
    quantity: strapi.db.connection.raw(`?? + ?`, ['quantity', 2])
  }
}, null, { transacting });

References:

I haven’t used transactions in Strapi but here is a recent article showing some examples. Using transactions with Strapi v4 - with tests

Hope this helps.

Thanks for the reply. My question is more to do with incrementing an existing number in the database - and then doing that within transactions.

Transactions i have working fine.

I will need to do some more digging to see if I can find out how to accomplish this.

You can do increment and decrement on strapi database fields like this:

const req = strapi.db.connection('tickets')
.where({id: 123})
.increment('quantity_available', 1)
.decrement('quantity_sold', 1)

Inside a transaction:

const req = strapi.db.connection('tickets')
.where({id: 123})
.increment('quantity_available', 1)
.decrement('quantity_sold', 1)
.transacting(trx)