Skip to content

Commit

Permalink
feat(types): provide augmentations for only and without (#1200)
Browse files Browse the repository at this point in the history
Co-authored-by: Ahad Birang <farnabaz@gmail.com>
Co-authored-by: Yaël Guilloux <yael.guilloux@gmail.com>
  • Loading branch information
3 people committed Jun 3, 2022
1 parent bda0cd0 commit 51a1620
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
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

0 comments on commit 51a1620

Please sign in to comment.