JiinX
JiinX
DTDrizzle Team
Created by JiinX on 3/17/2024 in #help
Need help with relations
Im not sure why this is throwing an error but it won't let me make my query

const token = await db.query.emailTokens.findFirst({
where: (token, { eq }) => eq(token.token_hash, tokenId)
});

if (!token || token.expiresAt < new Date()) {
return message(form, 'Token is invalid or has expired. Please request a new one.', {
status: 404
});
}


const user = await db.query.users.findFirst({
with: {
emailTokens: {
where: (emailTokens, { eq }) => eq(emailTokens.userId, token.userId)
}
}
});


Object literal may only specify known properties, and 'where' does not exist in type '{ columns?: { id?: boolean | undefined; userId?: boolean | undefined; token_hash?: boolean | undefined; expiresAt?: boolean | undefined; type?: boolean | undefined; } | undefined; with?: {} | undefined; extras?: Record<...> | ... 1 more ... | undefined; }'.ts(2353)

const token = await db.query.emailTokens.findFirst({
where: (token, { eq }) => eq(token.token_hash, tokenId)
});

if (!token || token.expiresAt < new Date()) {
return message(form, 'Token is invalid or has expired. Please request a new one.', {
status: 404
});
}


const user = await db.query.users.findFirst({
with: {
emailTokens: {
where: (emailTokens, { eq }) => eq(emailTokens.userId, token.userId)
}
}
});


Object literal may only specify known properties, and 'where' does not exist in type '{ columns?: { id?: boolean | undefined; userId?: boolean | undefined; token_hash?: boolean | undefined; expiresAt?: boolean | undefined; type?: boolean | undefined; } | undefined; with?: {} | undefined; extras?: Record<...> | ... 1 more ... | undefined; }'.ts(2353)
export const users = pgTable('users', {
id: text('id').primaryKey(),
email: text('email').unique().notNull().unique(),
hashed_password: text('hashed_password'),
role: userRoleEnum('role'),
status: userStatusEnum('status')
});

export const emailTokens = pgTable('email_tokens', {
id: serial('id').primaryKey(),
userId: text('userId'),
token_hash: text('token').unique().notNull(),
expiresAt: timestamp('expiresAt').notNull(),
type: emailTokenEnum('type')
});

export const usersRelations = relations(users, ({ one }) => ({
settings: one(settings, {
fields: [users.id],
references: [settings.user_id]
}),
emailTokens: one(emailTokens, {
fields: [users.id],
references: [emailTokens.userId]
})
}));
export const users = pgTable('users', {
id: text('id').primaryKey(),
email: text('email').unique().notNull().unique(),
hashed_password: text('hashed_password'),
role: userRoleEnum('role'),
status: userStatusEnum('status')
});

export const emailTokens = pgTable('email_tokens', {
id: serial('id').primaryKey(),
userId: text('userId'),
token_hash: text('token').unique().notNull(),
expiresAt: timestamp('expiresAt').notNull(),
type: emailTokenEnum('type')
});

export const usersRelations = relations(users, ({ one }) => ({
settings: one(settings, {
fields: [users.id],
references: [settings.user_id]
}),
emailTokens: one(emailTokens, {
fields: [users.id],
references: [emailTokens.userId]
})
}));
1 replies
DTDrizzle Team
Created by JiinX on 3/5/2024 in #help
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" }; }
2 replies