T
tRPC

Mutation type issue

Mutation type issue

Ooutis9910/17/2022
I'm trying to mutate something like this with tRPC and Prisma
const updateColumn = trpc.project.update.useMutation();
updateColumn.mutate({ colId: list.id, cards: list.cards })
const updateColumn = trpc.project.update.useMutation();
updateColumn.mutate({ colId: list.id, cards: list.cards })
list.id is just a string but list.cards has a type of Card[] which I'm importing from a file Prisma auto-generated from my schema Then on my backend
update: protectedProcedure
.input(
z.object({
colId: z.string(),
cards: //what do I put here?
})
)
.mutation(async ({ input, ctx }) => {
const result = await ctx.prisma.column.update({
where: {
id: input.colId,
},
data: {
cards: input.cards, //type error
},
});

console.log(result, "?dbresult");
})
update: protectedProcedure
.input(
z.object({
colId: z.string(),
cards: //what do I put here?
})
)
.mutation(async ({ input, ctx }) => {
const result = await ctx.prisma.column.update({
where: {
id: input.colId,
},
data: {
cards: input.cards, //type error
},
});

console.log(result, "?dbresult");
})
I tried putting something like z.array(z.object({})) but obviously that doesn't work and it shows this error
VVolks10/18/2022
@Outis I can pinch in with what I'd do, but you either define the types manually, or you can use something like https://github.com/omar-dulaimi/prisma-zod-generator and then generate the zod schema based on the card But if the card is a simple type then just manually create it
Ooutis9910/18/2022
This is really useful but my problem has evolved even further lol My front end receives an array of objects and I just want to overwrite/update my previous array of objects inside my cards property I've tried plenty of solutions, posted on the slack prisma help channel but nothing yet and I'm really contemplating if the switch to trcp/prisma is worth it
VVolks10/18/2022
What exactly is the issue that prevents you from updating the previous array?
Ooutis9910/18/2022
I just don't understand the prisma update syntax for it
const column = await ctx.prisma.column.update({
where: {
id: input.colId,
},
data: {
cards: {},
},
});
const column = await ctx.prisma.column.update({
where: {
id: input.colId,
},
data: {
cards: {},
},
});
This is where I get to and I truly have tried so much stuff
VVolks10/18/2022
You need to update all the cards? Or you just need to update a single entity with the cards?
Ooutis9910/18/2022
I simply want to update the order of the cards So what I'm doing is reorder my cards in my front end and send it to my backend
VVolks10/18/2022
Like this?
const column = await ctx.prisma.column.update({
where: {
id: input.colId,
},
data: {
cards: CARDS_YOU_GET_FROM_FRONTEND,
},
});
const column = await ctx.prisma.column.update({
where: {
id: input.colId,
},
data: {
cards: CARDS_YOU_GET_FROM_FRONTEND,
},
});
Ooutis9910/18/2022
You would think it works like that but doesn't
VVolks10/18/2022
Are you getting any errors or?
Ooutis9910/18/2022
VVolks10/18/2022
Yeah well it says that the type is invalid Your cards you got from the frontend seems to be incorrect Can you print typeof cards[0]?
Ooutis9910/18/2022
export type Card = {
id: string
title: string
index: number
description: string | null
columnId: string
}
export type Card = {
id: string
title: string
index: number
description: string | null
columnId: string
}
This is the type It's exactly the same, I don't it's a type issue I think it has to do with how updating records works in Prisma when you also have relations
VVolks10/18/2022
Yeah I'd say this is not related to tRPC but rather prisma, their error message is horrible so I cant decrypt much more from it IS there any more info in the error?
Ooutis9910/18/2022
Argument cards: Got invalid value
[
{
id: 'cl9ego52z0001wb0k7ueqykh7',
title: 'Test 2',
index: 1,
description: null,
columnId: 'cl9eftdjm0002wbucv6rjulj0'
},
{
id: 'cl9eg8r4r0000wb0kd9j6cy17',
title: 'Test',
index: 0,
description: null,
columnId: 'cl9eftdjm0002wbucv6rjulj0'
}
]
on prisma.updateOneColumn. Provided List<Json>, expected CardUpdateManyWithoutColumnNestedInput:
type CardUpdateManyWithoutColumnNestedInput {
create?: CardCreateWithoutColumnInput | List<CardCreateWithoutColumnInput> | CardUncheckedCreateWithoutColumnInput | List<CardUncheckedCreateWithoutColumnInput>
connectOrCreate?: CardCreateOrConnectWithoutColumnInput | List<CardCreateOrConnectWithoutColumnInput>
upsert?: CardUpsertWithWhereUniqueWithoutColumnInput | List<CardUpsertWithWhereUniqueWithoutColumnInput>
createMany?: CardCreateManyColumnInputEnvelope
set?: CardWhereUniqueInput | List<CardWhereUniqueInput>
disconnect?: CardWhereUniqueInput | List<CardWhereUniqueInput>
delete?: CardWhereUniqueInput | List<CardWhereUniqueInput>
connect?: CardWhereUniqueInput | List<CardWhereUniqueInput>
update?: CardUpdateWithWhereUniqueWithoutColumnInput | List<CardUpdateWithWhereUniqueWithoutColumnInput>
updateMany?: CardUpdateManyWithWhereWithoutColumnInput | List<CardUpdateManyWithWhereWithoutColumnInput>
deleteMany?: CardScalarWhereInput | List<CardScalarWhereInput>
}
Argument cards: Got invalid value
[
{
id: 'cl9ego52z0001wb0k7ueqykh7',
title: 'Test 2',
index: 1,
description: null,
columnId: 'cl9eftdjm0002wbucv6rjulj0'
},
{
id: 'cl9eg8r4r0000wb0kd9j6cy17',
title: 'Test',
index: 0,
description: null,
columnId: 'cl9eftdjm0002wbucv6rjulj0'
}
]
on prisma.updateOneColumn. Provided List<Json>, expected CardUpdateManyWithoutColumnNestedInput:
type CardUpdateManyWithoutColumnNestedInput {
create?: CardCreateWithoutColumnInput | List<CardCreateWithoutColumnInput> | CardUncheckedCreateWithoutColumnInput | List<CardUncheckedCreateWithoutColumnInput>
connectOrCreate?: CardCreateOrConnectWithoutColumnInput | List<CardCreateOrConnectWithoutColumnInput>
upsert?: CardUpsertWithWhereUniqueWithoutColumnInput | List<CardUpsertWithWhereUniqueWithoutColumnInput>
createMany?: CardCreateManyColumnInputEnvelope
set?: CardWhereUniqueInput | List<CardWhereUniqueInput>
disconnect?: CardWhereUniqueInput | List<CardWhereUniqueInput>
delete?: CardWhereUniqueInput | List<CardWhereUniqueInput>
connect?: CardWhereUniqueInput | List<CardWhereUniqueInput>
update?: CardUpdateWithWhereUniqueWithoutColumnInput | List<CardUpdateWithWhereUniqueWithoutColumnInput>
updateMany?: CardUpdateManyWithWhereWithoutColumnInput | List<CardUpdateManyWithWhereWithoutColumnInput>
deleteMany?: CardScalarWhereInput | List<CardScalarWhereInput>
}
Some more that was missing but I don't think it's of any use I also tried something like this
const column = await ctx.prisma.column.update({
where: {
id: input.colId,
},
data: {
cards: {
set: input.cards,
},
},
});
const column = await ctx.prisma.column.update({
where: {
id: input.colId,
},
data: {
cards: {
set: input.cards,
},
},
});
But set only takes exactly one arguement, the id
VVolks10/18/2022
Hopefully someone with more knowledge will be able to help out, I can only think that the type contained in the array cards is not appropriate but I dont understand their error message...
Ooutis9910/18/2022
Well I hope so too, appreciate the help This is what I came up with
updateColumn: protectedProcedure
.input(
z.object({
colId: z.string(),
cards: z.any(),
})
)
.mutation(async ({ input, ctx }) => {
for (let i = 0; i < input.cards.length; i++) {
const card = input.cards[i] as Card;

const _card = await ctx.prisma.card.update({
where: {
id: card.id,
},
data: {
index: i,
},
});
}

console.log("input cards?", input.cards);
}),
updateColumn: protectedProcedure
.input(
z.object({
colId: z.string(),
cards: z.any(),
})
)
.mutation(async ({ input, ctx }) => {
for (let i = 0; i < input.cards.length; i++) {
const card = input.cards[i] as Card;

const _card = await ctx.prisma.card.update({
where: {
id: card.id,
},
data: {
index: i,
},
});
}

console.log("input cards?", input.cards);
}),
It works but it has to update each index individually
VVolks10/18/2022
So every card is its own entity? Its a bit different from the code you listed above But if it works great, update me if someone on the prisma side replies
Ooutis9910/18/2022
Yes it is, this is my schema
model Project {
id String @id @default(cuid())
updatedAt DateTime @updatedAt @default(now())
slug String
title String

userId String
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
columns Column[]

@@unique([slug, userId])
}

