haoliangyu / pg-reactive

PostgreSQL + RxJS = pg-reactive
https://haoliangyu.github.io/pg-reactive/
MIT License
41 stars 2 forks source link

how to transform the result of query to use it in anothers #5

Closed jcaviedesc closed 7 years ago

jcaviedesc commented 7 years ago

hello, I am trying to transform the result into the first query and then use it in the next query


let devolution;   
let one = pgrx.query('UPDATE public.appoinment SET status=$1 WHERE id_appoinment = $2 RETURNING date;', [status, id_appointment])
      .do((result) =>{
        let start_actual_time = Math.round(new Date().getTime()/1000.0)
        let end_actual_time = Math.round(new Date(result.date).getTime()/1000.0);
        let diff = end_actual_time - start_actual_time;      
        if(diff > 86400){
           devolution = 1;
        } else if(diff > 18000){
          devolution = 0.5;
        }else if(diff > 0) {
          devolution = 0;
        } 
      })
      .mergeMap((response) => {
        return pgrx.query('INSERT INTO refund(fk_payment, date, amount, percentage) values ((select id_payment from payment where fk_appoinment = $1) , now(), ((select cast (ammount AS INT ) from payment where fk_appoinment = $2), $3);', [id_appointment,id_appointment, devolution] ); 
      }) ```
haoliangyu commented 7 years ago

In your code, the response in the mergeMap operator is from the first database query. You have two ways to solve this:

jcaviedesc commented 7 years ago

Great, thank you very much.