Using QueryBuilder

Using QueryBuilder

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

let photos = await connection
    .getRepository(Photo)
    .createQueryBuilder("photo") // first argument is an alias. Alias is what you are selecting - photos. You must specify it.
    .innerJoinAndSelect("photo.metadata", "metadata")
    .leftJoinAndSelect("photo.albums", "album")
    .where("photo.isPublished = true")
    .andWhere("(photo.name = :photoName OR photo.name = :bearName)")
    .orderBy("photo.id", "DESC")
    .skip(5)
    .take(10)
    .setParameters({ photoName: "My", bearName: "Mishka" })
    .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.