Skip to content

Commit

Permalink
further simply query constraints type
Browse files Browse the repository at this point in the history
  • Loading branch information
tylim88 committed Apr 6, 2024
1 parent dab4d50 commit 1764478
Show file tree
Hide file tree
Showing 11 changed files with 69 additions and 81 deletions.
12 changes: 8 additions & 4 deletions src/refs/composite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ export const andCreator: AndCreator =
() =>
// @ts-expect-error
(...queryConstraints) => {
// @ts-expect-error
const constraints = queryBuilder(queryConstraints)
const constraints = queryBuilder(
// @ts-expect-error
queryConstraints
)
return {
type: 'and',
constraints,
Expand All @@ -19,8 +21,10 @@ export const orCreator: OrCreator =
() =>
// @ts-expect-error
(...queryConstraints) => {
// @ts-expect-error
const constraints = queryBuilder(queryConstraints)
const constraints = queryBuilder(
// @ts-expect-error
queryConstraints
)
return {
type: 'or',
constraints,
Expand Down
6 changes: 2 additions & 4 deletions src/refs/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { QueryAllConstraints, MetaType, QueryFilterConstraints } from '../types'
import { QueryAllConstraints, QueryFilterConstraints } from '../types'

export const buildPathFromColIDsAndDocIDs = ({
collectionIDs,
Expand All @@ -14,9 +14,7 @@ export const buildPathFromColIDsAndDocIDs = ({
}

export const queryBuilder = (
queryConstraints:
| QueryAllConstraints<MetaType>[]
| QueryFilterConstraints<MetaType>[]
queryConstraints: QueryAllConstraints[] | QueryFilterConstraints[]
) =>
queryConstraints.reduce((acc, qc) => {
const type = qc.type
Expand Down
9 changes: 4 additions & 5 deletions src/types/queryConstraints/composite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ import { ErrorEmptyCompositeFilter } from '../error'
import { OriQueryCompositeFilterConstraint } from '../alias'

export type QueryCompositeFilterConstraint<
T extends MetaType,
Y extends 'and' | 'or',
X extends QueryFilterConstraints<T>[]
X extends QueryFilterConstraints[]
> = {
type: Y
constraints: X
Expand All @@ -17,7 +16,7 @@ export type QueryCompositeFilterConstraint<

type QueryCompositeFilter<T extends MetaType, Type extends 'and' | 'or'> = <
Q extends GeneralQuery<T>,
QFCs extends QueryFilterConstraints<T>[]
QFCs extends QueryFilterConstraints[]
>(
...queryFilterConstraints: QFCs extends never
? QFCs
Expand All @@ -28,9 +27,9 @@ type QueryCompositeFilter<T extends MetaType, Type extends 'and' | 'or'> = <
Q,
QFCs,
[],
QueryCompositeFilterConstraint<T, Type, QFCs>
QueryCompositeFilterConstraint<Type, QFCs>
>
) => QueryCompositeFilterConstraint<T, Type, QFCs>
) => QueryCompositeFilterConstraint<Type, QFCs>

/**
* Creates a new {@link QueryCompositeFilterConstraint} that is a conjunction of
Expand Down
17 changes: 8 additions & 9 deletions src/types/queryConstraints/query.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,23 @@
import { MetaType } from '../metaTypeCreator'
import { WhereFilterOp } from '../alias'
import { CursorType, CursorConstraint } from './cursor'
import { WhereConstraint } from './where'
import { LimitConstraint } from './limit'
import { OrderByConstraint } from './orderBy'
import { QueryCompositeFilterConstraint } from './composite'

type QueryNonFilterConstraints<T extends MetaType> =
type QueryNonFilterConstraints =
| LimitConstraint<'limit' | 'limitToLast'>
| CursorConstraint<CursorType, unknown[]>
| OrderByConstraint<string>

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

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

export type QueryAllConstraints<T extends MetaType> =
| QueryConstraints<T>
| QueryCompositeFilterConstraint<T, 'and' | 'or', QueryFilterConstraints<T>[]>
export type QueryAllConstraints =
| QueryConstraints
| QueryCompositeFilterConstraint<'and' | 'or', QueryFilterConstraints[]>
51 changes: 20 additions & 31 deletions src/types/queryConstraintsLimitations/composite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,20 @@ import { NotIn } from './utils'

type GetAllQueryFilterCompositeConstraint<
T extends MetaType,
QQCs extends readonly QueryAllConstraints<T>[],
QQCs extends readonly QueryAllConstraints[],
QueryCompositeConstraintAcc extends QueryCompositeFilterConstraint<
T,
'and' | 'or',
QueryFilterConstraints<T>[]
QueryFilterConstraints[]
>
> = QQCs extends [infer H, ...infer R extends QueryAllConstraints<T>[]]
> = QQCs extends [infer H, ...infer R extends QueryAllConstraints[]]
?
| QueryCompositeConstraintAcc
| GetAllQueryFilterCompositeConstraint<
T,
R,
| (H extends QueryCompositeFilterConstraint<
T,
'and' | 'or',
QueryFilterConstraints<T>[]
QueryFilterConstraints[]
>
? H
: never)
Expand All @@ -49,7 +47,7 @@ type GetAllQueryFilterCompositeConstraint<
// check where + or/and
export type ValidateTopLevelQueryCompositeFilterPartOne<
T extends MetaType,
AllQQCs extends readonly QueryAllConstraints<T>[]
AllQQCs extends readonly QueryAllConstraints[]
> = AllQQCs extends (infer P)[]
? Extract<P, WhereConstraint<string, WhereFilterOp, unknown>> extends never
? true
Expand All @@ -62,13 +60,12 @@ export type ValidateTopLevelQueryCompositeFilterPartOne<
// check or/and + or/and
export type ValidateTopLevelQueryCompositeFilterPartTwo<
T extends MetaType,
AllQQCs extends readonly QueryAllConstraints<T>[],
AllQQCs extends readonly QueryAllConstraints[],
AlreadyExist extends boolean = false
> = AllQQCs extends [infer Head, ...infer Rest extends QueryAllConstraints<T>[]]
> = AllQQCs extends [infer Head, ...infer Rest extends QueryAllConstraints[]]
? Head extends QueryCompositeFilterConstraint<
T,
'and' | 'or',
QueryFilterConstraints<T>[]
QueryFilterConstraints[]
>
? AlreadyExist extends false
? ValidateTopLevelQueryCompositeFilterPartTwo<T, Rest, true>
Expand All @@ -78,18 +75,17 @@ export type ValidateTopLevelQueryCompositeFilterPartTwo<

export type FlattenQueryCompositeFilterConstraint<
T extends MetaType,
QQCs extends readonly QueryAllConstraints<T>[],
ACC extends QueryConstraints<T>[] = []
> = QQCs extends [infer Head, ...infer Rest extends QueryAllConstraints<T>[]]
QQCs extends readonly QueryAllConstraints[],
ACC extends QueryConstraints[] = []
> = QQCs extends [infer Head, ...infer Rest extends QueryAllConstraints[]]
? FlattenQueryCompositeFilterConstraint<
T,
Rest,
Head extends QueryConstraints<T>
Head extends QueryConstraints
? [...ACC, Head]
: Head extends QueryCompositeFilterConstraint<
T,
'and' | 'or',
QueryFilterConstraints<T>[]
QueryFilterConstraints[]
>
? [
...ACC,
Expand All @@ -106,22 +102,17 @@ export type FlattenQueryCompositeFilterConstraint<
export type QueryFilterConstraintLimitation<
T extends MetaType,
Q extends GeneralQuery<T>,
RestQQCs extends readonly QueryAllConstraints<T>[],
PreviousQCs extends readonly QueryConstraints<T>[],
RestQQCs extends readonly QueryAllConstraints[],
PreviousQCs extends readonly QueryConstraints[],
ParentConstraint extends QueryCompositeFilterConstraint<
T,
'and' | 'or',
QueryFilterConstraints<T>[]
QueryFilterConstraints[]
>
> = RestQQCs extends [
infer Head extends
| QueryConstraints<T>
| QueryCompositeFilterConstraint<
T,
'and' | 'or',
QueryFilterConstraints<T>[]
>,
...infer Rest extends QueryAllConstraints<T>[]
| QueryConstraints
| QueryCompositeFilterConstraint<'and' | 'or', QueryFilterConstraints[]>,
...infer Rest extends QueryAllConstraints[]
]
? [
Head extends
Expand All @@ -138,12 +129,10 @@ export type QueryFilterConstraintLimitation<
: WhereConstraintLimitation<T, Q, Head, PreviousQCs>
: WhereConstraintLimitation<T, Q, Head, PreviousQCs>
: Head extends QueryCompositeFilterConstraint<
T,
'and' | 'or',
QueryFilterConstraints<T>[]
QueryFilterConstraints[]
>
? QueryCompositeFilterConstraint<
T,
Head['type'],
QueryFilterConstraintLimitation<
T,
Expand Down
2 changes: 1 addition & 1 deletion src/types/queryConstraintsLimitations/cursor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export type CursorConstraintLimitation<
T extends MetaType,
Q extends GeneralQuery<T>,
U extends CursorConstraint<CursorType, unknown[]>,
PreviousQCs extends readonly QueryConstraints<T>[]
PreviousQCs extends readonly QueryConstraints[]
> = CursorConstraint<
CursorType,
ValidateCursorOrderBy<T, Q, U['values'], GetAllOrderBy<T, PreviousQCs, []>>
Expand Down
4 changes: 2 additions & 2 deletions src/types/queryConstraintsLimitations/limit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import { QueryConstraints, LimitConstraint } from '../queryConstraints'
export type LimitToLastConstraintLimitation<
T extends MetaType,

Check warning on line 6 in src/types/queryConstraintsLimitations/limit.ts

View workflow job for this annotation

GitHub Actions / build_publish

'T' is defined but never used. Allowed unused vars must match /^_/u
U extends LimitConstraint<'limitToLast'>,
AllQCs extends readonly QueryConstraints<T>[]
AllQCs extends readonly QueryConstraints[]
> = AllQCs extends (infer A)[]
? A extends QueryConstraints<T>
? A extends QueryConstraints
? A['type'] extends 'orderBy'
? U
: ErrorLimitToLastOrderBy
Expand Down
8 changes: 4 additions & 4 deletions src/types/queryConstraintsLimitations/orderBy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@ import { QueryConstraints, OrderByConstraint } from '../queryConstraints'

export type GetFirstOrderBy<
T extends MetaType,
QCs extends readonly QueryConstraints<T>[]
QCs extends readonly QueryConstraints[]
> = QCs extends [infer H, ...infer Rest]
? H extends OrderByConstraint<string>
? H
: Rest extends readonly QueryConstraints<T>[]
: Rest extends readonly QueryConstraints[]
? GetFirstOrderBy<T, Rest>
: never // impossible route
: true // not found, no check needed

export type GetAllOrderBy<
T extends MetaType,
QCs extends readonly QueryConstraints<T>[],
QCs extends readonly QueryConstraints[],
AllOrderBy extends OrderByConstraint<string>[]
> = QCs extends [infer H, ...infer Rest]
? Rest extends readonly QueryConstraints<T>[]
? Rest extends readonly QueryConstraints[]
? GetAllOrderBy<
T,
Rest,
Expand Down
15 changes: 7 additions & 8 deletions src/types/queryConstraintsLimitations/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import {
// If you include a filter with a range comparison (<, <=, >, >=), your first ordering must be on the same field
export type ValidateOrderByAndInequalityWhere<
T extends MetaType,
AllQCs extends readonly QueryConstraints<T>[]
AllQCs extends readonly QueryConstraints[]
> = GetFirstInequalityWhere<T, AllQCs> extends infer W extends WhereConstraint<
string,
InequalityOpStr,
Expand All @@ -49,12 +49,12 @@ export type ValidateOrderByAndInequalityWhere<
export type QueryConstraintLimitation<
T extends MetaType,
Q extends GeneralQuery<T>,
RestQQCs extends readonly QueryAllConstraints<T>[],
PreviousQCs extends readonly QueryConstraints<T>[],
AllQCs extends readonly QueryConstraints<T>[]
RestQQCs extends readonly QueryAllConstraints[],
PreviousQCs extends readonly QueryConstraints[],
AllQCs extends readonly QueryConstraints[]
> = RestQQCs extends [
infer Head extends QueryAllConstraints<T>,
...infer Rest extends QueryAllConstraints<T>[]
infer Head extends QueryAllConstraints,
...infer Rest extends QueryAllConstraints[]
]
? [
Head extends LimitConstraint<'limit'> | OrderByConstraint<string>
Expand All @@ -72,9 +72,8 @@ export type QueryConstraintLimitation<
: Head extends CursorConstraint<CursorType, unknown[]>
? CursorConstraintLimitation<T, Q, Head, PreviousQCs>
: Head extends QueryCompositeFilterConstraint<
T,
'and' | 'or',
QueryFilterConstraints<T>[]
QueryFilterConstraints[]
>
? QueryFilterConstraintLimitation<
T,
Expand Down
22 changes: 11 additions & 11 deletions src/types/queryConstraintsLimitations/where.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import { DeepValue } from '../objectFlatten'
type ValidateWhereNotIn<
T extends MetaType,
U extends WhereConstraint<string, WhereFilterOp, unknown>,
PreviousQCs extends readonly QueryConstraints<T>[]
PreviousQCs extends readonly QueryConstraints[]
> = U['opStr'] extends NotIn
? Extract<
GetAllWhereConstraintOpStr<T, PreviousQCs, never>,
Expand All @@ -52,7 +52,7 @@ type ValidateWhereNotIn<
type ValidateWhereNotEqual<
T extends MetaType,
U extends WhereConstraint<string, WhereFilterOp, unknown>,
PreviousQCs extends readonly QueryConstraints<T>[]
PreviousQCs extends readonly QueryConstraints[]
> = U['opStr'] extends NotEqual
? Extract<
GetAllWhereConstraintOpStr<T, PreviousQCs, never>,
Expand All @@ -66,7 +66,7 @@ type ValidateWhereNotEqual<
export type ValidateWhereArrayContainsArrayContainsAny<
T extends MetaType,
U extends WhereConstraint<string, WhereFilterOp, unknown>,
PreviousQCs extends readonly QueryConstraints<T>[]
PreviousQCs extends readonly QueryConstraints[]
> = U['opStr'] extends ArrayContains
? Extract<
GetAllWhereConstraintOpStr<T, PreviousQCs, never>,
Expand All @@ -87,7 +87,7 @@ export type ValidateWhereArrayContainsArrayContainsAny<
type ValidateWhereInequalityOpStrSameField<
T extends MetaType,
U extends WhereConstraint<string, WhereFilterOp, unknown>,
PreviousQCs extends readonly QueryConstraints<T>[]
PreviousQCs extends readonly QueryConstraints[]
> = U['opStr'] extends InequalityOpStr
? Extract<
GetAllWhereConstraint<T, PreviousQCs, never>,
Expand All @@ -107,19 +107,19 @@ type ValidateWhereInequalityOpStrSameField<

export type GetFirstInequalityWhere<
T extends MetaType,
QCs extends readonly QueryConstraints<T>[]
> = QCs extends [infer H, ...infer Rest extends readonly QueryConstraints<T>[]]
QCs extends readonly QueryConstraints[]
> = QCs extends [infer H, ...infer Rest extends readonly QueryConstraints[]]
? 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>[],
AllQCs extends readonly QueryConstraints[],
WhereConstraintsAcc extends WhereConstraint<string, WhereFilterOp, unknown>
> = AllQCs extends [infer H, ...infer R]
? R extends readonly QueryConstraints<T>[]
? R extends readonly QueryConstraints[]
?
| WhereConstraintsAcc
| GetAllWhereConstraint<
Expand All @@ -135,10 +135,10 @@ export type GetAllWhereConstraint<

type GetAllWhereConstraintOpStr<
T extends MetaType,
QCs extends readonly QueryConstraints<T>[],
QCs extends readonly QueryConstraints[],
OpStrAcc extends WhereFilterOp
> = QCs extends [infer H, ...infer R]
? R extends readonly QueryConstraints<T>[]
? R extends readonly QueryConstraints[]
?
| OpStrAcc
| GetAllWhereConstraintOpStr<
Expand All @@ -156,7 +156,7 @@ export type WhereConstraintLimitation<
T extends MetaType,
Q extends GeneralQuery<T>,
U extends WhereConstraint<string, WhereFilterOp, unknown>,
PreviousQCs extends readonly QueryConstraints<T>[]
PreviousQCs extends readonly QueryConstraints[]
> = ValidateWhereNotIn<T, U, PreviousQCs> extends infer R extends string
? R
: ValidateWhereNotEqual<T, U, PreviousQCs> extends infer P extends string
Expand Down
Loading

0 comments on commit 1764478

Please sign in to comment.