Rough Implementation of Supabase
Devlog #7
I decided to implement Supabase due to how frequently the Statbotics API has been down recently. It also doubles as a place to store hundreds of trivia questions (I currently only have 40). Since Supabase is an SQL database, data within it is stored in tables, making it much more structured compared to No-SQL databases such as Firestore.
Issue (and Solution)
It took me much longer than it should have to get question filtering properly implemented for FRC trivia. Each question has a core category, era (time period), difficulty (easy, medium, or hard), and answer type (multiple choice, true or false, free response). Using the Supabase typescript .in() operator (used with the WHERE function in an SQL Editor), I can filter questions by their properties using .in() as a sort of shorthand for the logical OR operator.
The issue with this is that .in() only accepts a string parameter (which is easy enough) and a parameter with the type (string | readonly | null)[] (which is the issue here). Since I’m getting my second parameter from req.query, my Express backend will automatically group duplicate queries into a string array. However, any queries that are not duplicated will be saved as a string instead of a string array (even if I try forcing it to be a string array by using as string[]). This causes .in() to break since the second parameter has to be a string array. The fix here, which took me way too long to figure out, was to create a new variable for each query requirement and assign it a value depending on if the original requirement is an array or not. If the original requirement was an array, then the new variable will just be the exact same as the original requirement (since we need a string array anyways). On the other hand, if the original requirement wasn’t a string array already, we can just wrap the original requirement in brackets. To make sure the types of this new variable and the second parameter for .in() sufficiently overlap, we get the new variable as (string | null)[] . For example, const eras = (Array.isArray(era) ? era : [era]) as (string | null)[]. This ensures that eras will be a string array (or potentially null) and lets us use it within .in().
And now I can finally fetch questions from Supabase!
Next Steps
Get questions to show up on the frontend (that’s really it).