Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

98
Views
TypeORM QueryBuilder should return most recent inner joined record

I am trying to get most recent transaction by using this query inside my NestJs application

  public findUsersWithRecentTransactions(): Promise<UserEntity[]> {
    return this.createQueryBuilder('users')
      .innerJoinAndSelect('users.transactions', 'transactions')
      .where(
        '(users.userType = :userTypeParent AND users.registrationStatus = :registrationStatusParent)',
        {
          userTypeParent: UserType.Parent,
          registrationStatusParent: RegistrationStatus.onboarded,
        },
      )
      .orderBy('transactions.created_at', 'DESC').getMany();
  }

This returns an array of transactions sorted by their created_at date in descending order. However, I am trying to get the most recent transaction using the query itself so that I don't have to loop over the entities to get the transaction.

const usersWithTransactions = await this.userRepository.findAllUsersWithTransactions();
    for (const user of usersWithTransactions) {
      const mostRecentTransaction = user?.transactions[0];
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Hi you should try limit method.

  public findUsersWithRecentTransactions(): Promise<UserEntity[]> {
    return this.createQueryBuilder('users')
      .innerJoinAndSelect('users.transactions', 'transactions')
      .where(
        '(users.userType = :userTypeParent AND users.registrationStatus = :registrationStatusParent)',
        {
          userTypeParent: UserType.Parent,
          registrationStatusParent: RegistrationStatus.onboarded,
        },
      )
      .orderBy('transactions.created_at', 'DESC')
      .limit(1) // I include this.
      .getMany();
  }

Check this code above. I added '.limit(1)'.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!