Oyelowo / surreal-orm

Powerful & expressive ORM/query-builder/static checker for raw queries/Fully Automated migration tooling , designed to offer an intuitive API, strict type-checking, novel features, & full specification support. It provides a fresh perspective in data management. Currently supports SurrealDB engine. RDMSs(PG, MYSQL etc) and others coming soon
85 stars 2 forks source link

66 auto skip serialize relate readonly fields #67

Closed Oyelowo closed 2 months ago

Oyelowo commented 2 months ago

Summary

This PR introduces a series of enhancements and cleanups to the attribute handling and serialization logic specifically focusing on improving integration with the serde library for serialization and deserialization processes. The updates include rigorous validation, error handling improvements, and more intuitive usage patterns for developers working with relational fields and serialization attributes.

Key Changes

Technical Details

This PR makes sure the serialization logic is both robust and efficient, facilitating better maintainability and future-proofing the codebase against potential serialization-related issues.

Example:


use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use surreal_orm::*;

#[derive(Node, Serialize, Deserialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
#[surreal_orm(table = user)]
pub struct User<'a> {
    pub id: SurrealId<Self, String>,
    pub name: String,
    pub created: DateTime<Utc>,
    pub company: &'a str,
    pub tags: Vec<String>,
}

#[derive(Edge, Serialize, Deserialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
#[surreal_orm(table = like)]
pub struct Like<In: Node, Out: Node> {
    pub id: SurrealSimpleId<Self>,

    #[surreal_orm(link_many = In)]
    pub r#in: LinkOne<In>,

    #[surreal_orm(link_one = Out)]
    pub out: LinkOne<Out>,

    #[surreal_orm(ty = "option<float>")]
    pub score: Option<f64>,

    #[surreal_orm(ty = "array<array<float>>")]
    pub scores_plural: Vec<Vec<f64>>,

    #[surreal_orm(nest_object = Time)]
    pub time: Time,
}
pub type CompanyLikeUser<'a> = Like<Company<'a>, User<'a>>;

#[derive(Node, Serialize, Deserialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
#[surreal_orm(table = company)]
pub struct Company<'b> {
    pub id: SurrealSimpleId<Self>,
    pub name: String,
    pub namex: &'b str,

    #[surreal_orm(link_many = "User<'b>")]
    pub users: LinkMany<User<'b>>,

    #[surreal_orm(relate(model = "CompanyLikeUser<'b>", connection = "->like->user"))]
    #[serde(skip_serializing, default)]
    pub devs: Relate<User<'b>>,
}

#[derive(Object, Serialize, Deserialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
pub struct Time {
    pub connected: DateTime<Utc>,
}

#[derive(Node, Serialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
#[surreal_orm(table = organization)]
pub struct Organization<'a> {
    pub id: SurrealSimpleId<Self>,
    pub name: String,

    #[surreal_orm(link_many = "User<'a>")]
    pub users: LinkMany<User<'a>>,

    #[surreal_orm(nest_object = Time)]
    pub time: Time,
    pub age: u8,
}