Skip to content

Commit

Permalink
Merge pull request #54 from tylim88/new-features
Browse files Browse the repository at this point in the history
orderBy('\_name') cursor now only accept full doc path, if input type…
  • Loading branch information
tylim88 authored May 5, 2022
2 parents 1f59fd8 + ebe2af5 commit 87beb4d
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 23 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# HISTORY

## v1.3.0 6-May-2022

- orderBy('\_name') cursor now only accept full doc path, if input type is string, require const assertion or else display "Please use const assertion" error message.

## v1.2.1 6-May-2022

- fix cursor runtime not usable bug https://github.com/tylim88/Firelordjs/issues/51

## v1.2.0 5-May-2022

- fix cursor not able to use with query document snapshot and document snapshot https://github.com/tylim88/Firelordjs/issues/51
Expand Down
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,14 @@

<br/>

> FirelordJS is the only library that capable of providing insane type safety while exposing all the API of the original Firestore SDK.
>
> FirelordJS has the lowest learning curve, best type safety and possibly also the smallest.
>
> It is what you are looking at: the Master of Fire.
>
> Note: let me know if you found anything that is better than this.
support [@firebase/rules-unit-testing](https://firelordjs.com/tests)

The development code, built code and published code are all tested in ci.
Expand Down
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "firelordjs",
"version": "1.2.1",
"version": "1.3.0",
"description": "🔥 Extremely High Precision Typescript Wrapper for Firestore Web, Providing Unparalleled Type Safe and Dev Experience",
"source": "src/index.ts",
"main": "dist/src/index.js",
Expand Down Expand Up @@ -71,7 +71,7 @@
"eslint-plugin-prettier": "^4.0.0",
"firebase": "^9.7.0",
"husky": "^7.0.0",
"jest": "^27.2.5",
"jest": "^27.5.1",
"jsdoc": "^3.6.5",
"lodash": "^4.17.21",
"parcel": "^2.3.2",
Expand Down
23 changes: 21 additions & 2 deletions src/queryClauses/orderBy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,25 @@ it('Too many arguments provided to startAt/startAfter/endAt/endBefore(). The num
endAt(1, ParentDocumentSnapshot)
)
).toThrow() // the only value __name__ can use for cursor is full doc path

query(
ref,
orderBy('a.b.c'),
orderBy('__name__'),
limit(1),
startAt(1),
// @ts-expect-error
endAt(1, 'topLevel/FirelordTest/Users/123')
)

