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 JOINof the junction in the subquery). - The callback gets the subquery, a
SelectQueryBuilderof the related entity withaliasas its main alias, so typed methods work on it. The join condition is added after the callback's conditions, wrapped so that anorWhereof the callback can't escape it and awherecan't replace it. - Parameters of the subquery get unique names, as in whereNotExists: a
:statusinside doesn't read the value of an outer:status. aliasnames 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 readsUSERasuser): that throws aTypeError, as does a property that isn't a relation.- Inside
Bracketsit works on aSelectQueryBuilder, whoseBracketscallback is typed as aWhereExpressionBuilder: 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')