Skip to content

Commit

Permalink
simpl;ify where and orderby type
Browse files Browse the repository at this point in the history
  • Loading branch information
tylim88 committed Apr 6, 2024
1 parent 871a98e commit dab4d50
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 28 deletions.
8 changes: 4 additions & 4 deletions src/types/queryConstraints/query.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { MetaType, GetAllCompareKeys } from '../metaTypeCreator'
import { MetaType } from '../metaTypeCreator'
import { WhereFilterOp } from '../alias'
import { CursorType, CursorConstraint } from './cursor'
import { WhereConstraint } from './where'
Expand All @@ -9,14 +9,14 @@ import { QueryCompositeFilterConstraint } from './composite'
type QueryNonFilterConstraints<T extends MetaType> =

Check warning on line 9 in src/types/queryConstraints/query.ts

View workflow job for this annotation

GitHub Actions / build_publish

'T' is defined but never used. Allowed unused vars must match /^_/u
| LimitConstraint<'limit' | 'limitToLast'>
| CursorConstraint<CursorType, unknown[]>
| OrderByConstraint<GetAllCompareKeys<T>>
| OrderByConstraint<string>

export type QueryConstraints<T extends MetaType> =
| WhereConstraint<T, GetAllCompareKeys<T>, WhereFilterOp, unknown>
| WhereConstraint<string, WhereFilterOp, unknown>
| QueryNonFilterConstraints<T>

export type QueryFilterConstraints<T extends MetaType> =
| WhereConstraint<T, GetAllCompareKeys<T>, WhereFilterOp, unknown>
| WhereConstraint<string, WhereFilterOp, unknown>
| QueryCompositeFilterConstraint<T, 'and' | 'or', QueryFilterConstraints<T>[]>

export type QueryAllConstraints<T extends MetaType> =
Expand Down
5 changes: 2 additions & 3 deletions src/types/queryConstraints/where.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ import { WhereFilterOp, QueryFieldFilterConstraint } from '../alias'
import { __name__ } from '../fieldPath'

export type WhereConstraint<
T extends MetaType,
FieldPath extends GetAllCompareKeys<T> | __name__,
FieldPath extends string,
OpStr extends WhereFilterOp,
Value
> = {
Expand All @@ -24,4 +23,4 @@ export type Where = <
fieldPath: FieldPath,
opStr: OpStr,
value: Value
) => WhereConstraint<T, FieldPath, OpStr, Value>
) => WhereConstraint<FieldPath, OpStr, Value>
4 changes: 2 additions & 2 deletions src/types/queryConstraintsLimitations/composite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export type ValidateTopLevelQueryCompositeFilterPartOne<
T extends MetaType,
AllQQCs extends readonly QueryAllConstraints<T>[]
> = AllQQCs extends (infer P)[]
? Extract<P, WhereConstraint<T, any, WhereFilterOp, unknown>> extends never
? Extract<P, WhereConstraint<string, WhereFilterOp, unknown>> extends never
? true
: GetAllQueryFilterCompositeConstraint<T, AllQQCs, never> extends never
? true
Expand Down Expand Up @@ -129,7 +129,7 @@ export type QueryFilterConstraintLimitation<
| OrderByConstraint<string>
| CursorConstraint<CursorType, unknown[]>
? ErrorOrAndInvalidConstraints
: Head extends WhereConstraint<T, any, WhereFilterOp, unknown>
: Head extends WhereConstraint<string, WhereFilterOp, unknown>
? Head['opStr'] extends NotIn
? 'or' extends ParentConstraint['type']
? ParentConstraint['constraints']['length'] extends 1
Expand Down
5 changes: 2 additions & 3 deletions src/types/queryConstraintsLimitations/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ export type ValidateOrderByAndInequalityWhere<
T extends MetaType,
AllQCs extends readonly QueryConstraints<T>[]
> = GetFirstInequalityWhere<T, AllQCs> extends infer W extends WhereConstraint<
T,
any,
string,
InequalityOpStr,
unknown
>
Expand Down Expand Up @@ -62,7 +61,7 @@ export type QueryConstraintLimitation<
? Head
: Head extends LimitConstraint<'limitToLast'>
? LimitToLastConstraintLimitation<T, Head, AllQCs>
: Head extends WhereConstraint<T, any, WhereFilterOp, unknown>
: Head extends WhereConstraint<string, WhereFilterOp, unknown>
? ValidateWhereArrayContainsArrayContainsAny<
T,
Head,
Expand Down
28 changes: 12 additions & 16 deletions src/types/queryConstraintsLimitations/where.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import { DeepValue } from '../objectFlatten'
// You can't combine 'not-in' with 'or', 'in', 'array-contains-any', or '!=' in the same query.
type ValidateWhereNotIn<
T extends MetaType,
U extends WhereConstraint<T, any, WhereFilterOp, unknown>,
U extends WhereConstraint<string, WhereFilterOp, unknown>,
PreviousQCs extends readonly QueryConstraints<T>[]
> = U['opStr'] extends NotIn
? Extract<
Expand All @@ -51,7 +51,7 @@ type ValidateWhereNotIn<
// You cannot use more than one '!=' filter. (undocumented)
type ValidateWhereNotEqual<
T extends MetaType,
U extends WhereConstraint<T, any, WhereFilterOp, unknown>,
U extends WhereConstraint<string, WhereFilterOp, unknown>,
PreviousQCs extends readonly QueryConstraints<T>[]
> = U['opStr'] extends NotEqual
? Extract<
Expand All @@ -65,7 +65,7 @@ type ValidateWhereNotEqual<
// You can use at most one array-contains or array-contains-any clause per query. You can't combine array-contains with array-contains-any.
export type ValidateWhereArrayContainsArrayContainsAny<
T extends MetaType,
U extends WhereConstraint<T, any, WhereFilterOp, unknown>,
U extends WhereConstraint<string, WhereFilterOp, unknown>,
PreviousQCs extends readonly QueryConstraints<T>[]
> = U['opStr'] extends ArrayContains
? Extract<
Expand All @@ -86,20 +86,20 @@ export type ValidateWhereArrayContainsArrayContainsAny<
// In a compound query, range (<, <=, >, >=) and not equals (!=, not-in) comparisons must all filter on the same field.
type ValidateWhereInequalityOpStrSameField<
T extends MetaType,
U extends WhereConstraint<T, any, WhereFilterOp, unknown>,
U extends WhereConstraint<string, WhereFilterOp, unknown>,
PreviousQCs extends readonly QueryConstraints<T>[]
> = U['opStr'] extends InequalityOpStr
? Extract<
GetAllWhereConstraint<T, PreviousQCs, never>,
WhereConstraint<T, any, InequalityOpStr, unknown>
WhereConstraint<string, InequalityOpStr, unknown>
> extends never
? true
: Exclude<
Extract<
GetAllWhereConstraint<T, PreviousQCs, never>,
WhereConstraint<T, any, InequalityOpStr, unknown>
WhereConstraint<string, InequalityOpStr, unknown>
>,
WhereConstraint<T, U['fieldPath'], InequalityOpStr, unknown>
WhereConstraint<U['fieldPath'], InequalityOpStr, unknown>
> extends never
? true
: ErrorWhereInequalityOpStrSameField
Expand All @@ -109,23 +109,23 @@ export type GetFirstInequalityWhere<
T extends MetaType,
QCs extends readonly QueryConstraints<T>[]
> = QCs extends [infer H, ...infer Rest extends readonly QueryConstraints<T>[]]
? H extends WhereConstraint<T, any, InequalityOpStr, unknown>
? H extends WhereConstraint<string, InequalityOpStr, unknown>
? H
: GetFirstInequalityWhere<T, Rest>
: true // not found, no check needed

