Advertisement
Guest User

Untitled

a guest
Aug 1st, 2017
293
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function userFind(
  2.     t: Transaction,
  3.     options: {where: {username?: string, email?: string}},
  4.     callbackThen: (data: any) => Promise<any>,
  5.     callbackCatch?: (err: Error) => never
  6. ): Promise<any> {
  7.  
  8.     return User.findOne({
  9.             ...options,
  10.             transaction: t
  11.         })
  12.         .then(callbackThen)
  13.         .catch(callbackCatch || (err => {
  14.             throw new Error(); //Provoque un ROLLBACK
  15.         }));
  16.  
  17. }
  18.  
  19. //username = 'toto'
  20. //email = 'aaa@aol.com'
  21.  
  22. this.sequelize.transaction((t: Transaction) => {
  23.  
  24.     return userFind(t, {
  25.         where: {username: username}
  26.     }, (data: any) => {
  27.         if(data) {
  28.             throw new Error('...'); //Provoque un ROLLBACK
  29.         }
  30.  
  31.         return userFind(t, {
  32.             where: {email: email}
  33.         }, (data: any) => {
  34.             if(data) {
  35.                 throw new Error('...'); //Provoque un ROLLBACK
  36.             }
  37.  
  38.             //etc...
  39.         }
  40.     });
  41.  
  42. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement