Typing Prisma Wrapper Function
How can I type the return value of this to reflect what is passed into
include
?11 Replies
So you’d use typescript generics however I’m going to recommend with Prisma to just write extra functions instead of trying to wrap one
From my experience the DX of doing all of the generics sucks and sometimes screws up the types
I end up with a class with a bunch of inconsistent functions and it also requires me to make many fetches
Should I just make my prisma instance accessible via my client (not publishing this) and do everything raw?
Like make your Prisma instance available on the web client side? I’m not even sure if it’d work if you tried to do that
No
This is a discord bot and I’m writing the client and implementation of it
It has a db for storing various user information
Ah, is it a monorepo?
Yep
https://github.com/AnswerOverflow/AnswerOverflow/tree/main/packages/db this might be a helpful reference, it’s how I handle this in my Discord bot - exact same setup pretty much
GitHub
AnswerOverflow/packages/db at main · AnswerOverflow/AnswerOverflow
Indexing Discord Help Channel Questions into Google - AnswerOverflow/packages/db at main · AnswerOverflow/AnswerOverflow
I just make a DB package and put all the calls in there, then just do:
getUser()
getUserWithServerSettings()
etc
If you wanna go down the Prisma generics rabbit hole, here’s what I tried when I went down it:
https://github.com/prisma/prisma/discussions/6863
https://www.prisma.io/docs/concepts/components/prisma-client/advanced-type-safety/operating-against-partial-structures-of-model-types
GitHub
How to write a generic method in typescript? · prisma prisma · Disc...
import { PrismaClient } from '@prisma/client'; export const client = new PrismaClient(); /** * Delete * @param {string} id * @param {string} db * @returns {boolean} */ export function delet...
Prisma
Operating against partial structures of your model types
This page documents various scenarios for using the generated types from the Prisma namespace
So I should be writing a function for every scenario?
getUserWithBadges
getUserWithBadgesAndAccounts
Because otherwise I’m going to be making many db calls when it should really just be one
Right?
https://dev.to/natelindev/typescript-function-return-type-based-on-parameters-87d
This seems promising
DEV Community
Typescript function return type based on parameters
Typescript is all fun and games until you want some behaviour based on runtime values, recently I enc...