Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(types): provide augmentations for only and without #1200

Merged
merged 3 commits into from
Jun 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/runtime/query/query.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import type { DatabaseFetcher, QueryBuilder, QueryBuilderParams, SortOptions } from '../types'
import { ParsedContent } from '../types'
import { ensureArray } from './match/utils'

const arrayParams = ['sort', 'where', 'only', 'without']

export const createQuery = <T>(
export const createQuery = <T = ParsedContent>(
fetcher: DatabaseFetcher<T>,
queryParams?: Partial<QueryBuilderParams>
): QueryBuilder<T> => {
Expand All @@ -29,7 +30,7 @@ export const createQuery = <T>(

const query: QueryBuilder<T> = {
params: () => Object.freeze(params),
only: $set('only', ensureArray),
only: $set('only', ensureArray) as () => ReturnType<QueryBuilder<T>['only']>,
without: $set('without', ensureArray),
where: $set('where', (q: any) => [...ensureArray(params.where), q]),
sort: $set('sort', (sort: SortOptions) => [...ensureArray(params.sort), ...ensureArray(sort)]),
Expand Down
6 changes: 4 additions & 2 deletions src/runtime/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,12 +174,14 @@ export interface QueryBuilder<T = ParsedContentMeta> {
/**
* Select a subset of fields
*/
only(keys: string | string[]): QueryBuilder<T>
only<K extends keyof T | string>(keys: K): QueryBuilder<Pick<T, K>>
only<K extends (keyof T | string)[]>(keys: K): QueryBuilder<Pick<T, K[number]>>

/**
* Remove a subset of fields
*/
without(keys: string | string[]): QueryBuilder<T>
without<K extends keyof T | string>(keys: K): QueryBuilder<Omit<T, K>>
without<K extends (keyof T | string)[]>(keys: K): QueryBuilder<Omit<T, K[number]>>

/**
* Sort results
Expand Down