Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

how to pass sequelize transaction to association helper method

// 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

how to pass sequelize transaction to association helper method

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 :: javascript cahnge colour of strokerect 
Javascript :: redirect users to anmother page in javascript 
Javascript :: js toggle class 
Javascript :: angular disabled condition based 
Javascript :: copy text to clipboard javascript without input 
Javascript :: js json stringfy beutify 
Javascript :: TypeError: client.guilds.forEach is not a function 
Javascript :: node js to int 
Javascript :: javascript check if argument is passed 
Javascript :: checkbox event listener 
Javascript :: json array to string in postgresql 
Javascript :: mock a function jest react 
Javascript :: npm yarn run shell script 
Javascript :: pageyoffset jquery 
Javascript :: semantic ui dropdown value react 
Javascript :: how to render a new page in node js through express 
Javascript :: javascript remove final newline from string 
Javascript :: vanilla javascript add class 
Javascript :: django jquery 
Javascript :: html2canvas angular 
Javascript :: js input type range on hover 
Javascript :: ref css in jsp 
Javascript :: get the next character javascript 
Javascript :: foreach reverse javascript 
Javascript :: how to hash with crypto Node.js 
Javascript :: js convert array of array to array 
Javascript :: jquery growl cdn 
Javascript :: connect mongoose from node js 
Javascript :: javascript split by backslash 
Javascript :: javascrip check if string contains substring 
ADD CONTENT
Topic
Content
Source link
Name
1+1 =