1
- import type { Collections , PageCollections , CollectionQueryBuilder , SurroundOptions } from '@nuxt/content'
1
+ import type { Collections , PageCollections , CollectionQueryBuilder , SurroundOptions , SQLOperator } from '@nuxt/content'
2
2
import { collectionQureyBuilder } from './internal/query'
3
3
import { measurePerformance } from './internal/performance'
4
4
import { generateNavigationTree } from './internal/navigation'
@@ -7,16 +7,21 @@ import { generateSearchSections } from './internal/search'
7
7
import { fetchQuery } from './internal/api'
8
8
import { tryUseNuxtApp } from '#imports'
9
9
10
+ interface ChainablePromise < T extends keyof PageCollections , R > extends Promise < R > {
11
+ where ( field : keyof PageCollections [ T ] | string , operator : SQLOperator , value ?: unknown ) : ChainablePromise < T , R >
12
+ order ( field : keyof PageCollections [ T ] , direction : 'ASC' | 'DESC' ) : ChainablePromise < T , R >
13
+ }
14
+
10
15
export const queryCollection = < T extends keyof Collections > ( collection : T ) : CollectionQueryBuilder < Collections [ T ] > => {
11
16
return collectionQureyBuilder < T > ( collection , ( collection , sql ) => executeContentQuery ( collection , sql ) )
12
17
}
13
18
14
- export async function queryCollectionNavigation < T extends keyof PageCollections > ( collection : T , fields ?: Array < keyof PageCollections [ T ] > ) {
15
- return generateNavigationTree ( queryCollection ( collection ) , fields )
19
+ export function queryCollectionNavigation < T extends keyof PageCollections > ( collection : T , fields ?: Array < keyof PageCollections [ T ] > ) {
20
+ return chainablePromise ( collection , qb => generateNavigationTree ( qb , fields ) )
16
21
}
17
22
18
- export async function queryCollectionItemSurroundings < T extends keyof PageCollections > ( collection : T , path : string , opts ?: SurroundOptions < keyof PageCollections [ T ] > ) {
19
- return generateItemSurround ( queryCollection ( collection ) , path , opts )
23
+ export function queryCollectionItemSurroundings < T extends keyof PageCollections > ( collection : T , path : string , opts ?: SurroundOptions < keyof PageCollections [ T ] > ) {
24
+ return chainablePromise ( collection , qb => generateItemSurround ( qb , path , opts ) )
20
25
}
21
26
22
27
export async function queryCollectionSearchSections ( collection : keyof Collections , opts ?: { ignoredTags : string [ ] } ) {
@@ -45,3 +50,32 @@ async function queryContentSqlClientWasm<T extends keyof Collections, Result = C
45
50
46
51
return rows as Result [ ]
47
52
}
53
+
54
+ function chainablePromise < T extends keyof PageCollections , Result > ( collection : T , fn : ( qb : CollectionQueryBuilder < PageCollections [ T ] > ) => Promise < Result > ) {
55
+ const queryBuilder = queryCollection ( collection )
56
+
57
+ const chainable : ChainablePromise < T , Result > = {
58
+ where ( field , operator , value ) {
59
+ queryBuilder . where ( String ( field ) , operator , value )
60
+ return chainable
61
+ } ,
62
+ order ( field , direction ) {
63
+ queryBuilder . order ( String ( field ) , direction )
64
+ return chainable
65
+ } ,
66
+ then ( onfulfilled , onrejected ) {
67
+ return fn ( queryBuilder ) . then ( onfulfilled , onrejected )
68
+ } ,
69
+ catch ( onrejected ) {
70
+ return this . then ( undefined , onrejected )
71
+ } ,
72
+ finally ( onfinally ) {
73
+ return this . then ( undefined , undefined ) . finally ( onfinally )
74
+ } ,
75
+ get [ Symbol . toStringTag ] ( ) {
76
+ return 'Promise'
77
+ } ,
78
+ }
79
+
80
+ return chainable
81
+ }
0 commit comments