https://supabase.com/ logo
#help
Title
# help
m

Milou

03/26/2022, 6:47 PM
Hi! Short question: Is there any way to get more typesafe results from the supabase service? I'm trying to build an application in angular and right now I need to check in my components if all the fields from a row are present, i.e.
n

Needle

03/26/2022, 6:47 PM
Hello ! This thread has been automatically created from your message in a ``few seconds ago``. Pinging @User so that they see this as well! Want to unsubscribe from this thread? Right-click the thread in Discord (or use the ... menu) and select Leave Thread to unsubscribe from future updates. Want to change the title? Use the
/title
command! We have solved your problem? Click the button below to archive it.
m

Milou

03/26/2022, 6:49 PM
I have code like this:
Copy code
ts
getQuestion(id : string) : Observable<any> {
    return from(this.supabase.from('question').select('*').eq('id', id).single())
  }
in my supabase-service.ts But this is annoying as the result returned is some weird Observable/postgres-result-like object. What i'd much rather have, would be for the function to either return the complete, typesafe data from the request, for example by using an interface or object definition and instantiating it, or throw errors if the request failed.
Is there any way to make supabase do this automatically (return fully assembled objects from a request or throw error) or would i need to have to implement this myself?
should i just switch to prisma if i want more type-safety?
s

Scott P

03/26/2022, 7:59 PM
https://supabase.com/docs/reference/javascript/generating-types - It's possible to generate types for your DB
n

Needle

03/26/2022, 7:59 PM
Milou (2022-03-26)
s

Scott P

03/26/2022, 8:01 PM
You can then do
supabase.from<definitions['question']>('question').select('*').eq('id', id).single()
m

Milou

03/27/2022, 3:43 AM
neat, thank you! btw, super cool that youre using the openapi spec for this. ive been trying to convince people that you can use it for so much more other than documentation and thats a great example, not only for generating code from such a spec, but also achieving looser coupling (by not having to use an ORM such as prisma)