Using QueryBuilder

You can use QueryBuilder to build SQL queries of almost any complexity. For example, you can do this:

  1. let photos = await connection
  2. .getRepository(Photo)
  3. .createQueryBuilder("photo") // first argument is an alias. Alias is what you are selecting - photos. You must specify it.
  4. .innerJoinAndSelect("photo.metadata", "metadata")
  5. .leftJoinAndSelect("photo.albums", "album")
  6. .where("photo.isPublished = true")
  7. .andWhere("(photo.name = :photoName OR photo.name = :bearName)")
  8. .orderBy("photo.id", "DESC")
  9. .skip(5)
  10. .take(10)
  11. .setParameters({ photoName: "My", bearName: "Mishka" })
  12. .getMany();

This query selects all published photos with “My” or “Mishka” names.It will select results from position 5 (pagination offset),and will select only 10 results (pagination limit).The selection result will be ordered by id in descending order.The photo’s albums will be left-joined and their metadata will be inner joined.

You’ll use the query builder in your application a lot.Learn more about QueryBuilder here.