-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
breakup query utils into smaller files and fixup ts
- Loading branch information
Showing
18 changed files
with
182 additions
and
142 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './query.js'; | ||
export * from './results.js'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import type { | ||
CollectionNameFromModels, | ||
CollectionQuery, | ||
Models, | ||
QuerySelectionValue, | ||
RelationSubquery, | ||
} from '@triplit/db'; | ||
|
||
/** | ||
* Possible values for the syncStatus field in a query. | ||
* - pending: Items that are in the outbox | ||
* - confirmed: Items that have been confirmed by the server | ||
* - all: All items | ||
*/ | ||
export type SyncStatus = 'pending' | 'confirmed' | 'all'; | ||
|
||
/** | ||
* Query that can be passed to a Triplit Client. | ||
*/ | ||
export type ClientQuery< | ||
M extends ClientSchema | undefined, | ||
CN extends CollectionNameFromModels<M>, | ||
Selection extends QuerySelectionValue<M, CN> = QuerySelectionValue<M, CN>, | ||
Inclusions extends Record<string, RelationSubquery<M, any>> = Record< | ||
string, | ||
RelationSubquery<M, any> | ||
> | ||
> = { | ||
syncStatus?: SyncStatus; | ||
} & CollectionQuery<M, CN, Selection, Inclusions>; | ||
|
||
/** | ||
* A client query with default selection and inclusion. | ||
*/ | ||
export type ClientQueryDefault< | ||
M extends ClientSchema | undefined, | ||
CN extends CollectionNameFromModels<M> | ||
> = ClientQuery<M, CN, QuerySelectionValue<M, CN>, {}>; | ||
|
||
/** | ||
* Friendly alias for Models type. | ||
*/ | ||
export type ClientSchema = Models<any, any>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// There is some odd behavior when using infer with intersection types | ||
// Our query types are set up as: | ||
// CollectionQuery<...> = Query<...> & { ... } | ||
// ClientQuery<...> = CollectionQuery<...> & { ... } | ||
// | ||
// However, if you attempt to infer the generic of a base object (ex. CollectionQuery<infer M>) with the intersected object (ClientQuery<any>) the inferred type M is overly generic | ||
// | ||
// Recreating the fetch result types here to avoid this issue | ||
// Playground: https://www.typescriptlang.org/play?#code/KYDwDg9gTgLgBDAnmYcCyEAmwA2BnAHgCg44BhCHHYAYxgEsIA7AOQEMBbVUGYJzPHDwwo9JgHMANCTgAVODz4C4AJVrRMBYaImShIseIB8RI3AC8q9VE0UqtBs3Zc9sowG4iRJCjgAhNjxgAjQFEF5+QQxsfAI2JkQ9eMQjPTIWMIjlAGtgRAgAM3QzSwBvGTYYEQBGAC50AG10gF1PAF8vH1QAUXClYE1QxUj0LFxCZKSE1PIM4Zy8wuKLf0DgtDSWMwAyOFLKkQAmeu1DNs9vZFRZYGFqgnl5wQCguISplJK5TKVntbfEnBkmYAPxwADkYECeHBcHq4IKbHoOHBni6cluMEODx+IxewUmQOmX0efTx-zEBWAUDgAFUPqC6XCIYjkajOlc4ABJJhgACu8EsvSyAwIpV4wnq+3hBQgEHBbTaenBEpg4I8HN8ajwfJwMGqKxudwIPP5MA16O1uqxhsx2NNAo8QA | ||
|
||
import { | ||
CollectionNameFromModels, | ||
ReturnTypeFromQuery, | ||
Unalias, | ||
} from '@triplit/db'; | ||
import { ClientQuery, ClientQueryDefault, ClientSchema } from './query.js'; | ||
|
||
/** | ||
* Results from a query based on the query's model in the format `Map<id, entity>` | ||
*/ | ||
export type ClientFetchResult<C extends ClientQuery<any, any>> = Map< | ||
string, | ||
ClientFetchResultEntity<C> | ||
>; | ||
|
||
/** | ||
* Entity from a query based on the query's model | ||
*/ | ||
export type ClientFetchResultEntity<C extends ClientQuery<any, any, any, any>> = | ||
ReturnTypeFromQuery<C>; | ||
|
||
/** | ||
* The fully selected type of an entity, including all fields but not relations | ||
*/ | ||
export type Entity< | ||
M extends ClientSchema, | ||
CN extends CollectionNameFromModels<M> | ||
> = Unalias<ReturnTypeFromQuery<ClientQueryDefault<M, CN>>>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { | ||
CollectionNameFromModels, | ||
CollectionQuery, | ||
QueryBuilder, | ||
QuerySelectionValue, | ||
} from '@triplit/db'; | ||
import { ClientSchema } from '../client/types'; | ||
|
||
export class HttpClientQueryBuilder< | ||
CQ extends CollectionQuery<any, any, any, any> | ||
> extends QueryBuilder<CQ> { | ||
constructor(query: CQ) { | ||
super(query); | ||
} | ||
} | ||
|
||
export function httpClientQueryBuilder< | ||
M extends ClientSchema | undefined, | ||
CN extends CollectionNameFromModels<M> | ||
// syncStatus doesn't apply for the remote client | ||
>(collectionName: CN, params?: Omit<CollectionQuery<M, CN>, 'collectionName'>) { | ||
const query: CollectionQuery<M, CN> = { | ||
collectionName, | ||
...params, | ||
}; | ||
return new QueryBuilder< | ||
CollectionQuery<M, CN, QuerySelectionValue<M, CN>, {}> | ||
>(query); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.