tRPC prisma query type checking.

I'm wondering what the best practice is for type checking a prisma query in a tRPC function. I've been using input validation with zod for passing parameters into the functions but am not sure how to check types for a query which i then want to perform logic onto. bookingsPerDate: protectedProcedure.query(async ({ ctx }) => { const bookings = await ctx.prisma.bookings.findMany({}); for (let i = 0; i < bookings.length; i++) { checkIn = bookings[i].checkIn; checkOut = bookings[i].checkOut; /* More Logic */ } console.log(bookings); }),
32 Replies
Keef
Keef•2y ago
There really isn't a need to type check the response from a query since its comes from a "secure" source. Type checking user input is useful because the user is stupid but your database especially prisma should already be returning the correct type I see you being shy shy
Christian Lind
Christian Lind•2y ago
@CFKeef Thanks, that makes sense. TS is still complaining and saying that my bookings object may be undefined.
Keef
Keef•2y ago
its possible to do it just serves no purpose Is your prisma generated?
Christian Lind
Christian Lind•2y ago
What does that mean?
Keef
Keef•2y ago
So prisma needsto compile the typings for you run yarn prisma generate
Christian Lind
Christian Lind•2y ago
just ran prisma generate
Keef
Keef•2y ago
now do ctrl-p restart ts server in your vscode sometimes ts is lagging behind and you can force the sync
Christian Lind
Christian Lind•2y ago
did that, still get the same error...
Keef
Keef•2y ago
Could be your tsconfig
Christian Lind
Christian Lind•2y ago
My best guess is that its because the findMany can return empty lists?
Keef
Keef•2y ago
it should have a union type normally usually Model | undefined or something like that
Christian Lind
Christian Lind•2y ago
this is the type im seeing
Keef
Keef•2y ago
Yeah thats correct
Christian Lind
Christian Lind•2y ago
which is my prisma model
Keef
Keef•2y ago
Sorry for findOnes its gonna be a union type A empty array is still technically valid for your bookings[] Your typing looks correct
Christian Lind
Christian Lind•2y ago
This is what i'm seeing
Keef
Keef•2y ago
You should just store it in a variable first to avoid having to index for each usage You can also just bookings.map and that should give you no issues I think its a ts rule to have to check for existence iwth indexing but this is just typescript so theres alot of ways to skin this cat
Christian Lind
Christian Lind•2y ago
thanks for the help, fairly new to TS and the T3 stack
Keef
Keef•2y ago
np np Try it with .map and it should be good to go Thinkeng
Christian Lind
Christian Lind•2y ago
No errors this time 🙂 Strange issue...
Keef
Keef•2y ago
It’s gonna be the ts rule probably But if you are transforming data and want to stick to the newer js apis .map is the way to go
cje
cje•2y ago
bookings[i] is possibly undefined because you're accessing an arbitrary array index and typescript isn't smart enough to know that it's perfectly reasonable in this case map and foreach should work but depends on exact tsconfig setup etc
Keef
Keef•2y ago
Yeah I never really use a for loop now a days ty @cje It’s not able to get the contextual information that a for loop should be able to provide but doesn’t
cje
cje•2y ago
i ran into this a few times during advent of code where sometimes for loops make the most sense
Keef
Keef•2y ago
The for item in/of loop probably should work
cje
cje•2y ago
you can use non null assertions to "solve" this but feels bad
Keef
Keef•2y ago
The for item in array loop doesn’t work? But maybe that loop isn’t applicable to the use case you had
Christian Lind
Christian Lind•2y ago
Hypothetically - how would one do this? That makes sense
Keef
Keef•2y ago
Non null asssertions can be if(!item){…} Inside those braces item is null If you stick a return in that brace it’ll type the item as the right typing for code after it In your case you should store the indexing into a variable then just do a simple if(!booking) continue For the cases where it exists you’ll have the right typings available for you as long as you shove that work code after the check But you REALLLY SHOULD just use .map/.forEach depending on if transforming data or just doing something with it Unless you have to for loop I’m on my phone rn and lazy so I didn’t do code blocks but I’ll clean it up once I get to my computer
Christian Lind
Christian Lind•2y ago
I implemented the .map method - was just curious and wanted to know what was meant with the non null assertions as a learning excercise
Keef
Keef•2y ago
Yeah that’s a good reason to Sometimes people get stuck in their ways so I was just reiterating my point to hopefully convince that someone
Christian Lind
Christian Lind•2y ago
haha, yeah people are often pretty bad at learning and accepting new methods
Want results from more Discord servers?
Add your server