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

perform database transaction with sequelize

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 :: sequelize transaction 
Javascript :: electronjs 
Javascript :: javascript Symbol Methods 
Javascript :: es6 foreach dom element 
Javascript :: jquery get return jquery object 
Javascript :: how to prevent clickjacking in react js 
Javascript :: useselector in redux 
Javascript :: js object to c# object 
Javascript :: copy text to the clipboard 
Javascript :: online password generator 
Javascript :: get width of html photo 
Javascript :: ajax returning html instead of json 
Javascript :: limit number in javascript 
Javascript :: javascript closest parent 
Javascript :: string javascript concatenation 
Javascript :: confirm alert 
Javascript :: jq break line 
Javascript :: button click 
Javascript :: how to async javascript stack overflow 
Javascript :: call,bind and apply in javascript 
Javascript :: $(...).Datatables is not a function 
Javascript :: js fadeout 
Javascript :: js insert 
Javascript :: context api example in react 
Javascript :: open bootstrap modal using vanilla js 
Javascript :: add new html from javascript 
Javascript :: jqueyr get parent 
Javascript :: react js photo gallery 
Javascript :: autocomplete html in react 
Javascript :: concatenate arrays javascript 
ADD CONTENT
Topic
Content
Source link
Name
8+8 =