Skip to content

Commit 1cc9ca9

Browse files
committed
docs: document server utils
1 parent 902774f commit 1cc9ca9

File tree

5 files changed

+121
-4
lines changed

5 files changed

+121
-4
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
icon: i-lucide-square-function
2-
title: Vue Utils
2+
title: Query Utils

docs/content/docs/4.utils/1.query-collection.md

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ description: The queryCollection composable provides methods for querying and
88

99
Use the auto-imported `queryCollection` to find contents inside a collection. Here we assume that you have defined `docs` collection inside `content.config.ts`.
1010

11-
::note
11+
1212
If you have not defined any collection, check [How to define a collection](/docs/collections/define#defining-collections).
13-
::
13+
1414

1515
```vue [pages/[...slug\\].vue]
1616
<script>
@@ -21,6 +21,10 @@ const { data: page } = await useAsyncData(route.path, () => {
2121
</script>
2222
```
2323

24+
::tip
25+
The `queryCollection` utility is available in both Vue and Nitro. Checkout [Server Usage](#server-usage) for more details on how to use it on the server side.
26+
::
27+
2428
## API
2529

2630
### Type
@@ -238,3 +242,26 @@ const { data: docs } = await useAsyncData('documents-list', () => {
238242
</NuxtLink>
239243
</template>
240244
```
245+
246+
## Server Usage
247+
248+
Nuxt Content provides a similar utility to query collections on the server side. The only difference is that you need to pass `event` as the first argument to the `queryCollection` function.
249+
250+
```ts [server/api/[slug\\].ts]
251+
export default eventHandler(async (event) => {
252+
const { slug } = getRouterParams(event)
253+
const page = await queryCollection(event, 'docs').path(slug).first()
254+
return page
255+
})
256+
```
257+
258+
:::note
259+
Make sure to create `server/tsconfig.json` file with the following content to avoid type error.
260+
261+
```json
262+
{
263+
"extends": "../.nuxt/tsconfig.server.json"
264+
}
265+
```
266+
:::
267+

docs/content/docs/4.utils/2.query-collection-navigation.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ const { data } = await useAsyncData('navigation', () => {
3636
</script>
3737
```
3838

39+
::tip
40+
The `queryCollectionNavigation` utility is available in both Vue and Nitro. Checkout [Server Usage](#server-usage) for more details on how to use it on the server side.
41+
::
42+
3943
## API
4044

4145
### `queryCollectionNavigation(collection: CollectionName, extraField: keyof Collection)`
@@ -101,3 +105,26 @@ const { data } = await useAsyncData('navigation', () => {
101105
</nav>
102106
</template>
103107
```
108+
109+
110+
## Server Usage
111+
112+
Nuxt Content provides a similar utility to query collections on the server side. The only difference is that you need to pass `event` as the first argument to the `queryCollectionNavigation` function.
113+
114+
```ts [server/api/navigation.ts]
115+
export default eventHandler(async (event) => {
116+
const navigation = await queryCollectionNavigation(event, 'docs')
117+
return navigation
118+
})
119+
```
120+
121+
:::note
122+
Make sure to create `server/tsconfig.json` file with the following content to avoid type error.
123+
124+
```json
125+
{
126+
"extends": "../.nuxt/tsconfig.server.json"
127+
}
128+
```
129+
:::
130+

docs/content/docs/4.utils/3.query-collection-item-surroundings.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ const { data } = await useAsyncData('surround', () => {
3737
</script>
3838
```
3939

40+
::tip
41+
The `queryCollectionItemSurroundings` utility is available in both Vue and Nitro. Checkout [Server Usage](#server-usage) for more details on how to use it on the server side.
42+
::
43+
44+
4045
## API
4146

4247
### `queryCollectionItemSurroundings(collection: CollectionName, path: string, opts?: SurroundOptions)`
@@ -102,3 +107,27 @@ const { data } = await useAsyncData('surround', () => {
102107
})
103108
</script>
104109
```
110+
111+
112+
113+
## Server Usage
114+
115+
Nuxt Content provides a similar utility to query collections on the server side. The only difference is that you need to pass `event` as the first argument to the `queryCollectionItemSurroundings` function.
116+
117+
```ts [server/api/surroundings.ts]
118+
export default eventHandler(async (event) => {
119+
const surroundings = await queryCollectionItemSurroundings(event, 'docs', '/foo')
120+
return surroundings
121+
})
122+
```
123+
124+
:::note
125+
Make sure to create `server/tsconfig.json` file with the following content to avoid type error.
126+
127+
```json
128+
{
129+
"extends": "../.nuxt/tsconfig.server.json"
130+
}
131+
```
132+
:::
133+

docs/content/docs/4.utils/4.query-collection-search-sections.md

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,14 @@ description: The queryCollectionSearchSections composable generates searchable
77
## Type
88

99
```ts
10-
function queryCollectionSearchSections(collection: keyof Collections, opts?: { ignoredTags: string[] }): Promise<Section[]>
10+
function queryCollectionSearchSections(collection: keyof Collections, opts?: { ignoredTags: string[] }): ChainablePromise<T, Section[]>
11+
12+
interface ChainablePromise<T extends keyof PageCollections, R> extends Promise<R> {
13+
where(field: keyof PageCollections[T] | string, operator: SQLOperator, value?: unknown): ChainablePromise<T, R>
14+
andWhere(groupFactory: QueryGroupFunction<PageCollections[T]>): ChainablePromise<T, R>
15+
orWhere(groupFactory: QueryGroupFunction<PageCollections[T]>): ChainablePromise<T, R>
16+
order(field: keyof PageCollections[T], direction: 'ASC' | 'DESC'): ChainablePromise<T, R>
17+
}
1118
```
1219

1320
## Usage
@@ -22,6 +29,11 @@ const { data: sections } = await useAsyncData('search-sections', () => {
2229
</script>
2330
```
2431

32+
::tip
33+
The `queryCollectionSearchSections` utility is available in both Vue and Nitro. Checkout [Server Usage](#server-usage) for more details on how to use it on the server side.
34+
::
35+
36+
2537
## API
2638

2739
### `queryCollectionSearchSections(collection: CollectionName, options?: SearchSectionsOptions)`
@@ -52,3 +64,25 @@ const { data: surround } = await useAsyncData('foo-surround', () => {
5264
})
5365
</script>
5466
```
67+
68+
## Server Usage
69+
70+
Nuxt Content provides a similar utility to query collections on the server side. The only difference is that you need to pass `event` as the first argument to the `queryCollectionSearchSections` function.
71+
72+
```ts [server/api/search-sections.ts]
73+
export default eventHandler(async (event) => {
74+
const sections = await queryCollectionSearchSections(event, 'docs')
75+
return sections
76+
})
77+
```
78+
79+
:::note
80+
Make sure to create `server/tsconfig.json` file with the following content to avoid type error.
81+
82+
```json
83+
{
84+
"extends": "../.nuxt/tsconfig.server.json"
85+
}
86+
```
87+
:::
88+

0 commit comments

Comments
 (0)