#Better way of implementing Queries at controller level? (im using Prisma)

17 messages · Page 1 of 1 (latest)

mighty ridge
tacit ginkgo
#

Better... How? What are you looking to optimize here?

mighty ridge
#

I mean like, is this the typical way to implement querying when working with NestJS and prisma ?

#

Is it like the cleanest way i mean

#

If thats the cleanest way to set it up, I would probable hide the logic above in a function and just call it in the controller, just to keep it s bit cleaner

mighty ridge
tacit ginkgo
#

Personally I'd probably pass the entire query object on to the service and do any manipulation there. Thin controllers, fat services.

I probably would use a function to abstract out the logic as well. I don't know how prisma handles undefined, but if it just ignores it then you could simply do filters = { city, propertyType, price } with only the price have any assignment logic around it

mighty ridge
#

Gotcha, i was also planning out to just pass this logic to the service and thats all

#

I also like to keep my controllers lean

tacit ginkgo
#

You could also get all the query vals at once using @Query() query: Type

mighty ridge
#

Oh so like with the help of an interface ?

tacit ginkgo
#

Interface, DTO class that can be validated, Typescript type, yeah

mighty ridge
#

Gotcha

#

Thank you kind sir

mighty ridge
#

Just looked it up, and it days that prisma ignores “undefined” fields

#

So i guess a good approach would be to create an Type for the query param