|
| 1 | +import { GroqBuilder } from "../groq-builder"; |
| 2 | +import { Expressions } from "../types/groq-expressions"; |
| 3 | +import { ConfigCreateNestedScope } from "../types/query-config"; |
| 4 | +import { ResultItem } from "../types/result-types"; |
| 5 | + |
| 6 | +declare module "../groq-builder" { |
| 7 | + export interface GroqBuilder<TResult, TQueryConfig> { |
| 8 | + /** |
| 9 | + * This is an alias for the `filterRaw` method; please use that instead. |
| 10 | + * |
| 11 | + * Filters based on any raw filter expression. |
| 12 | + * This method is NOT type-checked, but does provide suggestions. |
| 13 | + * |
| 14 | + * @deprecated Please use `filterRaw` instead! |
| 15 | + * |
| 16 | + * @example |
| 17 | + * q.star.filterRaw("count(items[]) > 5") |
| 18 | + * |
| 19 | + * @param filterExpression - Any valid GROQ expression that can be used for filtering |
| 20 | + */ |
| 21 | + filter( |
| 22 | + ...filterExpression: NonEmptyArray< |
| 23 | + Expressions.AnyConditional<ResultItem.Infer<TResult>, TQueryConfig> |
| 24 | + > |
| 25 | + ): GroqBuilder<TResult, TQueryConfig>; |
| 26 | + |
| 27 | + /** |
| 28 | + * Filters based on any raw filter expression(s). |
| 29 | + * This method is NOT type-checked, but does provide suggestions. |
| 30 | + * |
| 31 | + * Multiple expressions will be combined with "OR" logic. |
| 32 | + * |
| 33 | + * @example |
| 34 | + * q.star.filterRaw("count(items[]) > 5") |
| 35 | + * |
| 36 | + * @param filterExpression - Any valid GROQ expression that can be used for filtering |
| 37 | + */ |
| 38 | + filterRaw( |
| 39 | + ...filterExpression: NonEmptyArray< |
| 40 | + Expressions.AnyConditional< |
| 41 | + ResultItem.Infer<TResult>, |
| 42 | + ConfigCreateNestedScope<TQueryConfig, ResultItem.Infer<TResult>> |
| 43 | + > |
| 44 | + > |
| 45 | + ): GroqBuilder<TResult, TQueryConfig>; |
| 46 | + |
| 47 | + /** |
| 48 | + * Filters the results based on a simple, |
| 49 | + * strongly-typed equality expression. |
| 50 | + * |
| 51 | + * Multiple calls will result in "AND" logic. |
| 52 | + * Multiple arguments will be combined with "OR" logic. |
| 53 | + * |
| 54 | + * This method is strongly-typed, but only supports common expressions. |
| 55 | + * If you'd like to filter based off more complex logic, use `filterRaw` instead. |
| 56 | + * |
| 57 | + * @example |
| 58 | + * q.star.filterByType("product") |
| 59 | + * .filterBy("image.url != null") |
| 60 | + * .filterBy('category == "food"') |
| 61 | + * .filterBy("price < 50", "msrp < 50") |
| 62 | + * .filterBy("references(^._id)") |
| 63 | + */ |
| 64 | + filterBy( |
| 65 | + ...filterExpression: NonEmptyArray< |
| 66 | + Expressions.Conditional< |
| 67 | + ResultItem.Infer<TResult>, |
| 68 | + ConfigCreateNestedScope<TQueryConfig, ResultItem.Infer<TResult>> |
| 69 | + > |
| 70 | + > |
| 71 | + ): GroqBuilder<TResult, TQueryConfig>; |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +type NonEmptyArray<T> = [T, ...T[]]; |
| 76 | + |
| 77 | +GroqBuilder.implement({ |
| 78 | + filter(this: GroqBuilder, ...filterExpressions) { |
| 79 | + return this.filterRaw(...filterExpressions); |
| 80 | + }, |
| 81 | + filterRaw(this: GroqBuilder, ...filterExpressions) { |
| 82 | + const needsWrap = this.query.endsWith("->"); |
| 83 | + const self = needsWrap ? this.extend({ query: `(${this.query})` }) : this; |
| 84 | + return self.chain(`[${filterExpressions.join(" || ")}]`, "passthrough"); |
| 85 | + }, |
| 86 | + filterBy(this: GroqBuilder, ...filterExpressions) { |
| 87 | + return this.filterRaw(...filterExpressions); |
| 88 | + }, |
| 89 | +}); |
0 commit comments