Prisma: Finding users in the same chat room - node.js

model Chatroom {
room_id Int #id #default(autoincrement())
Participant Participant[]
Message Message[]
}
model Participant {
participant_id Int #id #default(autoincrement())
roomId Chatroom #relation(fields: [chatroomRoom_id], references: [room_id])
chatroomRoom_id Int
user_id User #relation(fields: [userId], references: [id])
userId Int
}
Is there a way to find if two target participants have a chatroom together in the Prisma client?
I've tried querying both users rooms and filtering it where I can find the room the two users are in exist but I want to know if there's a way to do this in prisma findFirst.

Related

How is the way for me to transfer data from an ITEM that is related to my salesman in the database?

I have this relationship in my schema:
generator client {
provider = "prisma-client-js"
}
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}
model Salesman {
id String #id #default(uuid())
username String #unique
password String
contact String #unique
item Item[]
##map("salesman")
}
model Buyer {
id String #id #default(uuid())
username String #unique
password String
##map("buyer")
}
model Item {
id String #id #default(uuid())
item_name String
float_item String
price String
type_skin String
salesman Salesman? #relation(fields: [id_salesman], references: [id])
id_salesman String?
##map("item")
}
i try like this
import { prisma } from "../../../../database/prismaClient" interface IRequest{ username: String, contact: String, id: String } export class GetItemUseCase { async execute({ username, contact, id}: IRequest ){ const info = await prisma.item.findFirst({ where:{id}, select:{ salesman { username, contact, }, } }) } } I tried something like this.
and I'm trying to make a way where when choosing an ITEM, I get the data from that salesman.
but I have no idea how.
What better way to inform?
Sorry, I'm a beginner.
I've tried verifying the relationships and I've tried searching the database.

prisma - How to impose condition to the last record of a many to many relationship

My schema:
model Order {
id Int #id #default(autoincrement())
OrderStatusLogs OrderStatusLog[]
}
model OrderStatusLog {
id Int #id #default(autoincrement())
status OrderStatus
createdAt DateTime #default(now())
updatedAt DateTime #updatedAt
orderId Int
Order Order #relation(fields: [orderId], references: [id])
}
enum OrderStatus {
ONE
TWO
THREE
}
The important OrderStatusLog to me is the last one, a practical example of the query I want to achieve:
Select all orders which the last status is X, take 10 from cursor 5
Observation: I'm using pagination, so if I needed to do 2 queries I would prefer to write I raw query instead.

How can I check if a relation is established/connected?

I have this prisma schema which looks like this:
model UserProfile {
createdAt DateTime #default(now())
updatedAt DateTime #updatedAt
id String #id #default(cuid())
username String #unique
avatar String #default("defaultAvatar")
banner String #default("defaultBanner")
name String #default("A New Gem User")
verified Boolean #default(false)
gemStaff Boolean #default(false)
gender Gender[]
location String?
verifiedDescription String?
bio String?
author User #relation(fields: [authorId], references: [id])
authorId String #unique
posts Post[] #relation("UserProfilePosts")
postGroups PostGroup[] #relation("UserProfilePostGroups")
giveaways Giveaway[]
categories UserCategory[]
following UserProfile[] #relation("UserFollows")
followers UserProfile[] #relation("UserFollows")
comments Comment[] #relation("UserProfileComments")
subComments SubComment[] #relation("UserProfileSubComments")
likedPosts Post[]
likedComments Comment[] #relation("UserProfileLikesComments")
likedSubComments SubComment[] #relation("UserProfileLikesSubComments")
groupsJoined UserGroup[]
blocking UserProfile[] #relation("UserBlocked")
blockedBy UserProfile[] #relation("UserBlocked")
favoriteGroups FavoriteGroup[] #relation("UserProfileFavoritedGroups")
views Int #default(0)
}
model Post {
createdAt DateTime #default(now())
updatedAt DateTime #updatedAt
public Boolean #default(true)
id String #id #default(cuid())
author UserProfile #relation("UserProfilePosts", fields: [authorId], references: [id])
authorId String
favoritedGroupsAdded FavoriteGroup[]
content String
views Int #default(0)
likedBy UserProfile[]
tags PostTag[]
comments Comment[] #relation("PostComments")
subComments SubComment[] #relation("PostSubComments")
group PostGroup? #relation("PostGroups", fields: [groupId], references: [id])
groupId String?
}
When you like something, you create a relation from UserProfile to Post. When you dislike something, you just delete the relation. Does anyone know how can I check a relation between one UserProfile to one Post so I can figure out if someone liked a post?

prisma query postgresql into nestJs

I have a problem as following:
schema:
model company {
id Int #id #default(autoincrement())
name String?
services company_service[]
...
}
model company_service {
companyId Int #map("company_id")
serviceId Int #map("service_id")
company company? #relation(fields: [companyId], references: [id])
service service? #relation(fields: [serviceId], references: [id])
##id([companyId, serviceId])
}
model service {
id Int #id #default(autoincrement())
name String?
companies company_service[]
jobs job[]
}
model job {
id Int #id #default(autoincrement())
customerId Int? #map("customer_id")
serviceId Int? #map("service_id")
addressId Int? #map("address_id")
status Int? #default(1) // 1: Available, 2: In Progress, 3:Cancel ,4: Completed,
customer customer? #relation(fields: [customerId], references: [id])
service service? #relation(fields: [serviceId], references: [id])
}.
I need to execute the query:
SELECT company_id, count(j.status) as numberOfWorkComplete
FROM company_service sv, job j
WHERE sv.service_id = j.service_id and j.status = 4
GROUP BY company_id
I have the result of the above query but is there any way i can still get that result without having to write sql? thanks
Currently a raw query is the only way to do this. We have a request for the same here so it would be great if you could add a 👍 to this so that we can look at the priority.

get posts by tag Id using Prisma.js

How can I get posts by filtering tagId?
I test this code but it doesn't work:
I get all of the posts without filtering!
prisma.post.findMany({
include:
{
Tags:
{
where: { TagId: tagId },
include:
{
Tag: true
}
}
},
})
schema.prisma:
model Post {
id Int #id #default(autoincrement())
title String
tags PostTags[]
}
model PostTags {
id Int #id #default(autoincrement())
post Post? #relation(fields: [postId], references: [id])
tag Tag? #relation(fields: [tagId], references: [id])
postId Int?
tagId Int?
}
model Tag {
id Int #id #default(autoincrement())
name String #unique
posts PostTags[]
}
how can I fix the problem?
You would need to filter it inside the main query and not in include. include is only for fetching relations and then filtering inside them, it will not affect the main query.
Your final query would look like:
await prisma.post.findMany({
where: { tags: { some: { tag: { id: 1 } } } }
})
You can read more about querying for relations here.

Resources