Select and Filter
Select target model and relations
// Simply select shop, each with array of products
let shops = await Shop.find({
selectProps: ['products']
})
1
2
3
4
2
3
4
findOptions
Select target model and relations by // find shops with related products which are only in color 'red'
let shops = await Shop.find({
select: {
// select the computed property 'products'
products: {
where: {
color: 'red'
}
}
},
where: {
//...
}
})
1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
Select target model with relations match criteria
// find all products which belongs to a Shop
let products = await Product.find({
where: ({root}) => root.shop().exists()
})
// find all products which belongs to Shop with location 'Hong Kong'
let products = await Product.find({
where: ({root}) => root.shop({
where: {
location: 'Hong Kong'
}
}).exists()
})
// another approach (if the 'hasMany' relationship is not defined in Model Schema)
await Product.find({
where: ({root, Exists}) => Exists( Shop.dataset({
where: {
shopId: root.id,
location: 'Hong Kong'
}
}))
})
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
Select target model with certain number of related records
Besides, the funtion call of ComputeProperty
returns a Scalar
that can be transformed into subquery like "SELECT count(*) FROM ..."
// find all shops which has more than 5 products
let shops = await Shop.find({
where: ({root}) => root.products().count().greaterThan(5)
})
// another approach (if the 'hasMany' relationship is not defined in Model Schema)
let shops = await Shop.find({
where: ({root}) => Product.dataset({
where: {
shopId: root.id
}
}).toDScalar().count().greaterThan(5)
})
1
2
3
4
5
6
7
8
9
10
11
12
13
2
3
4
5
6
7
8
9
10
11
12
13