Rust Diesel 一对一关系

Rust Diesel one to one relationship

嘿,我正在为 return 用户创建一个 api 和他们的个人资料

我有两个 table 来自两个 单独的数据库 ,用户和配置文件

fn handle(
        &mut self,
        query_strings: SearchUsersQueryStrings,
        _: &mut SyncContext<Self>,
    ) -> Self::Result {
        let gateway_conn: &PgConnection = &self.1.get().unwrap();
        let own_conn: &PgConnection = &self.0.get().unwrap();

        let pattern = format!("%{}%", query_strings.username);

        let found_users = users
            .filter(username.like(pattern))
            .get_results::<User>(gateway_conn)?;

        let profile = Profile::belonging_to(&found_users)
            .load::<Profile>(own_conn)?
            .grouped_by(&found_users);
 

        let data = found_users.into_iter().zip(profile).collect();

        Ok(data)
    }

这里的坏处是数据类型结果是,很难解析

[
    [
        {
            "id": 22,
            "username": "412212512",
            "email": "1231q1222122@gmail.com",
            "avatar": null,
            "created_at": "2022-02-21T09:31:29.855851"
        },
        [
            {
                "id": 3,
                "user_id": 22,
                "status": "qqq",
                "description": "xxx",
                "created_at": "2022-03-07T22:53:17.491532",
                "updated_at": null,
                "deleted_at": null
            }
        ]
    ],
    [
        {
            "id": 25,
            "username": "1412drew212512",
            "email": "1231q11srew222122@gmail.com",
            "avatar": null,
            "created_at": "2022-02-21T10:37:04.588795"
        },
        []
    ],
]

但我想要这样的东西:

[
            {
                "id": 22,
                "username": "1412212512",
                "email": "1231q1222122@gmail.com",
                "avatar": null,
                "created_at": "2022-02-21T09:31:29.855851",
                "profile": {
                    "id": 3,
                    "user_id": 22,
                    "status": "qqq",
                    "description": "xxx",
                    "created_at": "2022-03-07T22:53:17.491532",
                    "updated_at": null,
                    "deleted_at": null
                
                },
           ....
           
]

如果我在配置文件查询中使用 load func 它将 return Vec<Profile> 但每个用户都有一个 Profile 记录,如果我不使用 load 并使用first 而不是我不能在它上面使用 grouped_by

像这样创建一个名为 UserAPI 的结构:

pub struct UserAPI {
    #[serde(flatten)]
    pub user: User,

    pub profile: Profile,
}

然后在压缩数据后执行此操作:

fn handle(
        &mut self,
        query_strings: SearchUsersQueryStrings,
        _: &mut SyncContext<Self>,
    ) -> Self::Result {
        let gateway_conn: &PgConnection = &self.1.get().unwrap();
        let own_conn: &PgConnection = &self.0.get().unwrap();

        let pattern = format!("%{}%", query_strings.username);

        let found_users = users
            .filter(username.like(pattern))
            .get_results::<User>(gateway_conn)?;

        let profile = Profile::belonging_to(&found_users)
            .load::<Profile>(own_conn)?
            .grouped_by(&found_users);
 

        let data = found_users.into_iter().zip(profile).collect();

        let users_with_profile: Vec<UserAPI> = data
                .into_iter()
                .map(|(user, profile)| {
    
                    UserAPI {
                        user,
                        profile,
                    }
                })
                .collect();

        Ok(users_with_profile)
    }