This repository was archived by the owner on Mar 21, 2025. It is now read-only.
How to optional pass a required param in update? #124
Answered
by
Brendonovich
dohaicuong
asked this question in
Q&A
-
Hi team, I'm trying to build a simple crud api. with the prisma model
the update input
How can I optionally update the required title field ?
|
Beta Was this translation helpful? Give feedback.
Answered by
Brendonovich
Aug 14, 2022
Replies: 1 comment
-
There's many ways to do it, but I would do something like the following: // Array instead of Vec to avoid memory allocation
let params = [
// If title is provided, will be Some(SetParam)
// If title is not provided, will be None
input.title.map(post::title::set),
// Will always be Some(SetParam)
Some(post::content::set(input.content))
];
// Filter array to remove None values and extract value inside Some(_)
let params = params
.into_iter()
.filter_map(|p| p)
.collect();
let updated_post_result = db
.post()
.find_unique(post::id::equals(post_id))
.update(params)
.exec()
.await?; |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
Brendonovich
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
There's many ways to do it, but I would do something like the following: