Most of the time when you develop an application, you need pagination functionality. This is used if you have pagination, page slider, or infinite scroll components in your application.
const users = await dataSource
.getRepository(User)
.createQueryBuilder("user")
.leftJoinAndSelect("user.photos", "photo")
.take(10)
.getMany()
This will give you the first 10 users with their photos.
const users = await dataSource
.getRepository(User)
.createQueryBuilder("user")
.leftJoinAndSelect("user.photos", "photo")
.skip(10)
.getMany()
This will give you all except the first 10 users with their photos. You can combine those methods:
const users = await dataSource
.getRepository(User)
.createQueryBuilder("user")
.leftJoinAndSelect("user.photos", "photo")
.skip(5)
.take(10)
.getMany()
This will skip the first 5 users and take 10 users after them.
take
and skip
may look like we are using limit
and offset
, but they aren’t. limit
and offset
may not work as you expect once you have more complicated queries with joins or subqueries. Using take
and skip
will prevent those issues.