There are two types of results you can get using select query builder: entities and raw results. Most of the time, you need to select real entities from your database, for example, users. For this purpose, you use getOne
and getMany
. However, sometimes you need to select specific data, like the sum of all user photos. Such data is not an entity, it’s called raw data. To get raw data, you use getRawOne
and getRawMany
. Examples:
const { sum } = await dataSource
.getRepository(User)
.createQueryBuilder("user")
.select("SUM(user.photosCount)", "sum")
.where("user.id = :id", { id: 1 })
.getRawOne()
const photosSums = await dataSource
.getRepository(User)
.createQueryBuilder("user")
.select("user.id")
.addSelect("SUM(user.photosCount)", "sum")
.groupBy("user.id")
.getRawMany()
// result will be like this: [{ id: 1, sum: 25 }, { id: 2, sum: 13 }, ...]