Generic 'Where' type #10409
Replies: 2 comments
-
It's a primitive solution, for example it doesn't support nested keys (for arrays / relations etc), richtext / json querying and the |
Beta Was this translation helpful? Give feedback.
-
Ok, a little update. The documentation doesn't have many examples of how to use some operators (e.g. As for arrays, I'll think about how to approach this in the next few days. import { FieldPath, FieldPathValue } from 'react-hook-form';
export type AnyWhereField<TValue> = {
exists?: boolean;
equals?: TValue;
not_equals?: TValue;
};
export type ComparableWhereField<TValue extends number | string> = {
greater_than?: TValue;
greater_than_equal?: TValue;
less_than?: TValue;
less_than_equal?: TValue;
};
export type SearchableWhereField<TValue extends string> = {
like?: TValue;
contains?: TValue;
};
export type Point = [number, number];
export type PointValue = { type: 'Polygon'; coordinates: Point[] }; // TODO
export type SpatialableWhereField<TValue extends Point> = {
near?: [number, number, number | null, number | null]; // TODO
within?: PointValue;
intersect?: PointValue;
};
export type WhereField<TValue> = AnyWhereField<TValue> &
(TValue extends number | string ? ComparableWhereField<TValue> : {}) &
(TValue extends string ? SearchableWhereField<TValue> : {}) &
(TValue extends Point ? SpatialableWhereField<TValue> : {});
export type Where<T extends object> = {
[TPath in FieldPath<T>]?: WhereField<FieldPathValue<T, TPath>>;
} & { and?: Where<T>[]; or?: Where<T>[] }; |
Beta Was this translation helpful? Give feedback.
-
Proposal: Generic: 'Where' type
Currently, the
Where
type in Payload relies on a loosely typed structure (e.g.,JsonValue
), which results in limited editor autocompletion and a higher risk of runtime errors. I'd like to propose a generic version ofWhere
that leverages TypeScript's type system, providing stronger type safety and better DX.Example
Beta Was this translation helpful? Give feedback.
All reactions