Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

perform database transaction with sequelize

// First, we start a transaction and save it into a variable
const t = await sequelize.transaction();

try {

  // Then, we do some calls passing this transaction as an option:

  const user = await User.create({
    firstName: 'Bart',
    lastName: 'Simpson'
  }, { transaction: t });

  await user.addSibling({
    firstName: 'Lisa',
    lastName: 'Simpson'
  }, { transaction: t });

  // If the execution reaches this line, no errors were thrown.
  // We commit the transaction.
  await t.commit();

} catch (error) {

  // If the execution reaches this line, an error was thrown.
  // We rollback the transaction.
  await t.rollback();

}
Comment

sequelize transaction util

//TransactionExecutor.js
import { DbConnection } from './dataSource';
const { Transaction } = require('sequelize');
const executeTransaction = (callBack) => {
   return DbConnection().transaction({
        isolationLevel: Transaction.ISOLATION_LEVELS.READ_COMMITTED,
   }, (t) => callBack(t));
};
export {
   executeTransaction,
};
Comment

how to perform transaction with sequelize

return sequelize.transaction().then(function (t) {
  return User.create({
    firstName: 'Homer',
    lastName: 'Simpson'
  }, {transaction: t}).then(function (user) {
    return user.addSibling({
      firstName: 'Lisa',
      lastName: 'Simpson'
    }, {transaction: t});
  }).then(function () {
    return t.commit();
  }).catch(function (err) {
    return t.rollback();
  });
});
Comment

sequelize transaction

try {

  const result = await sequelize.transaction(async (t) => {

    const user = await User.create({
      firstName: 'Abraham',
      lastName: 'Lincoln'
    }, { transaction: t });

    await user.setShooter({
      firstName: 'John',
      lastName: 'Boothe'
    }, { transaction: t });

    return user;

  });

  // If the execution reaches this line, the transaction has been committed successfully
  // `result` is whatever was returned from the transaction callback (the `user`, in this case)

} catch (error) {

  // If the execution reaches this line, an error occurred.
  // The transaction has already been rolled back automatically by Sequelize!

}
Comment

PREVIOUS NEXT
Code Example
Javascript :: Nodemon continuously restart 
Javascript :: datepicker date and time 
Javascript :: set state using object 
Javascript :: JavaScript Extract Values 
Javascript :: 2 dimensional array index of element value 
Javascript :: javascript keylogger 
Javascript :: hot to start cypress 
Javascript :: Example of Reactjs Controlled-Components 
Javascript :: merge two singly linked lists 
Javascript :: check variable value and do something 
Javascript :: asynch action redux 
Javascript :: nodejs mysql transactions 
Javascript :: shape of array in js 
Javascript :: javascript log where function was called 
Javascript :: javascript destructing 
Javascript :: jquery get return jquery object 
Javascript :: plus in javascript 
Javascript :: gps nodejs 
Javascript :: mongoose max record 
Javascript :: what is react reveal 
Javascript :: how to get time zone difference date-fns 
Javascript :: jest check if button is disabled 
Javascript :: javascript number and math 
Javascript :: Example React Hook 
Javascript :: Nuxt.js + Electron 
Javascript :: js get elements in array from x to y 
Javascript :: vue js documentation 
Javascript :: window frames javascript 
Javascript :: how to sort an array in js 
Javascript :: js add event listener 
ADD CONTENT
Topic
Content
Source link
Name
1+9 =