model Column {
id String @id @default(cuid())
title String

projectId String
project Project @relation(fields: [projectId], references: [id], onDelete: Cascade)
cards Card[]
}

model Card {
id String @id @default(cuid())
title String
index Int
description String?

columnId String
column Column @relation(fields: [columnId], references: [id], onDelete: Cascade)
}
model Project {
id String @id @default(cuid())
updatedAt DateTime @updatedAt @default(now())
slug String
title String

userId String
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
columns Column[]

@@unique([slug, userId])
}

model Column {
id String @id @default(cuid())
title String

projectId String
project Project @relation(fields: [projectId], references: [id], onDelete: Cascade)
cards Card[]
}

model Card {
id String @id @default(cuid())
title String
index Int
description String?

columnId String
column Column @relation(fields: [columnId], references: [id], onDelete: Cascade)
}
VVolks10/18/2022
Are the cards in the column embedded or via a reference? Not sure how Prisma works or what driver youre using Basically you can use a transaction and bulk insert all the cards and then update the column with the newly inserted cards But I cant give you the exact implementation as I am not familiar with the technology unfortunately

Looking for more? Join the community!

T
tRPC

Mutation type issue

Join Server
Recommended Posts
Can I redirect the user from inside my router?I have a query protectedProcedure, which returns an object from my prisma planetscale db What's theInfer the type of ctx for a specific procedureLet's say I had a helper function that I wanted to pass the context to, I can't use the default ContCORS in standalone serverHi, I'm trying to get CORS working in standalone server. Attempted this solution but unfortunatelyQueries break when in production on Vercel; work on localhostThe query is called like this: ```javascript const { data } = api.useQuery(["user.!getCount"], { Can't get client to workHi, I can't get my client to work I'm using Next.js but I have also tried the React method from the Request context inside middleware?Hi, Is it possible to get the request context inside a middleware somehow? I'm trying to migrate data becomes never[] when destructuring with a fallback valueCurrently it doesn't seem possible to set a fallback value on a destructured `data` property, for exSyntaxError Unexpected token u in JSON at position 0 with mutationsI'm trying to use trpc client to await a mutation and just keep getting this error. everything worksLosing the type between the client and serverHello everyone, I am new to tRPC and the magic of types in typescript so I am looking for ideas as tMobile app with tRPCWhat's the suggested way of working with tRPC when it comes to mobile apps? How do you ensure that yHow to use trpc react hooks from an external data sourceHi, I have a monorepo, nextjs and keystone cms. The cms has trpc running, and I managed to get it coNextJS & Keystone CMS, issues when connecting both in a monorepoHello everyone, so I have a monorepo with NextJS and keystone cms, both are running trpc v10beta.15Best practices for implementing an offline applicationHey there! I'm building a full stack react-native app with Expo and a tRPC backend. I'd like for thisomehow when move typed function out of the router files, frontend infer type to be any ?this is inferred corrected since they are in the same file. ``` getX: t.procedure.query(() => { retBig companies that use tRPC?Some of my fellow colleagues were wondering if there are any big companies that use tRPC and how matMigrating to V10 from V9If someone has some ideas on what I might have screwed up when trying to bump tRPC for this public sAuthentication broke after bump to v10 from v9Anyone that has any suggestions on what might have caused this? I get the following error when tryiShow a spinner when any mutation is loading?Looking to show an activity spinner in the header of our application whenever a mutation is in flighSharing schemas between server and clientIn a typical nextJs setup, what is the idiomatic way to share zod schemas between frontend and backev10 Migration interop, router doesnt have attributesHi, i've tried to migrate like stated in the docs, these are my routers. Question: Shouldn't appRout