I am trying to limit the related data while finding with query builder, but I miss the concept.
Here is my code to get the employee orders:
import { getRepository, Repository } from "typeorm";
public async findEmployeeQuery(id : number) {
try {
let query = await getRepository(Employees)
.createQueryBuilder('employee')
.where('employee.id = :id' , {id})
.leftJoinAndSelect('employee.customers' , 'customers')
.getOne()
const user = query
return user
} catch (error) {
throw error
}
}
Now I want to limit the number of customers for each request - how can I do that?
I tried the limit and skip options but this only works with the employee, not with the joined data.
You have to make another query to limit customers:
import { getRepository, Repository } from "typeorm";
public async findEmployeeQuery(id : number) {
try {
let user = await getRepository(Employees)
.createQueryBuilder('employee')
.where('employee.id = :id' , {id});
.getOne()
user.customers = await getRepository(Customers)
.createQueryBuilder('customer')
.where('customer.employee= :id' , {user.id});
.limit(10) // here you set limitation you want
.getMany()
return user;
} catch (error) {
throw error
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With