P
Prisma•4mo ago
Arthur

Return Type With Relations

Hello there, I have a quick question about return type with relation. If I have this sort of query:
this.prismaService.loan.findFirst({
where,
include: {
customer: true
},
});
this.prismaService.loan.findFirst({
where,
include: {
customer: true
},
});
So should Prisma's type have a customer as a return type?
import { Loan } from '@prisma/client';
import { Loan } from '@prisma/client';
If it is, why does it always crying that the customer is not a part of the type of loan?
4 Replies
RaphaelEtim
RaphaelEtim•4mo ago
Hi @Arthur 👋 When you use include option in a query, the return type should indeed include the related fields specified in the include clause. Can you share how you are using it in your code?
Arthur
ArthurOP•4mo ago
Sure,
import { Loan, Prisma } from '@prisma/client';

async findLoanDetails<T extends Prisma.LoanWhereInput>(where: T): Promise<Loan> {
return this.prismaService.loan.findFirst({
where,
include: {
customer: true,
},
});
}
import { Loan, Prisma } from '@prisma/client';

async findLoanDetails<T extends Prisma.LoanWhereInput>(where: T): Promise<Loan> {
return this.prismaService.loan.findFirst({
where,
include: {
customer: true,
},
});
}
Usage of the method:
const result = await this.loanRepository.findLoanDetails({ id: loanId });
console.log(result.customer);
const result = await this.loanRepository.findLoanDetails({ id: loanId });
console.log(result.customer);
The error:
TS2339: Property customer does not exist on type
TS2339: Property customer does not exist on type
It's only works when I create the type by myself:
import { Loan, Customer } from '@prisma/client';

export type FindLoanDetailsEntity = Loan & {
customer: Customer;
};
import { Loan, Customer } from '@prisma/client';

export type FindLoanDetailsEntity = Loan & {
customer: Customer;
};
RaphaelEtim
RaphaelEtim•4mo ago
Thank you for sharing. To address your issue, you can use Prisma Validator API to resolve this issue. Look at my example implementation
const loanWithCustomerFields = Prisma.validator<Prisma.LoanInclude>()({
customer: true,
});

type LoanWithCustomer = Prisma.LoanGetPayload<{ include: typeof loanWithCustomerFields }> | null;

async findLoanDetails<T extends Prisma.LoanWhereInput>(where: T): Promise<LoanWithCustomer> {
return this.prismaService.loan.findFirst({
where,
include: loanWithCustomerFields,
});
}
const loanWithCustomerFields = Prisma.validator<Prisma.LoanInclude>()({
customer: true,
});

type LoanWithCustomer = Prisma.LoanGetPayload<{ include: typeof loanWithCustomerFields }> | null;

async findLoanDetails<T extends Prisma.LoanWhereInput>(where: T): Promise<LoanWithCustomer> {
return this.prismaService.loan.findFirst({
where,
include: loanWithCustomerFields,
});
}
You can now use the findLoanDetails method
const result = await this.loanRepository.findLoanDetails({ id: loanId });
if (result) {
console.log(result.customer); // This should now be correctly typed
} else {
console.log('No loan found');
}
const result = await this.loanRepository.findLoanDetails({ id: loanId });
if (result) {
console.log(result.customer); // This should now be correctly typed
} else {
console.log('No loan found');
}
Prisma validator | Prisma Documentation
The Prisma validator is a utility function that takes a generated type and returns a type-safe object which adheres to the generated types model fields.
Arthur
ArthurOP•4mo ago
Thank you for your answer, I'll read more about this include your approach.
Want results from more Discord servers?
Add your server