Reputation: 391
I have a mutation that changes data in the extern system (for example posts
).
From my UI I public post on external-api
and fetch this api for refresh posts after this mutation
I want save changes that make my system in extenal-api
, but it is not very important, more important is to update posts
on my site immediately after addiction newPost, without awaiting save these posts in my database
I expected that this code makes it
.mutation('post.add', {
input: z.object({ title: z.string(), text: z.string() }),
async resolve({ input }) {
const postId = await useExternApiForPublicThisPost(input);
// save change in my database, but not await response
prisma.posts.create({
data: {postId, ...input}
});
}
})
but unfortunately, after resolving this request, tRPC kill Prisma, for save data in the database need await Prisma, but it's is slow
How can return response from tRPC, but continue to execute remaining promises?
Upvotes: 1
Views: 1439
Reputation: 391
Prisma doesn't work as I expected
prisma.posts.create({
data: {postId, ...input}
});
don't start promise, promise started only after call .then()
I don't want use await prisma.post.create()
but can use prisma.post.create().then()
or prisma.$transaction([prisma.post.create()])
the correct version of initial code
.mutation('post.add', {
input: z.object({ title: z.string(), text: z.string() }),
async resolve({ input }) {
const postId = await useExternApiForPublicThisPost(input);
// save change in my database, but not await response
prisma.posts.create({
data: {postId, ...input}
}).then();
}
})
Upvotes: 1