Oyelowo / oyestack

Foundation for highly scalable distributed web application with `Rustlang` for apps, `Typescript` and Kubernetes for Infra
https://codebreather.com
Apache License 2.0
0 stars 0 forks source link

Surrealdb Orm: Implement LinkOne and LinkMany and prevent it from self-shooting in the feet #177

Closed Oyelowo closed 1 year ago

Oyelowo commented 1 year ago

Purpose

e.g: LinkOne::from(course) or course.into() makes sure course id is returned rather than the entire course struct

#[derive(SurrealdbModel, TypedBuilder, Serialize, Deserialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
pub struct Student {
    #[serde(skip_serializing_if = "Option::is_none")]
    #[builder(default, setter(strip_option))]
    id: Option<String>,
    first_name: String,
    #[surrealdb(reference_one = "Course", skip_serializing)]
    course: LinkOne<Course>,

    #[surrealdb(reference_one = "Course", skip_serializing)]
    #[serde(rename = "lowo")]
    all_semester_courses: LinkMany<Course>,
}

#[derive(SurrealdbModel, TypedBuilder, Default, Serialize, Deserialize, Debug, Clone)]
#[surrealdb(rename_all = "camelCase")]
pub struct Course {
    #[serde(skip_serializing_if = "Option::is_none")]
    #[builder(default, setter(strip_option))]
    id: Option<String>,
    title: String,
}

fn main {
    let course: Option<Course> = DB
        .select(SurIdComplex::from_string(cx.clone().id.unwrap()))
        // .select(SurIdComplex(("Course".to_string(), cx.id.unwrap()).0))
        .await
        .unwrap();

    let course = course.as_ref().unwrap();
    // let course = &course.unwrap();
    let student = query
        .first_name("dayo".into())
        .course(course.into()) // Same as LinkOne::from(course). This makes sure courseid is returned rather than the entire course struct 
        .all_semester_courses(vec![course.into(), course.into()])
        .build();
}
Oyelowo commented 1 year ago

Done here: https://github.com/Oyelowo/modern-distributed-app-template/pull/184