export type GetAllWhereConstraint<
T extends MetaType,
AllQCs extends readonly QueryConstraints<T>[],
WhereConstraintsAcc extends WhereConstraint<T, any, WhereFilterOp, unknown>
WhereConstraintsAcc extends WhereConstraint<string, WhereFilterOp, unknown>
> = AllQCs extends [infer H, ...infer R]
? R extends readonly QueryConstraints<T>[]
?
| WhereConstraintsAcc
| GetAllWhereConstraint<
T,
R,
| (H extends WhereConstraint<T, any, WhereFilterOp, unknown>
| (H extends WhereConstraint<string, WhereFilterOp, unknown>
? H
: never)
| WhereConstraintsAcc
Expand All @@ -144,7 +144,7 @@ type GetAllWhereConstraintOpStr<
| GetAllWhereConstraintOpStr<
T,
R,
| (H extends WhereConstraint<T, any, WhereFilterOp, unknown>
| (H extends WhereConstraint<string, WhereFilterOp, unknown>
? H['opStr']
: never)
| OpStrAcc
Expand All @@ -155,7 +155,7 @@ type GetAllWhereConstraintOpStr<
export type WhereConstraintLimitation<
T extends MetaType,
Q extends GeneralQuery<T>,
U extends WhereConstraint<T, any, WhereFilterOp, unknown>,
U extends WhereConstraint<string, WhereFilterOp, unknown>,
PreviousQCs extends readonly QueryConstraints<T>[]
> = ValidateWhereNotIn<T, U, PreviousQCs> extends infer R extends string
? R
Expand All @@ -169,14 +169,12 @@ export type WhereConstraintLimitation<
? K
: U['opStr'] extends ValueOfOptStr
? WhereConstraint<
T,
U['fieldPath'],
U['opStr'],
GetCorrectDocumentIdBasedOnRef<T, Q, U['fieldPath'], U['value']>
>
: U['opStr'] extends ArrayOfOptStr
? WhereConstraint<
T,
U['fieldPath'],
U['opStr'],
U['value'] extends readonly never[]
Expand All @@ -187,7 +185,6 @@ export type WhereConstraintLimitation<
>
: U['opStr'] extends ValueOfOnlyArrayOptStr
? WhereConstraint<
T,
U['fieldPath'],
U['opStr'],
U['value'] extends readonly never[]
Expand All @@ -198,7 +195,6 @@ export type WhereConstraintLimitation<
>
: U['opStr'] extends ElementOfOptStr
? WhereConstraint<
T,
U['fieldPath'],
U['opStr'],
DeepValue<T['compare'], U['fieldPath']> extends readonly (infer R)[]
Expand Down

0 comments on commit dab4d50

Please sign in to comment.