Handling duplicates with trpc

Lets say I have an User in Prisma database that has a Coin[] array in his model. Currently I'm doing something like this to display table of all items:
// Create an item
.mutation(async ({ ctx, input }) => {
try {
await ctx.prisma.coin.create({data: ...})}

// Then get all the items
getAll: publicProcedure.query(async ({ ctx }) => {
try {
return await ctx.prisma.coin.findMany({
where: {userId: ctx.session?.user?.id}
// Create an item
.mutation(async ({ ctx, input }) => {
try {
await ctx.prisma.coin.create({data: ...})}

// Then get all the items
getAll: publicProcedure.query(async ({ ctx }) => {
try {
return await ctx.prisma.coin.findMany({
where: {userId: ctx.session?.user?.id}
It works but the problem is that this way, person can add same items multiple times to this array since they all have unique ids. But if I try to make id not unique then if one user adds an item to his array, the rest cant. So im looking for a better way to handle this. Prisma schema for context:
model Coin {
id String @id @default(cuid())
createdAt DateTime @default(now())
name String
other stuff but not important
}
model User {
id String @id @default(cuid())
name String?
email String? @unique
emailVerified DateTime?
image String?
accounts Account[]
sessions Session[]
coins Coin[]
}
model Coin {
id String @id @default(cuid())
createdAt DateTime @default(now())
name String
other stuff but not important
}
model User {
id String @id @default(cuid())
name String?
email String? @unique
emailVerified DateTime?
image String?
accounts Account[]
sessions Session[]
coins Coin[]
}
11 Replies
Amos
Amos•2y ago
You can make the combination of Coin's id and a User's id unique maybe? Not sure if I understand the question Or make the name unique if you want people to only be able to create a coin that doesn't already exist? 🤔
noctate
noctate•2y ago
I thought about this but the problem is that Im currently displaying a table of coins and I want users to be able to add them to their favorites. But if one user adds a Bitcoin for example the others cant which sucks
Amos
Amos•2y ago
Ah I get what you are doing
noctate
noctate•2y ago
is there a way to write something like this in prisma?
ctx.prisma.user.coins.findmany where userId is the one that currently is clicking
ctx.prisma.user.coins.findmany where userId is the one that currently is clicking
cje
cje•2y ago
You can make a combination of fields unique in Prisma
Amos
Amos•2y ago
You can add a table in between
cje
cje•2y ago
Prisma
Unique constraints and indexes with Prisma and MySQL
Learn how to configure unique constraints and indexes with Prisma and MySQL by following the step-by-step instructions in this practical guide.
cje
cje•2y ago
Look for @@unique
noctate
noctate•2y ago
Ill check it, thanks guys
Amos
Amos•2y ago
model Coin {
id String @id @default(cuid())
favouritedCoins FavouriteCoin[]
}

model FavouriteCoin {
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
userId String
coin Coin @relation(fields: [coinId], references: [id], onDelete: Cascade)
coinId String

@@unique([userId, coinId])
}

model User {
id String @id @default(cuid())
favouriteCoins FavouriteCoin[]
}
model Coin {
id String @id @default(cuid())
favouritedCoins FavouriteCoin[]
}

model FavouriteCoin {
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
userId String
coin Coin @relation(fields: [coinId], references: [id], onDelete: Cascade)
coinId String

@@unique([userId, coinId])
}

model User {
id String @id @default(cuid())
favouriteCoins FavouriteCoin[]
}
Idk maybe something like this if I get what you are trying to do
noctate
noctate•2y ago
ill try it out, thanks!
Want results from more Discord servers?
Add your server