title: NestJs: How to access database in entity listeners?
body: <p>I've got a <code>@BeforeInsert()</code> listener in my <code>Post</code> entity. The listener is supposed to create a unique slug for the <code>slug</code> column before insertion.</p>
  
  <p>For example:</p>
  
  <pre><code>export class Post extends BaseEntity {
      @PrimaryGeneratedColumn()
      id: number
  
      @Column()
      title: string
  
      @Column()
      slug: string
  
      @BeforeInsert()
      private updateSlug() {
          this.slug = slugify(this.title)
          // I will need to access the database to check whether the same slug has existsed already or not. 
          // How do I access the database in this listener method?
      }
  }
  </code></pre>
  
  <p>Since the <code>slug</code> column is supposed to be unique, I will have to check in the database to know whether the same slug already exists. If the slug already exists, I will then need to append a number behind the slug, like <code>hello-word-1</code>.</p>
  
  <p>However, to do so, I will need to access the <code>Post</code> repository of the entity in the <code>Post</code> entity class before I can access the database. But I don't see how I can inject the repository into my <code>Post</code> entity class to access the database for this purpose.</p>
  
  <p>How should I approach this problem?</p>