Skip to content

Commit

Permalink
fix and simplify orderByConstraints type
Browse files Browse the repository at this point in the history
  • Loading branch information
tylim88 committed Apr 16, 2024
1 parent 13b12aa commit 22400bd
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 28 deletions.
14 changes: 5 additions & 9 deletions src/types/queryConstraints/orderBy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,16 @@ import { OrderByDirection } from '../alias'
import { MetaType, GetAllCompareKeys } from '../metaTypeCreator'
import { __name__ } from '../fieldPath'

export type OrderByConstraint<
FieldPath extends string,
DirectionStr extends OrderByDirection | undefined = undefined
> = {
export type OrderByConstraint<FieldPath extends string> = {
type: 'orderBy'
fieldPath: FieldPath
directionStr: DirectionStr
directionStr: OrderByDirection
}

export type OrderBy = <
T extends MetaType,
FieldPath extends GetAllCompareKeys<T> | __name__,
DirectionStr extends OrderByDirection | undefined = undefined
FieldPath extends GetAllCompareKeys<T> | __name__
>(
fieldPath: FieldPath,
directionStr?: DirectionStr
) => OrderByConstraint<FieldPath, DirectionStr>
directionStr?: OrderByDirection
) => OrderByConstraint<FieldPath>
4 changes: 2 additions & 2 deletions src/types/queryConstraints/query.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { CursorType, CursorConstraint } from './cursor'
import { WhereConstraint } from './where'
import { WhereFilterOp, OrderByDirection } from '../alias'
import { WhereFilterOp } from '../alias'
import { LimitConstraint } from './limit'
import { OrderByConstraint } from './orderBy'
import { OffsetConstraint } from './offset'
Expand All @@ -9,5 +9,5 @@ export type QueryConstraints =
| WhereConstraint<string, WhereFilterOp, unknown>
| LimitConstraint<'limit' | 'limitToLast', number>
| CursorConstraint<CursorType, unknown[]>
| OrderByConstraint<string, OrderByDirection | undefined>
| OrderByConstraint<string>
| OffsetConstraint
9 changes: 3 additions & 6 deletions src/types/queryConstraintsLimitations/cursor.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { MetaType } from '../metaTypeCreator'
import { OrderByDirection } from '../alias'
import { ErrorCursorTooManyArguments } from '../error'
import {
QueryConstraints,
Expand All @@ -18,10 +17,10 @@ type ValidateCursorOrderBy<
T extends MetaType,
Q extends Query<T>,
Values extends unknown[],
AllOrderBy extends OrderByConstraint<string, OrderByDirection | undefined>[]
AllOrderBy extends OrderByConstraint<string>[]
> = Values extends [infer Head, ...infer Rest]
? AllOrderBy extends [infer H, ...infer R]
? H extends OrderByConstraint<string, OrderByDirection | undefined>
? H extends OrderByConstraint<string>
? [
H['fieldPath'] extends __name__
? GetCorrectDocumentIdBasedOnRef<T, Q, H['fieldPath'], Head>
Expand All @@ -38,9 +37,7 @@ type ValidateCursorOrderBy<
T,
Q,
Rest,
R extends OrderByConstraint<string, OrderByDirection | undefined>[]
? R
: []
R extends OrderByConstraint<string>[] ? R : []
>
]
: never // impossible route
Expand Down
13 changes: 5 additions & 8 deletions src/types/queryConstraintsLimitations/orderBy.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { MetaType } from '../metaTypeCreator'
import { OrderByDirection } from '../alias'
import { ErrorWhereOrderByEquality } from '../error'
import {
QueryConstraints,
Expand All @@ -12,7 +11,7 @@ import { In, Equal } from './utils'
// You can't order your query by a field included in an equality (==) or (in) clause.
export type ValidateOrderByEqualityWhere<
T extends MetaType,
U extends OrderByConstraint<string, OrderByDirection | undefined>,
U extends OrderByConstraint<string>,
AllQCs extends QueryConstraints[]
> = Extract<
GetAllWhereConstraint<T, AllQCs, never>,
Expand All @@ -23,7 +22,7 @@ export type ValidateOrderByEqualityWhere<

export type OrderByConstraintLimitation<
T extends MetaType,
U extends OrderByConstraint<string, OrderByDirection | undefined>,
U extends OrderByConstraint<string>,
AllQCs extends QueryConstraints[]
> = ValidateOrderByEqualityWhere<T, U, AllQCs> extends false
? ErrorWhereOrderByEquality
Expand All @@ -33,7 +32,7 @@ export type GetFirstOrderBy<
T extends MetaType,
QCs extends QueryConstraints[]
> = QCs extends [infer H, ...infer Rest]
? H extends OrderByConstraint<string, OrderByDirection | undefined>
? H extends OrderByConstraint<string>
? H
: Rest extends QueryConstraints[]
? GetFirstOrderBy<T, Rest>
Expand All @@ -43,15 +42,13 @@ export type GetFirstOrderBy<
export type GetAllOrderBy<
T extends MetaType,
QCs extends QueryConstraints[],
AllOrderBy extends OrderByConstraint<string, OrderByDirection | undefined>[]
AllOrderBy extends OrderByConstraint<string>[]
> = QCs extends [infer H, ...infer Rest]
? Rest extends QueryConstraints[]
? GetAllOrderBy<
T,
Rest,
H extends OrderByConstraint<string, OrderByDirection | undefined>
? [...AllOrderBy, H]
: AllOrderBy
H extends OrderByConstraint<string> ? [...AllOrderBy, H] : AllOrderBy
>
: [] // impossible route
: AllOrderBy // not found, no check needed
6 changes: 3 additions & 3 deletions src/types/queryConstraintsLimitations/query.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { MetaType } from '../metaTypeCreator'
import { WhereFilterOp, OrderByDirection } from '../alias'
import { WhereFilterOp } from '../alias'
import {
QueryConstraints,
WhereConstraint,
Expand All @@ -25,7 +25,7 @@ export type ValidateOrderByAndInequalityWhere<
> = GetFirstInequalityWhere<T, AllQCs> extends infer W
? W extends WhereConstraint<string, InequalityOpStr, unknown>
? GetFirstOrderBy<T, AllQCs> extends infer O
? O extends OrderByConstraint<string, OrderByDirection | undefined>
? O extends OrderByConstraint<string>
? IsSame<W['fieldPath'], O['fieldPath']> extends true
? true
: ErrorWhereOrderByAndInEquality<O['fieldPath'], W['fieldPath']>
Expand All @@ -47,7 +47,7 @@ export type QueryConstraintLimitation<
? [
Head extends LimitConstraint<'limit', number> | OffsetConstraint
? Head
: Head extends OrderByConstraint<string, OrderByDirection | undefined>
: Head extends OrderByConstraint<string>
? OrderByConstraintLimitation<T, Head, AllQCs>
: Head extends LimitConstraint<'limitToLast', number>
? LimitToLastConstraintLimitation<T, Head, AllQCs>
Expand Down

0 comments on commit 22400bd

Please sign in to comment.