Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Diesel, a Rust ORM Better solution #3

Closed
incker opened this issue Feb 24, 2020 · 4 comments
Closed

Diesel, a Rust ORM Better solution #3

incker opened this issue Feb 24, 2020 · 4 comments

Comments

@incker
Copy link

incker commented Feb 24, 2020

Hello,
I do not know where to write you, so will write you here.
I know better solution for this article

https://leward.eu/2018/09/23/diesel-rust-notes.html

#[derive(Queryable, PartialEq)]
pub struct News {
    pub id: i64,
    pub content: String,
    pub date: Option<NaiveDateTime>,
    pub title: String,
    pub cover: Cover, // Here I want to have a Cover, not a cover_id
}


#[derive(Queryable, PartialEq)]
pub struct Cover {
    pub id: i64,
    pub name: String,
    pub url: String,
}


fn get_latest_news() -> Vec<News> {
    use schema::cover;
    use schema::news;
    let connection = &*get_pooled_connection();

    news::table
        .inner_join(cover::table)
        .limit(5)
        .select((
            news::dsl::id,
            news::dsl::content,
            news::dsl::date,
            news::dsl::title,
            (cover::dsl::id, cover::dsl::name, cover::dsl::url)
        ))
        .order(news::date.desc())
        .load::<News>(connection) // To this point we get the result as a tuple.
        .expect("Error loading news") // Another panic waiting to happen!
}

Forward thank you if you will also fix article

@Leward
Copy link
Owner

Leward commented Feb 25, 2020

Hi! Thank for reading my article and commenting here!

Let me have a look and re-run the snippets when I have a bit of time.

@incker
Copy link
Author

incker commented Feb 28, 2020

Also I want to admit to that article that no need to make always copy or clone as rust can destruct nearly everything like

let (num1 num2) = (1, 2);

or

let MyStruct { data: my_struct_data } = my_struct;
println!("{}", my_struct_data );

Even function input argument can be a destructor.
It allows not copy data but give data in owning

@Leward
Copy link
Owner

Leward commented Mar 22, 2020

I had a quick look a sample code a gave it a go. What you are suggesting does work, which is a good thing.

As to, which solution is better (besides my use of clone() out of laziness), it depends on what you prefer (my humble opinion):

  • The solution I gave allows not to have to select which column to select, hower it needs an extra step to step to to build News
  • Your solution requires to pass all the required fields in the query. Given this is validated by the compiler it's kind of nice.

I've added your suggestion at the end of the article. Thank you for having a look into this 🙏

@incker
Copy link
Author

incker commented Mar 23, 2020

Thank you too, for paying attention!)

@incker incker closed this as completed Mar 23, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants