skip to Main Content

Node.js / Nest.js /Typeorm /postgresql
my code is

i have table farm
| id | center_point type GeoJson | point type GeoPoints|

It can save to database, Does it have another way to update before commitTransaction
**

if Transaction fail does function updateFarmMapPoint are going to run
? (this function have parameter which is id from farm in Transaction )

**

<pre> 
async updateFarmMapPoint(farmId: number, centerPoint: GeoPointDto) {    
      try { const wkt = `POINT(${centerPoint.long} ${centerPoint.lat})`;       
      await this.repository.query(
        `UPDATE farms SET map_point = ST_GeomFromText($1,${Srid.SRID_4326})             
           WHERE id = $2;`,[wkt, farmId],);       
         return;     
          } catch (error) {
             throw new InternalServerErrorException(error);     
             }     
         }        

async createFarmsBo(data){     
      const createFarms = this.repository.create({ ...data});     
      const queryRunner = this.connection.createQueryRunner();     
      await queryRunner.connect();     
      await queryRunner.startTransaction();      
      const farms = awaitqueryRunner.manager.save(FarmsEntity,createFarms);      

      return await this.saveFarmsBo(farms,queryRunner);     
}      
 
async saveFarmsBo(farms: Partial<FarmsEntity>, runner?: QueryRunner){     const queryRunner = runner || this.connection.createQueryRunner();    
      if (!runner) {       
            await queryRunner.connect();       
            await queryRunner.startTransaction();     
      }     
   try {       
     await queryRunner.manager.save(FarmsEntity, farms),        
     await queryRunner.commitTransaction();        
     // What can go wrong this this line after commitTransaction?   
    
     await this.updateFarmMapPoint(farms.id, farms.centerPoint);  
                 
     return true;     
  } catch (error) {       
    await queryRunner.rollbackTransaction();       
    throw new InternalServerErrorException(error);     
  } finally {      
     await queryRunner.release();     
   }         
   }; 

<code>

2

Answers


  1. Chosen as BEST ANSWER

    i use trigger function instead of create function in code


  2. Transactions are used to make sure if anything that is done within the transactions (from start transactions to commit transactions) fails; all preceding successfully SQL operations are reverted/rolled back. If anything is done outside a transaction, they will no-doubt work like independent SQL statements but will not roll-back.

    Let’s say you want to update Table A and then Table B. If you do both inside a transaction, and if Table A passes and Table B fails, the action done on Table A will be reverted as if it never happened. So all actions in the transaction happen, or nothing happens. Without transaction, they will run without this roll back – Table A goes ahead, and Table B fails.

    So yes, the SQL statement will run, even after the transaction after commit, and do changes accordingly – pass or fail.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search