title: TypeORM AfterSave() triggers after creation but when queried, it returns NULL
body: <p>I have an Entity called <code>Trip</code>. The structure is:
  <a href="https://i.stack.imgur.com/JNOl5.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/JNOl5.png" alt="db structure"></a></p>
  
  <p>What I want is whenever a new trip is created, the <code>room</code> column should be populated with <code>${tripId}_${someRandomStringHere}</code>. So for example, I just created a new trip using this body:</p>
  
  <p><a href="https://i.stack.imgur.com/UadiM.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/UadiM.png" alt="trip_body"></a></p>
  
  <p>The response should be:
  <a href="https://i.stack.imgur.com/yFxD4.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/yFxD4.png" alt="tr]ip response"></a></p>
  
  <p>The newly created <code>trip</code> has the <code>id</code> of <code>15</code>. So, the response has the <code>room</code> valued at <code>15_4gupvdo0ea408c25ia0qsbh</code> because again: <code>${tripId}_${someRandomStringHere}</code>.</p>
  
  <p>This is working as <strong>expected</strong> whenever I <code>POST</code> the request and create the trip. <strong>BUT</strong> whenever I query all the trips created, the <code>room</code> property of each trip objects shows <code>null</code>! </p>
  
  <p><code>Look at the /api/trips:</code></p>
  
  <p><a href="https://i.stack.imgur.com/afSp4.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/afSp4.png" alt="trips array"></a></p>
  
  <p><code>room</code> property is <strong>NULL</strong>. So what the heck I dont understand what is happening.</p>
  
  <p>My <code>Trip Entity</code> code is:</p>
  
  <pre><code>import { PrimaryGeneratedColumn, Column, CreateDateColumn, 
  UpdateDateColumn, Entity, Unique, ManyToOne, AfterInsert, JoinColumn, getConnection } from 'typeorm'
  import { DriverEntity } from 'src/driver/driver.entity';
  
  @Entity('trips')
  export class TripEntity {
    @PrimaryGeneratedColumn()
    id: number
  
    @Column()
    destination: string
  
    @Column('decimal')
    destination_lat: number
  
    @Column('decimal')
    destination_long: number
  
    @Column()
    maxPassenger: number
  
    @Column()
    totalPassenger: number
  
    @Column({ nullable: true })
    room: string
  
    @CreateDateColumn()
    created_at: Date
  
    @UpdateDateColumn()
    updated_at: Date
  // ---------&gt;HERE: The after insert 
    @AfterInsert()
    async createSocketRoom(): Promise&lt;void&gt; {
      const randomString = Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15)
      this.room =  `${this.id}_${randomString}`
    }
  
    // Trip belongs to driver
    // Adds driver_id to trips table
    @ManyToOne(type =&gt; DriverEntity, driver =&gt; driver.trips)
    @JoinColumn({ name: 'driver_id' })
    driver: DriverEntity
  }
  </code></pre>
  
  <p>My <code>Trip Service Code</code> is:</p>
  
  <pre><code>async create(data: CreateTripDTO) {
      const { driver_id } = data
      const driver = await this.driverRepository.findOne({ where: { id: driver_id } })
      const trip = await this.tripRepository.create(data)
      trip.driver = driver
  
      await this.tripRepository.save(trip)
      return trip
    }
  </code></pre>
  
  <p>I dont think I need to include the <code>Trip Controller</code> code but anyway..
  I don't know why it is happening because I have my User Entity with @BeforeUpdate and works fine... </p>
  
  <p><strong>After reading alot of similar github issues, watched youtube tutorials [Hi Ben Awad! :D], I found a somewhat fix.. by using Subscribers</strong></p>
  
  <p>Actually, I don't know what is the difference of the Listener/Subscriber. Maybe I am doing the wrong usage. Can someone enlighten me please? For example the difference of AfterSave of Entity Listener vs AfterSave of Entity Subscriber. When/Best case  to use? something like that. Anyway back with the <code>"fix..."</code></p>
  
  <p>I created a <code>Trip Subscriber</code>:</p>
  
  <pre><code>import { EventSubscriber, EntitySubscriberInterface, InsertEvent } from "typeorm";
  import { TripEntity } from "src/trip/trip.entity";
  
  @EventSubscriber()
  export class TripSubsriber implements EntitySubscriberInterface&lt;TripEntity&gt; {
      // Denotes that this subscriber only listens to Trip Entity
      listenTo() {
        return TripEntity
      }
  
      // Called after entity insertion
      async afterInsert(event: InsertEvent&lt;any&gt;) {
          console.log(`AFTER ENTITY INSERTED: `, event.entity);
          const randomString = Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15)
          // query the trip with given event.entity
          const trip = await event.manager.getRepository(TripEntity).findOne(event.entity.id)
          // populate the room with desired format
          trip.room = `${trip.id}_${randomString}`
          // save it!
          await event.manager.getRepository(TripEntity).save(trip)
      }
  
  }
  </code></pre>
  
  <p>At first it is not working but after digging for hours again, I need to add a <code>subscriber</code> property with the value of the path of my subscribers at the <code>ormconfig.json</code> for it to work! 
  e.g: <code>"subscribers": [
      "src/subscriber/*.ts"
    ]</code></p>
  
  <p>Again, the <code>Trip Subscriber</code> code seems spaghetti to me because I already have the <code>event.entity</code> object but I do not know how to update it without the need of querying and updating it using <code>event.manager.getRepository()</code>. Please can someone fix this code for me? the proper way of doing it?</p>
  
  <p><strong>NOW, It is working!</strong>
  the request body:
  <a href="https://i.stack.imgur.com/nGlHy.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/nGlHy.png" alt="the request body"></a></p>
  
  <p>the /api/trips res:
  <a href="https://i.stack.imgur.com/xuVYB.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/xuVYB.png" alt="res"></a></p>
  
  <p>My questions are:</p>
  
  <ol>
  <li>Why whenever I use that method methoud subscriber, it is not working. Is it not the proper way to do it? The why is it in the docs? Or for other use case?</li>
  <li>Do I really have to use subscriber for it to achieve? Thats so many steps. </li>
  </ol>
  
  <p>I came from Rails. So having to create files/subscribers just to do it somewhat tiring. Unlike ActiveRecord's <code>after_save</code> callback it is very easy..</p>
  
  <p>PS. I'm new to nest-js and typeorm</p>