Thanks for the clarification @Tobbe. I need more help .
I have almost 0 experience in prisma and backend stuff and hence being slow.
I was able to create the schema in prisma and do the migration.
My Schema looks like
model User {
id Int @id @default(autoincrement())
email String @unique
hashedPassword String @default("")
salt String @default("")
resetToken String?
resetTokenExpiresAt DateTime?
favoriteMovies FavoriteMovieOnUser[]
}
model FavoriteMovie {
id Int @id @default(autoincrement())
name String @unique
users FavoriteMovieOnUser[]
}
model FavoriteMovieOnUser {
id Int @id @default(autoincrement())
favoriteMovieId Int
favoriteMovie FavoriteMovie @relation(fields: [favoriteMovieId], references: [id])
userId Int
user User @relation(fields: [userId], references: [id])
@@unique([userId, favoriteMovieId])
}
After creating the schema, I performed the migration via yarn rw prisma migrate dev
.
Next, I created three SDLs with.
yarn rw g sdl FavoriteMovie
yarn rw g sdl User
yarn rw g sdl FavoriteMovieOnUser
This in turn created the sdl files as well as the service scripts.
Since the service scripts are already generated, I then utilized the graphql playground in order to play with the database and familiarize myself with it. I am able to create and query stuff from individual tables.
However, one thing I am totally unsure of is creating new entry relationship between tables by utilizing the combination of sdl and service scripts. The default generated ones are very useful but only when you want to make changes to individual tables. Here are the things I am not sure of:
- I am not clear on how to create relationship between table entries. Let’s say I have a User row and a FavoriteMovie row, how can I created a relationship between these two.
- Or I want to create a relationship between a User and FavoriteMovie that doesn’t exist yet. What’s the right way of doing this.
Lastly, I am aware that there are ways like pointed here but I wanted to do it the RW WayTM. The same way RW does it in the service and sdl scripts. For example for updating a favorite movie entry we have:
Inside sdl files:
input UpdateFavoriteMovieInput {
name: String
}
type Mutation {
updateFavoriteMovie(
id: Int!
input: UpdateFavoriteMovieInput!
): FavoriteMovie! @skipAuth}
}
And then inside service scripts:
export const updateFavoriteMovie = ({ id, input }) => {
return db.favoriteMovie.update({
data: input,
where: { id },
})
}
Thanks and sorry for going verbose here.