Prisma: filter include statement by nullable relation, exclude when null

I have the following code, being used to get a cart on a user session.
type CartType = Cart & { items: (Item & { product: Product })[] };

const getCart = async (ctx: Context): Promise<CartType> => {
if (!ctx.session || !ctx.session.user) {
throw new TRPCError({ code: "UNAUTHORIZED" });
}

let cart: CartType;

if (ctx.session.cartId) {
cart = await ctx.prisma.cart.findUniqueOrThrow({
where: {
id: ctx.session.cartId
},
include: {
items: {
where: {
product: {
isNot: null,
},
},
include: {
product: true,
},
}
}
});
} else {
cart = await ctx.prisma.cart.create({
data: {},
include: {
items: {
where: {
product: {
isNot: null,
},
},
include: {
product: true,
},
},
},
});
}

return cart;
}
type CartType = Cart & { items: (Item & { product: Product })[] };

const getCart = async (ctx: Context): Promise<CartType> => {
if (!ctx.session || !ctx.session.user) {
throw new TRPCError({ code: "UNAUTHORIZED" });
}

let cart: CartType;

if (ctx.session.cartId) {
cart = await ctx.prisma.cart.findUniqueOrThrow({
where: {
id: ctx.session.cartId
},
include: {
items: {
where: {
product: {
isNot: null,
},
},
include: {
product: true,
},
}
}
});
} else {
cart = await ctx.prisma.cart.create({
data: {},
include: {
items: {
where: {
product: {
isNot: null,
},
},
include: {
product: true,
},
},
},
});
}

return cart;
}
This results in the following error:
Type 'Cart & { items: (Item & { product: Product | null; })[]; }' is not assignable to type 'CartType'.
Type 'Cart & { items: (Item & { product: Product | null; })[]; }' is not assignable to type '{ items: (Item & { product: Product; })[]; }'.
Types of property 'items' are incompatible.
Type '(Item & { product: Product | null; })[]' is not assignable to type '(Item & { product: Product; })[]'.
Type 'Item & { product: Product | null; }' is not assignable to type 'Item & { product: Product; }'.
Type 'Item & { product: Product | null; }' is not assignable to type '{ product: Product; }'.
Types of property 'product' are incompatible.
Type 'Product | null' is not assignable to type 'Product'.
Type 'null' is not assignable to type 'Product'.ts(2322)
Type 'Cart & { items: (Item & { product: Product | null; })[]; }' is not assignable to type 'CartType'.
Type 'Cart & { items: (Item & { product: Product | null; })[]; }' is not assignable to type '{ items: (Item & { product: Product; })[]; }'.
Types of property 'items' are incompatible.
Type '(Item & { product: Product | null; })[]' is not assignable to type '(Item & { product: Product; })[]'.
Type 'Item & { product: Product | null; }' is not assignable to type 'Item & { product: Product; }'.
Type 'Item & { product: Product | null; }' is not assignable to type '{ product: Product; }'.
Types of property 'product' are incompatible.
Type 'Product | null' is not assignable to type 'Product'.
Type 'null' is not assignable to type 'Product'.ts(2322)
The items all need a product so that i can calculate a price. Unfortunately I have to work off an older schema as this is a rewrite of a site that was previously written using ASP.NET. The previous developer used the Item model for multiple purposes hence why it doesn't always correlate to a product, and instead sometimes to an order. I understand the problem here but cannot reach a solution that doesn't involve manually casting to the non-nullable type, which I'd like to try and avoid.
0 Replies
No replies yetBe the first to reply to this messageJoin
Want results from more Discord servers?
Add your server