Skip to content
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
Discussion options

You must be logged in to vote

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?;

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by Brendonovich
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants