query based on deep relations
I need help modifying the query code to properly query the chat where it has the the two members.
export const findOrCreateChat = async (fromID: number, toID: number) => {
const existingchat = await db.query.chats.findFirst({
with: { members: true },
where(fields, { and, eq }) {
// the line below needs fix.
return and(eq(chatMembers.userID, fromID), eq(chatMembers.userID, toID));
},
});
if (existingchat) {
return existingchat;
}
const newChat = await db
.insert(chats)
.values({
name: `new chat`,
})
.returning();
await db.insert(chatMembers).values([
{ chatID: newChat[0].id, userID: fromID },
{ chatID: newChat[0].id, userID: toID },
]);
return newChat[0];
};export const findOrCreateChat = async (fromID: number, toID: number) => {
const existingchat = await db.query.chats.findFirst({
with: { members: true },
where(fields, { and, eq }) {
// the line below needs fix.
return and(eq(chatMembers.userID, fromID), eq(chatMembers.userID, toID));
},
});
if (existingchat) {
return existingchat;
}
const newChat = await db
.insert(chats)
.values({
name: `new chat`,
})
.returning();
await db.insert(chatMembers).values([
{ chatID: newChat[0].id, userID: fromID },
{ chatID: newChat[0].id, userID: toID },
]);
return newChat[0];
};