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 :: This version of CLI is only compatible with Angular versions 0.0.0 || ^9.0.0-beta || =9.0.0 <10.0.0, but Angular version 10.0.14 was found instead. 
Javascript :: string to array javascript 
Javascript :: checkbox change event javascript 
Javascript :: fetch javascript 
Javascript :: copy to clipboard using javascript 
Javascript :: how to make a vowel counter in javascript 
Javascript :: javascript get point of line intersection 
Javascript :: how to specify max number of character angular mat input 
Javascript :: js how to remove # from any url using js 
Javascript :: export default arrow function 
Javascript :: how to catch and throw error js 
Javascript :: js get id value 
Javascript :: .call javascript 
Javascript :: javascript convert character to ascii 
Javascript :: semantic ui dropdown value 
Javascript :: add array of object to state react 
Javascript :: how to change image source using javascript 
Javascript :: Jquery handle change event of file upload created dynamically 
Javascript :: date format in react js 
Javascript :: js add to array conditionally 
Javascript :: js input type range get value on select 
Javascript :: js number to scientific notation 
Javascript :: How to call a c# functio from Javascript 
Javascript :: loop through array backwards 
Javascript :: what is ngmodel property binding 
Javascript :: socket io close client connection 
Javascript :: javascript create element with class 
Javascript :: Remove duplication from array in javascript 
Javascript :: js onchange input value event listene 
Javascript :: check if string contains character javascript 
ADD CONTENT
Topic
Content
Source link
Name
6+1 =