query(
ref,
orderBy('a.b.c'),
orderBy('__name__'),
limit(1),
startAt(1), // @ts-expect-error
endAt(UserQueryDocumentSnapshot, 'a/123')
)
})
it('Too many arguments provided to startAt/startAfter/endAt/endBefore(). The number of arguments must be less than or equal to the number of orderBy() clauses, positive case', () => {
// cursor with has x number of arguments must has x number of orderBy clause before that cursor
Expand Down Expand Up @@ -215,7 +234,7 @@ it('Too many arguments provided to startAt/startAfter/endAt/endBefore(). The num
orderBy('__name__'),
limit(1),
startAt(1),
endAt(1, 'a/123')
endAt(1, `topLevel/FirelordTest/Users/123` as const)
)

query(
Expand All @@ -232,6 +251,6 @@ it('Too many arguments provided to startAt/startAfter/endAt/endBefore(). The num
orderBy('__name__'),
limit(1),
startAt(1),
endAt(UserQueryDocumentSnapshot, 'a/123')
endAt(UserQueryDocumentSnapshot, `topLevel/FirelordTest/Users/123` as const)
)
})
3 changes: 3 additions & 0 deletions src/types/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ export type ErrorWhereDocumentFieldPath =
'If field path is document ID, then value must be string'
export type ErrorWhere__name__ =
"Error: Dont use ( __name__ ) directly as where's field path, use documentId() sentinel field path instead."
export type ErrorCursor__name__ =
'Error: detected type is string, please do const assertion'

export type ErrorMsgs =
| ErrorUndefined
Expand Down Expand Up @@ -111,6 +113,7 @@ export type ErrorMsgs =
| ErrorWhereDocumentFieldPath
| ErrorWhere__name__
| ErrorWhereNoNeverEmptyArray
| ErrorCursor__name__

export type ReplaceErrorMsgsWithNever<T> = T extends ErrorMsgs ? never : T // ! not yet implemented anywhere

Expand Down
68 changes: 52 additions & 16 deletions src/types/queryConstraintLimitation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
ErrorWhereOnlyOneNotEqual,
ErrorCursorTooManyArguments,
ErrorWhereNoNeverEmptyArray,
ErrorCursor__name__,
} from './error'
import { IsSame, IsTrue } from './utils'
import {
Expand All @@ -25,6 +26,7 @@ import { Query, CollectionReference } from './ref'
import {
GetCorrectDocumentIdBasedOnRef,
RemoveSentinelFieldPathFromCompare,
__name__,
} from './fieldPath'
import { CursorType } from './cursor'
import { QueryDocumentSnapshot, DocumentSnapshot } from './snapshot'
Expand Down Expand Up @@ -124,15 +126,45 @@ export type QueryConstraintLimitation<
type ValidateCursorOrderBy<
T extends MetaType,
Values extends unknown[],
AllOrderFieldValue extends unknown[]
AllOrderBy extends OrderByConstraint<
T,
string,
FirelordFirestore.OrderByDirection | undefined
>[]
> = Values extends [infer Head, ...infer Rest]
? AllOrderFieldValue extends [infer H, ...infer R]
? [
Head extends H | QueryDocumentSnapshot<T> | DocumentSnapshot<T>
? Head | QueryDocumentSnapshot<T> | DocumentSnapshot<T>
: H | QueryDocumentSnapshot<T> | DocumentSnapshot<T>,
...ValidateCursorOrderBy<T, Rest, R>
]
? AllOrderBy extends [infer H, ...infer R]
? H extends OrderByConstraint<
T,
string,
FirelordFirestore.OrderByDirection | undefined
>
? [
H['fieldPath'] extends __name__
? string extends Head
? ErrorCursor__name__
: T['docPath']
: Head extends
| T['compare'][H['fieldPath']]
| QueryDocumentSnapshot<T>
| DocumentSnapshot<T>
? Head | QueryDocumentSnapshot<T> | DocumentSnapshot<T>
:
| T['compare'][H['fieldPath']]
| QueryDocumentSnapshot<T>
| DocumentSnapshot<T>,
...ValidateCursorOrderBy<
T,
Rest,
R extends OrderByConstraint<
T,
string,
FirelordFirestore.OrderByDirection | undefined
>[]
? R
: []
>
]
: never // impossible route
: [ErrorCursorTooManyArguments]
: [] // end, Rest is []

Expand All @@ -145,7 +177,7 @@ type CursorConstraintLimitation<
ValidateCursorOrderBy<
RemoveSentinelFieldPathFromCompare<T>,
U['values'],
GetAllOrderByFieldValue<T, PreviousQCs, []>
GetAllOrderBy<T, PreviousQCs, []>
>
>

Expand Down Expand Up @@ -336,7 +368,7 @@ type WhereConstraintLimitation<
U['opStr'],
U['value'] extends never[]
? ErrorWhereNoNeverEmptyArray
: T['compare'][U['fieldPath']] extends (infer R)[]
: T['compare'][U['fieldPath']] extends unknown[]
? T['compare'][U['fieldPath']]
: ErrorWhereCompareValueMustBeArray<U['fieldPath']>
>
Expand Down Expand Up @@ -377,25 +409,29 @@ type GetFirstOrderBy<
: never // impossible route
: true // not found, no check needed

type GetAllOrderByFieldValue<
type GetAllOrderBy<
T extends MetaType,
QCs extends QueryConstraints<T>[],
FieldValueTypeAcc extends unknown[]
AllOrderBy extends OrderByConstraint<
T,
string,
FirelordFirestore.OrderByDirection | undefined
>[]
> = QCs extends [infer H, ...infer Rest]
? Rest extends QueryConstraints<T>[]
? GetAllOrderByFieldValue<
? GetAllOrderBy<
T,
Rest,
H extends OrderByConstraint<
T,
string,
FirelordFirestore.OrderByDirection | undefined
>
? [...FieldValueTypeAcc, T['compare'][H['fieldPath']]]
: FieldValueTypeAcc
? [...AllOrderBy, H]
: AllOrderBy
>
: [] // impossible route
: FieldValueTypeAcc // not found, no check needed
: AllOrderBy // not found, no check needed

type GetAllWhereConstraint<
T extends MetaType,
Expand Down

0 comments on commit 87beb4d

Please sign in to comment.