@@ -376,13 +376,47 @@ type FieldFilter<
376376 AllowedKinds
377377 >
378378 : // primitive
379- PrimitiveFilter <
379+ AddFuzzyFilterIfSupported <
380+ Schema ,
380381 GetModelFieldType < Schema , Model , Field > ,
381- ModelFieldIsOptional < Schema , Model , Field > ,
382- WithAggregations ,
383- AllowedKinds
382+ AllowedKinds ,
383+ PrimitiveFilter <
384+ GetModelFieldType < Schema , Model , Field > ,
385+ ModelFieldIsOptional < Schema , Model , Field > ,
386+ WithAggregations ,
387+ AllowedKinds
388+ >
384389 > ;
385390
391+ /**
392+ * Conditionally augments a primitive filter with the `fuzzy` operator when:
393+ * 1. The field's type is `String`, AND
394+ * 2. The `Fuzzy` filter kind is allowed for this field, AND
395+ * 3. The schema's provider supports fuzzy search (postgres only).
396+ *
397+ * Returns `Base` unchanged when any condition fails — never `Base & {}`,
398+ * since intersecting with `{}` would strip `null`/`undefined` from `Base`.
399+ */
400+ type AddFuzzyFilterIfSupported <
401+ Schema extends SchemaDef ,
402+ FieldType extends string ,
403+ AllowedKinds extends FilterKind ,
404+ Base ,
405+ > = FieldType extends 'String'
406+ ? 'Fuzzy' extends AllowedKinds
407+ ? ProviderSupportsFuzzy < Schema > extends true
408+ ? Base & {
409+ /**
410+ * Performs a fuzzy search on the string field. Only available when
411+ * the schema's provider is `postgresql` (uses `pg_trgm`).
412+ * See {@link FuzzyFilterPayload} for the full options reference.
413+ */
414+ fuzzy ?: FuzzyFilterPayload ;
415+ }
416+ : Base
417+ : Base
418+ : Base ;
419+
386420type EnumFilter <
387421 Schema extends SchemaDef ,
388422 T extends GetEnums < Schema > ,
@@ -889,6 +923,92 @@ type TypedJsonFieldsFilter<
889923export type SortOrder = 'asc' | 'desc' ;
890924export type NullsOrder = 'first' | 'last' ;
891925
926+ type StringFields < Schema extends SchemaDef , Model extends GetModels < Schema > > = {
927+ [ Key in NonRelationFields < Schema , Model > ] : MapModelFieldType < Schema , Model , Key > extends string | null
928+ ? Key
929+ : never ;
930+ } [ NonRelationFields < Schema , Model > ] ;
931+
932+ /**
933+ * Payload for the `fuzzy` string filter operator. Performs a fuzzy search using
934+ * PostgreSQL `pg_trgm` (only available when the schema's provider is `postgresql`).
935+ * Not supported on MySQL or SQLite (throws `NotSupported` at runtime).
936+ *
937+ * Modes:
938+ * - `'simple'` (default): trigram similarity on the whole value (operator `%`,
939+ * function `similarity()`).
940+ * - `'word'`: word similarity — checks if the search term is approximately
941+ * contained as a word inside the value (operator `<%`,
942+ * function `word_similarity()`).
943+ * - `'strictWord'`: stricter variant of `'word'` (operator `<<%`,
944+ * function `strict_word_similarity()`).
945+ *
946+ * When `threshold` is provided the function form is used
947+ * (`similarity() > threshold`) instead of the operator form, so the
948+ * `pg_trgm.*_threshold` session settings are bypassed.
949+ *
950+ * `unaccent` is opt-in (defaults to `false`) — set it to `true` to make the
951+ * comparison accent-insensitive. Enabling it requires the `unaccent` extension
952+ * to be installed on the database.
953+ */
954+ export type FuzzyFilterPayload = {
955+ /**
956+ * Search term to match against (must be a non-empty string).
957+ */
958+ search : string ;
959+ /**
960+ * Matching mode. Defaults to `'simple'`.
961+ */
962+ mode ?: 'simple' | 'word' | 'strictWord' ;
963+ /**
964+ * Optional similarity threshold in `[0, 1]`. When provided, the function
965+ * form is used and matches require `similarity > threshold`.
966+ */
967+ threshold ?: number ;
968+ /**
969+ * Whether to apply `unaccent()` to both sides. Defaults to `false`.
970+ * Set to `true` to enable accent-insensitive matching (requires the
971+ * `unaccent` extension on PostgreSQL).
972+ */
973+ unaccent ?: boolean ;
974+ } ;
975+
976+ export type FuzzyRelevanceOrderBy < Schema extends SchemaDef , Model extends GetModels < Schema > > = {
977+ /**
978+ * Sorts by fuzzy search relevance using PostgreSQL `pg_trgm` similarity functions.
979+ * Not supported on MySQL or SQLite (throws `NotSupported` at runtime).
980+ * Cannot be combined with cursor-based pagination.
981+ *
982+ * The `_fuzzyRelevance` name is intentionally distinct from `_searchRelevance`
983+ * (reserved for future full-text-search relevance) so the two can coexist.
984+ */
985+ _fuzzyRelevance ?: {
986+ /**
987+ * String fields to compute relevance against (must be non-empty).
988+ *
989+ * When multiple fields are provided, the row's relevance score is the
990+ * greatest per-field similarity, i.e. `GREATEST(similarity(field1, search), similarity(field2, search), ...)`.
991+ */
992+ fields : [ StringFields < Schema , Model > , ...StringFields < Schema , Model > [ ] ] ;
993+ /**
994+ * The search term to compute relevance for.
995+ */
996+ search : string ;
997+ /**
998+ * Fuzzy matching mode used to compute relevance.
999+ */
1000+ mode ?: 'simple' | 'word' | 'strictWord' ;
1001+ /**
1002+ * Whether to remove accents before computing relevance.
1003+ */
1004+ unaccent ?: boolean ;
1005+ /**
1006+ * Sort direction.
1007+ */
1008+ sort : SortOrder ;
1009+ } ;
1010+ } ;
1011+
8921012export type OrderBy <
8931013 Schema extends SchemaDef ,
8941014 Model extends GetModels < Schema > ,
@@ -1239,7 +1359,10 @@ type SortAndTakeArgs<
12391359 /**
12401360 * Order by clauses
12411361 */
1242- orderBy ?: OrArray < OrderBy < Schema , Model , true , false > > ;
1362+ orderBy ?: OrArray <
1363+ OrderBy < Schema , Model , true , false > &
1364+ ( ProviderSupportsFuzzy < Schema > extends true ? FuzzyRelevanceOrderBy < Schema , Model > : { } )
1365+ > ;
12431366
12441367 /**
12451368 * Cursor for pagination
@@ -2528,6 +2651,8 @@ type ProviderSupportsDistinct<Schema extends SchemaDef> = Schema['provider']['ty
25282651 ? true
25292652 : false ;
25302653
2654+ type ProviderSupportsFuzzy < Schema extends SchemaDef > = Schema [ 'provider' ] [ 'type' ] extends 'postgresql' ? true : false ;
2655+
25312656/**
25322657 * Extracts extended query args for a specific operation.
25332658 */
0 commit comments