1- import type { Collections , PageCollections , CollectionQueryBuilder , SurroundOptions } from '@nuxt/content'
1+ import type { Collections , PageCollections , CollectionQueryBuilder , SurroundOptions , SQLOperator } from '@nuxt/content'
22import { collectionQureyBuilder } from './internal/query'
33import { measurePerformance } from './internal/performance'
44import { generateNavigationTree } from './internal/navigation'
@@ -7,16 +7,21 @@ import { generateSearchSections } from './internal/search'
77import { fetchQuery } from './internal/api'
88import { tryUseNuxtApp } from '#imports'
99
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+
1015export const queryCollection = < T extends keyof Collections > ( collection : T ) : CollectionQueryBuilder < Collections [ T ] > => {
1116 return collectionQureyBuilder < T > ( collection , ( collection , sql ) => executeContentQuery ( collection , sql ) )
1217}
1318
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 ) )
1621}
1722
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 ) )
2025}
2126
2227export async function queryCollectionSearchSections ( collection : keyof Collections , opts ?: { ignoredTags : string [ ] } ) {
@@ -45,3 +50,32 @@ async function queryContentSqlClientWasm<T extends keyof Collections, Result = C
4550
4651 return rows as Result [ ]
4752}
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