File tree Expand file tree Collapse file tree 3 files changed +38
-4
lines changed Expand file tree Collapse file tree 3 files changed +38
-4
lines changed Original file line number Diff line number Diff line change @@ -16,7 +16,7 @@ export const countDistinct: CountDistinct = async function countDistinct(
16
16
} )
17
17
. from ( this . tables [ tableName ] )
18
18
. where ( where )
19
- return Number ( countResult [ 0 ] ?. count )
19
+ return Number ( countResult ?. [ 0 ] ?. count ?? 0 )
20
20
}
21
21
22
22
let query : SQLiteSelect = db
@@ -39,5 +39,5 @@ export const countDistinct: CountDistinct = async function countDistinct(
39
39
// Instead, COUNT (GROUP BY id) can be used which is still slower than COUNT(*) but acceptable.
40
40
const countResult = await query
41
41
42
- return Number ( countResult [ 0 ] ?. count )
42
+ return Number ( countResult ?. [ 0 ] ?. count ?? 0 )
43
43
}
Original file line number Diff line number Diff line change @@ -16,7 +16,8 @@ export const countDistinct: CountDistinct = async function countDistinct(
16
16
} )
17
17
. from ( this . tables [ tableName ] )
18
18
. where ( where )
19
- return Number ( countResult [ 0 ] . count )
19
+
20
+ return Number ( countResult ?. [ 0 ] ?. count ?? 0 )
20
21
}
21
22
22
23
let query = db
@@ -39,5 +40,5 @@ export const countDistinct: CountDistinct = async function countDistinct(
39
40
// Instead, COUNT (GROUP BY id) can be used which is still slower than COUNT(*) but acceptable.
40
41
const countResult = await query
41
42
42
- return Number ( countResult [ 0 ] . count )
43
+ return Number ( countResult ?. [ 0 ] ? .count ?? 0 )
43
44
}
Original file line number Diff line number Diff line change @@ -2451,4 +2451,37 @@ describe('database', () => {
2451
2451
2452
2452
expect ( res . docs [ 0 ] . id ) . toBe ( customID . id )
2453
2453
} )
2454
+
2455
+ it ( 'should count with a query that contains subqueries' , async ( ) => {
2456
+ const category = await payload . create ( {
2457
+ collection : 'categories' ,
2458
+ data : { title : 'new-category' } ,
2459
+ } )
2460
+ const post = await payload . create ( {
2461
+ collection : 'posts' ,
2462
+ data : { title : 'new-post' , category : category . id } ,
2463
+ } )
2464
+
2465
+ const result_1 = await payload . count ( {
2466
+ collection : 'posts' ,
2467
+ where : {
2468
+ 'category.title' : {
2469
+ equals : 'new-category' ,
2470
+ } ,
2471
+ } ,
2472
+ } )
2473
+
2474
+ expect ( result_1 . totalDocs ) . toBe ( 1 )
2475
+
2476
+ const result_2 = await payload . count ( {
2477
+ collection : 'posts' ,
2478
+ where : {
2479
+ 'category.title' : {
2480
+ equals : 'non-existing-category' ,
2481
+ } ,
2482
+ } ,
2483
+ } )
2484
+
2485
+ expect ( result_2 . totalDocs ) . toBe ( 0 )
2486
+ } )
2454
2487
} )
You can’t perform that action at this time.
0 commit comments