Will the pictures belong to the user? Or does ever...
# orm-help
d
Will the pictures belong to the user? Or does everything belong to the user?
generator client {
 
provider = "prisma-client-js"
}
datasource db {
 
provider = "mysql"
 
url      = env("DATABASE_URL")
}
model User {
 
id         Int       @id @default(autoincrement())
 
email      String    @unique @db.VarChar(255)
 
firstname  String    @db.VarChar(255)
 
lastname   String    @db.VarChar(255)
 
created_at DateTime  @default(now())
 
updated_at DateTime?
 
posts      Post[]
 
@@map("users")
}
model Post {
 
id         Int       @id @default(autoincrement())
 
title      String    @db.VarChar(255)
 
content    String    @db.Text
 
images     Image[]
 
comments   Comment[]
 
created_at DateTime  @default(now())
 
updated_at DateTime?
 
user       User      @relation(fields: [userId], references: [id])
 
userId     Int
 
@@map("posts")
}
model Image {
 
id     Int    @id @default(autoincrement())
 
puth   String @db.VarChar(255)
 
Post   Post   @relation(fields: [postId], references: [id])
 
postId Int
 
@@map("images")
}
model Comment {
 
id      Int    @id @default(autoincrement())
 
comment String @db.Text
 
post    Post   @relation(fields: [postId], references: [id])
 
postId  Int
 
@@map("comments")
}
import { Injectable } from '@nestjs/common';
import { PrismaService } from '../prisma.service';
import { Post, Prisma } from '@prisma/client';
@Injectable()
export class PostsService {
 
constructor(private prisma: PrismaService) {}
 
async findAll(): Promise<Post[]> {
   
return this.prisma.post.findMany({
     
include: {
       
user: {
         
select: {
           
firstname: true,
           
lastname: true,
         
},
       
},
       
images: {
         
select: {
           
id: true,
           
puth: true,
         
},
       
},
       
_count: {
         
select: {
           
comments: true
         
}
       
},
     
},
   
});
 
}
}
r
@Dia TM 👋 I didn’t quite get your question. Could you explain a bit in terms of the code you’ve posted?
d
Images belongs to Posts and Users? Or just Posts? Sorry Google translate
r
As per the schema, Images belong only to Posts and not Users.
d
model Image {
 
id     Int    @id @default(autoincrement())
 
puth   String @db.VarChar(255)
 
Post   Post   @relation(fields: [postId], references: [id])
 
postId Int
 
User   User  @relation(fields: [userId], references: [id])
 
userId Int
 
@@map("images")
}
Or
model Image {
 
id     Int    @id @default(autoincrement())
 
puth   String @db.VarChar(255)
 
Post   Post   @relation(fields: [postId], references: [id])
 
postId Int
 
@@map("images")
}
r
The first one belongs to both Users and Posts, then second one only to Posts
d
Thank you very much. And what kind of link is used in the blogs, could you tell me?
Post and User?
r
Which blogs?
d
That's probably the right thing to do.
generator client {
 
provider = "prisma-client-js"
}
datasource db {
 
provider = "mysql"
 
url      = env("DATABASE_URL")
}
model User {
 
id         Int       @id @default(autoincrement())
 
email      String    @unique @db.VarChar(255)
 
firstname  String    @db.VarChar(255)
 
lastname   String    @db.VarChar(255)
 
created_at DateTime  @default(now())
 
updated_at DateTime?
 
posts      Post[]
 
images     Image[]
 
comments   Comment[]
 
@@map("users")
}
model Post {
 
id         Int       @id @default(autoincrement())
 
title      String    @db.VarChar(255)
 
content    String    @db.Text
 
images     Image[]
 
comments   Comment[]
 
created_at DateTime  @default(now())
 
updated_at DateTime?
 
user       User      @relation(fields: [userId], references: [id])
 
userId     Int
 
@@map("posts")
}
model Image {
 
id     Int    @id @default(autoincrement())
 
puth   String @db.VarChar(255)
 
post   Post   @relation(fields: [postId], references: [id])
 
postId Int
 
user   User   @relation(fields: [userId], references: [id])
 
userId Int
 
@@map("images")
}
model Comment {
 
id      Int    @id @default(autoincrement())
 
comment String @db.Text
 
post    Post   @relation(fields: [postId], references: [id])
 
postId  Int
 
user    User   @relation(fields: [userId], references: [id])
 
userId  Int
 
@@map("comments")
}