Setting up relations on signin
Have some code where I need to setup a user (portal) and relate it to an app but I've been having issues matching the types and don't know how to proceed.
This is what I have so far and the issue is that when i try to check if the portal exists and then create a new one it throws a type error saying
Type 'RowList<never[]>' is not assignable to type '{ id: number; portalId: number | null; portalAppJunction: { id: number; status: "active" | "disabled" | null; portalId: number; appId: number; paymentPlan: string | null; executionRuns: number | null; executionLimit: number | null; apps: { ...; }; }[]; } | undefined' Code: if (!portalId) { console.error("No portal ID found"); return { error: "No portal ID found" }; } let portal = await db.query.portals.findFirst({ where: (portals, { eq }) => eq(portals.portalId, parseInt(portalId)), with: { portalAppJunction: { with: { apps: true, }, }, }, }); if (!portal) { // If the portal does not exist, create it and link it to an app portal = await db.insert(portals).values({ portalId: parseInt(portalId) }); }
if (!portal!.portalAppJunction) { // If the portal exists but is not linked to an app, link it to an app await db.insert(portalAppJunction).values({ portalId: portal.id, appId: appEntry[0].id }); await db.insert(tokens).values({ portalId: portal[0].id, appId: appEntry[0].id, accessToken, refreshToken, expiresIn, updatedAt: new Date(), createdAt: new Date(), }); } else { return { error: "Portal already linked to app" }; }
Type 'RowList<never[]>' is not assignable to type '{ id: number; portalId: number | null; portalAppJunction: { id: number; status: "active" | "disabled" | null; portalId: number; appId: number; paymentPlan: string | null; executionRuns: number | null; executionLimit: number | null; apps: { ...; }; }[]; } | undefined' Code: if (!portalId) { console.error("No portal ID found"); return { error: "No portal ID found" }; } let portal = await db.query.portals.findFirst({ where: (portals, { eq }) => eq(portals.portalId, parseInt(portalId)), with: { portalAppJunction: { with: { apps: true, }, }, }, }); if (!portal) { // If the portal does not exist, create it and link it to an app portal = await db.insert(portals).values({ portalId: parseInt(portalId) }); }
if (!portal!.portalAppJunction) { // If the portal exists but is not linked to an app, link it to an app await db.insert(portalAppJunction).values({ portalId: portal.id, appId: appEntry[0].id }); await db.insert(tokens).values({ portalId: portal[0].id, appId: appEntry[0].id, accessToken, refreshToken, expiresIn, updatedAt: new Date(), createdAt: new Date(), }); } else { return { error: "Portal already linked to app" }; }
1 Reply
The
portal = await db.insert()
call does not return the inserted row. You need to use db.insert().returning() if your db engine supports it or fetch the portal after inserting it