Refactor improve query type safety#1
Draft
MurkyTheMurloc wants to merge 9 commits into
Draft
Conversation
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Thank you for submitting this pull request! We appreciate you spending the time to work on these changes.
What is the motivation?
This pull request aims to make the query function and the PreparedQuery class more type safe. This includes auto-completion for query bindings and basic type validation for binding values to reduce mistakes.
Limitations?
While working on this request, I came across some limitations of typescript.
TemplateStringsArray
As described here https://github.com/microsoft/TypeScript/issues/33304 until now typescript can't be used with type safe TemplateStringsArray. This creates some limitations.
First example:
Second example, value validation can't be inferred correctly:
A workaround would be to create a ESLint/biome extension to ensure proper value checking for query bindings.
partial generic type arguments
As described here https://github.com/microsoft/TypeScript/issues/10571 typescript doesn't support partial generic type arguments. This introduces the
problem when passing the return generic to query, for example
DB parameters
The last limitations are DB parameters. To make the query fully type safe, we need to know what params are set on the server. This could be achieved by adding runtime overhead inside the SDK by creating an object that will be inferred. This requires updating it when using the let and unset function, and fetch the params from the DB every time the use function is called.
Another approach would be to add a CLI tool with a watch mode similar to GraphQL-gen that generates the typescript types from the DB.
What does this change do?
Add typescript auto-completion for binding keys, value validation for strings including: UUID, Date, Record, Future. Optional bindings for rust function params. Excluding bindings for reserved variables like auth.
Add type safety for:
Adding a new query function for many queries this is useful when you write your query for example in surql files and just import them. The functions take in many surql statements and merges them into a single one.
Final change: changing <T extends unknown []> to <T extends unknown[T]>
that's more a personal opinion, but I think that it's more intuitive than wrapping the response type in []
What is your testing strategy?
I played around with the SDK and a local surrealdb instance but didn't found any unexpected errors.
Is this related to any issues?
If this pull request is related to any other pull request or issue, or resolves any issues - then link all related pull requests and issues here.
Have you read the Contributing Guidelines?