Open wesharper opened 1 month ago
thanks for the writeup
We originally wanted exactly one way to query each collection. The project is mature enough that it might be time to expand that for the user QOL you mentioned
This unlikely to get attention given right away given our other priorities but I'll leave it open for when the time comes
Summary
Automatically generate queries for all tables with a defined primary key, where primary key is the singular argument and the query returns one singular record.
Rationale
While there are a handful of well-documented workarounds and approaches, they each have their own set of drawbacks. I believe that adding first-party support for this would simplify the learning curve, reduce ramp time, and better align with consumer expectations.
Other approaches and their issues
Using the
node(nodeId)
query:From the documentation, it seems as though this is the preferred method of consumption for singular records. While this approach works, there are some quality-of-life drawbacks.
Because the query necessitates the use of a fragment, usage in
typescript
front-ends can be cumbersome. This typically requires that the consumer check whether or not the node is of the correct type before they can unwrap. This leads to quite a bit of boilerplate in front-end code.Additionally, because
nodeId
is required to be globally unique, and becausenodeId
is not stored in a location accessible to the end user, there is no way for the application to know which record it is attempting to fetch. This makes the type-checking boilerplate necessary, because the intent of the application is unknown.This could also make URLs unstable i.e. (
/users/:userId
), depending on how, when, and wherenodeId
is generated.Creating UDFs:
It is fairly trivial to implement a UDF when this functionality is desired. However, if this is a pattern that the application developers adopt, it means generating a host of one-off functions that are practically identical.
These functions, while very simple, can quickly make the Supabase UI less useful (imagine 30
table_by_id
functions in the api docs section and the database functions view).UDFs are also more prone to user error, and increase the surface area through which policies and RBAC are accidentally overruled.
The TL;DR
Adding first-party support provides the following:
node(nodeId)
querynodeId
(filter, order, update)Design
<tablename>_by_pk(pk: pk_type)
method to all tables that define a pkExamples
Illustrations and examples to clarify descriptions from previous sections.
Drawbacks
Alternatives
Detailed in rationale section.
Unresolved Questions
Unfortunately, I don't currently have deep understanding of the codebase, so I don't have the necessary knowledge to generate a proper design.
What should happen if the record doesn't exist (err or null)?