Skip to main content

whereRelationExistsTyped / whereRelationNotExistsTyped

Description​

EXISTS / NOT EXISTS over a relation of an entity, with the join condition taken from the relation's metadata. A hand-written correlated subquery repeats the join ("image"."userId" = "user"."id") and breaks silently when a column is renamed; here the relation is picked by a typed callback.

// users with an image that is not hidden
userRepository
.createQueryBuilder('user')
.andWhereRelationExistsTyped(
user => user.images,
'image',
images => images.andWhereTyped(image => image.hidden, '=', false)
)
WHERE EXISTS (
SELECT 1 FROM "images" "image"
WHERE "image"."hidden" = :andWhere_x1Fq2 AND "image"."userId" = "user"."id"
)
  • Relations: one-to-many and many-to-one, one-to-one from either side, and many-to-many from either side, through its junction table (INNER JOIN of the junction in the subquery).
  • The callback gets the subquery, a SelectQueryBuilder of the related entity with alias as its main alias, so typed methods work on it. The join condition is added after the callback's conditions, wrapped so that an orWhere of the callback can't escape it and a where can't replace it.
  • Parameters of the subquery get unique names, as in whereNotExists: a :status inside doesn't read the value of an outer :status.
  • alias names the related entity in the subquery. It must not be an alias of the outer query, which the join condition refers to, also in another letter case (SQLite reads USER as user): that throws a TypeError, as does a property that isn't a relation.
  • Inside Brackets it works on a SelectQueryBuilder, whose Brackets callback is typed as a WhereExpressionBuilder: cast it, (where as SelectQueryBuilder<User>).whereRelationExistsTyped(…).
  • Lazy relations (Promise<Image[]>) aren't typed by the callback: TypeScript rejects them.

Signature​

whereRelationExistsTyped(relation: (entity) => Related, alias: string, where?: (subQuery: SelectQueryBuilder<Related>) => unknown): this
andWhereRelationExistsTyped(relation, alias, where?): this
orWhereRelationExistsTyped(relation, alias, where?): this

whereRelationNotExistsTyped(relation, alias, where?): this
andWhereRelationNotExistsTyped(relation, alias, where?): this
orWhereRelationNotExistsTyped(relation, alias, where?): this

whereRelationExistsTyped / whereRelationNotExistsTyped replace the WHERE conditions set before, like where. On SelectQueryBuilder.

Usage​

// a relation of a joined entity: name the root type, as with other typed methods, and the related
// entity, which the callback can't give past `alias.relation`
type Joins = { user: UserEntity; community: CommunityEntity };

query
.leftJoin('user.community', 'community')
.andWhereRelationExistsTyped<InterestEntity, Joins>(joins => joins.community.interests, 'interest')

// or with the root set once by typed<Joins>(): only the related entity is named
query
.leftJoin('user.community', 'community')
.typed<Joins>()
.andWhereRelationExistsTyped<InterestEntity>(joins => joins.community.interests, 'interest')
// members who are in no group: NOT EXISTS through the junction table
memberRepository
.createQueryBuilder('member')
.andWhereRelationNotExistsTyped(member => member.groups, 'grp')