0

Is there an elegant way to squash these two statements into one, similar to a plain SQL INSERT INTO t1 SELECT 'something', t2.x FROM t2 WHERE ...?:

    const commentIds: Array<Pick<SectionComment, 'id'>> =
      await this.articleRepository
        .createQueryBuilder('article')
        .select('sectionComment.id', 'id')
        .innerJoin('article.sectionComments', 'sectionComment')
        .where(
          'article.vaultId = :vaultId and article.valueId = :valueId and sectionComment.userId = :userId',
          {
            valueId,
            vaultId,
            userId,
          }
        )
        .getRawMany();

    if (commentIds.length) {
      await this.userSectionReadRepository
        .createQueryBuilder()
        .insert()
        .into(UserSectionRead)
        .values(
          commentIds.map((sectionComment) => ({
            userId,
            commentId: sectionComment.id,
          }))
        )
        .orIgnore()
        .execute();
    }

The problem is the values() method in InsertQueryBuilder does not accept a subquery function (qb: this) => string like the where() method does.

kosmičák
  • 1,043
  • 1
  • 17
  • 41

0 Answers0