Code sharing: type-safe select query builder with Typescript #35453
parad0xe
started this conversation in
Show and tell
Replies: 1 comment
-
Can we get this noticed? |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I would like to present type-safe select query builder that integrates with Supabase using TypeScript. The main goal is to provide a robust and type-checked way of constructing database queries and helping developers reduce runtime errors.
Key features
select
andbuild
function allows developers to build queries dynamically, specifying tables, fields, and related data, while maintaining type safety and correct select query structure.Code overview
Code
SHOW CODE (+400 lines)
1. Utility types
The utility types serve as the backbone for the select query builder. They define the core structures for tables, fields, and relationships in Supabase. By leveraging these types, developers can manage database schemas, foreign keys, and relationships in a way that ensures type safety at every stage of the select query process.
Among these, the
InferSelect
andBuild
types are the basis for ensuring type safety and flexibility within the system.InferSelect
: This type infers the fields and relations from a select definition. It automatically deduces the types for the fields and relationships of the specified table and its joins, providing enhanced safety for select query construction.Build
: This type transforms a select query definition into a string representing the SQL select query. It dynamically handles select query construction while respecting the structure of fields and relationships defined in the database schema. This type supports recursion to handle deeply nested relations.These types enable efficient select query management while ensuring that the generated queries align with database schema definitions, maintaining type safety throughout the process.
2. Dynamic select query construction
The select query builder functions, such as
select
,one
,many
andbuild
, allow developers to construct complex queries dynamically while ensuring that the select query syntax is consistent with the database schema. This guarantees that field names, relationships, and table references remain valid, preventing issues caused by typos or incorrect references.The
select
function, in particular, provides a clean and flexible way to build select queries. Developers can specify which fields to retrieve and which related tables to join, all while ensuring that the resulting select query structure is type-checked and adheres to the database schema.3. Inference and safety
Through TypeScript's powerful type inference system, the builder automatically determines the correct types for fields, relationships, and the resulting data. This automatic type checking ensures that the structure of select query results aligns with expectations, reducing the likelihood of runtime errors related to data mismatches.
4. Relation management
The select query builder handles complex relational data efficiently. It supports defining one-to-one and one-to-many relationships between tables. Nested relations are automatically handled, ensuring referential integrity and simplifying the process of select querying related data without the need for manual joins or complex select query logic.
Usage
Example: select query with one-to-one relationship
In this example, we will walk through the process of creating a select query that selects posts and their related categories using a select query builder.
1. Selecting posts
To start, we'll create a basic select query that selects posts from the database. We’ll focus on retrieving the
id
,name
, andslug
fields for each post.This select query is straightforward and will return posts with the necessary fields:
id
,name
, andslug
.2. Selecting categories
Next, we need to fetch categories from the categories table. In this case, we are selecting the
id
andname
fields of each category. This gives us the relevant data about the categories linked to the posts.This select query will return categories with their
id
andname
fields.3. Merging the selects with a one-to-one relationship
Now, we combine the two queries—posts and categories—by merging them together using a relationship. This is where we define the one-to-one relationship between the posts and categories tables.
In this relationship:
one
function, which links the posts to their respective categories. This is done through the foreign key relationship between the two tables.A relationship is established between the foreign key
posts_category_id_fkey
and thecategory_id
field in the posts table. This ensures the correct linking and typing of posts to their categories.4. Final select query
After merging the queries, we now have a final select query that selects posts along with their related categories. The result will return posts and their associated categories in a structured format.
By using this approach:
5. Build select query
You also have the option to build the SQL select query string for Supabase using the build function.
6. Use with supabase
Summary
In this example:
select
is used to define basic queries for posts and categories.one
is used to define a one-to-one relationship between posts and categories.merge
is used to combine the post select query with the category relation, enabling the retrieval of posts with their associated categories.build
is used to create the select query string used bysupabase.select()
This approach ensures that relational data can be queried with full type safety, making it easier to work with complex database relationships in a structured manner.
Functions
select
: Creates a new select query definition.merge
: Combines select queries into one.one
: Defines a one-to-one relationship in the select query.many
: Defines a one-to-many relationship in the select query.ref
: Defines foreign key relationships between tables.build
: Builds the final select query string based on the select defintion and their relationships.Conclusion
This type-safe select query builder improves the development process by ensuring type safety throughout database interactions. It simplifies managing relationships, supports dynamic select query construction, and guarantees that select query results match the expected structure. By providing a flexible approach to building select queries and handling complex relational data, it helps to write cleaner, more reliable code with fewer runtime errors.
Beta Was this translation helpful? Give feedback.
All reactions