#custom prisma method not recognized by TS
1 messages · Page 1 of 1 (latest)
At this time i'm really tired and already shut down for today
So i cant share any code atm from it, will have to do that tomorow
But this custom method defined directly in the prisma service
I'm going to need some help with this as it been days
I am confident that when you use .$extends() on the client that the return value will be the correct type
so you need to either extract that type or reference the created instance directly.
correct in the prismaService constructor i got this
this.$extends({
model: {
$allModels: {
async findById<T, A>(
this: T,
id: Prisma.Exact<A, Prisma.Args<T, 'findUniqueOrThrow'>['where']>,
): Promise<Prisma.Result<T, A, 'findUniqueOrThrow'>> {
const context = Prisma.getExtensionContext(this)
const result = await (context as any).findUniqueOrThrow({ where: { id } })
return result
},
},
},
})
Yeah well I dont see how thats going to work because typescript has no way of knowing that you extended the prisma client and changed it.
const client = new PrismaClient()
.$extends({...})
client
.users
.findById(...)
so you could use typeof client wherever you inject it to get the correct type
but then again, and we discussed this last week, I dont consider this a solution to your "problem"
if you want an active record then just go typeorm or mikroorm
would't it be possible to create a custom interface define it
and then as customPrisma on the this.$extends({})?
everything is possible but you are ducttaping something that shouldnt be ducttaped
will this come with the types the correct way then?
i really need some guide as my head is overloaded at this point
I literally gave you an example?
But then again... I wouldnt do this and im done discussing it
now i read uppon it, yeah i now understand what you where saying
i have to apologize for being brain dead sometimes 
i can be slow to pickup things i struggle to understand, you should know this by now already from TPH 😉
it seems even prisma uses this method even in their docs
so how would your suggestion apply in a situation like this?
@Injectable()
export class DatabaseService extends PrismaClient implements OnModuleInit {
private readonly logger = new Logger(DatabaseService.name)
constructor() {
super()
// adds a prisma.model.findById(<number>) method
// how can i create a out of this and be exported?
this.$extends({
model: {
$allModels: {
async findById<T, A>(
this: T,
id: Prisma.Exact<A, Prisma.Args<T, 'findUniqueOrThrow'['where']>,
): Promise<Prisma.Result<T, A, 'findUniqueOrThrow'>> {
const context = Prisma.getExtensionContext(this)
const result = await (context as any).findUniqueOrThrow({ where: { id } })
return result
},
},
},
})
}
}
@Injectable()
export class AccountService {
readonly logger = new Logger(AccountService.name)
constructor(
private readonly emailService: EmailService,
private config: ApiConfigService,
private readonly tokenService: TokenService,
// use this export type here?
private readonly prisma: DatabaseService,
) {}
async findById(id: number, includeUser = false): Promise<AccountUserRolesBundle | Account> {
if (!includeUser) {
const account = (await this.prisma.account.findById(id)) as Account
if (!account) throw new NotFoundException()
return account as Account
} else {
const account = await this.prisma.account.findUnique({
where: { id },
include: {
user: {
include: { roles: { select: { role: { select: { name: true, priority: true, permission: true } } } } },
},
},
})
if (!account) throw new NotFoundException()
return account
}
}
}