Typing Prisma Wrapper Function

public async getUser(
{ provider, accountId }: Partial<{ provider: string; accountId: string }>,
include?: Prisma.UserInclude
) {
return await this.prisma.user.findFirst({
where: {
accounts: {
some: {
accountId,
provider,
},
},
},
include: include,
});
}
public async getUser(
{ provider, accountId }: Partial<{ provider: string; accountId: string }>,
include?: Prisma.UserInclude
) {
return await this.prisma.user.findFirst({
where: {
accounts: {
some: {
accountId,
provider,
},
},
},
include: include,
});
}
How can I type the return value of this to reflect what is passed into include?
11 Replies
Rhys
Rhys15mo ago
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
Hycord | @ When Replying
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?
Rhys
Rhys15mo ago
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
Hycord | @ When Replying
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
Rhys
Rhys15mo ago
Ah, is it a monorepo?
Rhys
Rhys15mo ago
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
Rhys
Rhys15mo ago
I just make a DB package and put all the calls in there, then just do: getUser() getUserWithServerSettings() etc
Rhys
Rhys15mo ago
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
Hycord | @ When Replying
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?
Hycord | @ When Replying
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...