2026-06-04 22:42:57 +02:00
|
|
|
use async_graphql::*;
|
2024-06-20 14:50:36 +02:00
|
|
|
/// It is easier to track each type alias if this file is located on the top-level directory (here)
|
|
|
|
|
/// than in each domain. Also, separating them will create a lot of duplicate code.
|
|
|
|
|
use uuid::Uuid;
|
|
|
|
|
|
|
|
|
|
pub type Time = chrono::DateTime<chrono::Utc>;
|
|
|
|
|
|
|
|
|
|
/// The ID scalar type represents a unique identifier, often used to refetch an object or as key for a cache.
|
|
|
|
|
/// The ID type appears in a JSON response as a String; however, it is not intended to be human-readable.
|
|
|
|
|
/// When expected as an input type, any string (such as "4") or integer (such as 4) input value will be accepted as an ID.
|
|
|
|
|
pub type Id = Uuid;
|
2026-06-03 22:08:44 +02:00
|
|
|
|
2026-06-04 22:42:57 +02:00
|
|
|
// pub type Ulid = String;
|
|
|
|
|
|
|
|
|
|
// pub type Ulid = ulid::Ulid;
|
|
|
|
|
|
|
|
|
|
// #[derive(Clone, Copy, Eq, PartialEq)]
|
|
|
|
|
// pub struct Ulid(pub ulid::Ulid);
|
|
|
|
|
//
|
|
|
|
|
// /// The ULID scalar type represents a Universally Unique Lexicographically Sortable Identifier as defined by the ULID specification.
|
|
|
|
|
// /// ULIDs are 26-character strings that are URL-safe, case-insensitive, and lexicographically sortable,
|
|
|
|
|
// /// making them ideal for distributed systems requiring time-ordered unique identifiers.
|
|
|
|
|
// #[Scalar]
|
|
|
|
|
// impl ScalarType for Ulid {
|
|
|
|
|
// fn parse(value: Value) -> InputValueResult<Self> {
|
|
|
|
|
// match value {
|
|
|
|
|
// Value::String(s) => {
|
|
|
|
|
// let ulid = ulid::Ulid::from_string(&s).map_err(InputValueError::custom)?;
|
|
|
|
|
// Ok(Ulid(ulid))
|
|
|
|
|
// }
|
|
|
|
|
// _ => Err(InputValueError::expected_type(value)),
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
// fn to_value(&self) -> Value {
|
|
|
|
|
// Value::String(self.0.to_string())
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
// impl fmt::Display for Ulid {
|
|
|
|
|
// fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
|
// write!(f, "{}", self.0) // delegiert an Ulid::to_string()
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
//
|
|
|
|
|
//
|
|
|
|
|
use sqlx::{
|
|
|
|
|
postgres::{PgArgumentBuffer, PgHasArrayType, PgTypeInfo, PgValueRef},
|
|
|
|
|
Decode, Encode, Postgres, Type,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// #[derive(sqlx::Type)]
|
|
|
|
|
// #[sqlx(type_name = "ULID")]
|
|
|
|
|
// #[sqlx(no_pg_array)]
|
|
|
|
|
#[derive(Clone, Copy, PartialEq, Eq)]
|
|
|
|
|
pub struct Ulid(pub ulid::Ulid);
|
|
|
|
|
|
|
|
|
|
#[Scalar]
|
|
|
|
|
impl ScalarType for Ulid {
|
|
|
|
|
fn parse(value: Value) -> InputValueResult<Self> {
|
|
|
|
|
match value {
|
|
|
|
|
Value::String(s) => {
|
|
|
|
|
let ulid = ulid::Ulid::from_string(&s).map_err(InputValueError::custom)?;
|
|
|
|
|
Ok(Ulid(ulid))
|
|
|
|
|
}
|
|
|
|
|
_ => Err(InputValueError::expected_type(value)),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn to_value(&self) -> Value {
|
|
|
|
|
Value::String(self.0.to_string())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Type<Postgres> for Ulid {
|
|
|
|
|
fn type_info() -> PgTypeInfo {
|
|
|
|
|
PgTypeInfo::with_name("ULID")
|
|
|
|
|
}
|
|
|
|
|
fn compatible(ty: &PgTypeInfo) -> bool {
|
|
|
|
|
<String as Type<Postgres>>::compatible(ty)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
impl<'r> Decode<'r, Postgres> for Ulid {
|
|
|
|
|
fn decode(value: PgValueRef<'r>) -> Result<Self, Box<dyn std::error::Error + Send + Sync>> {
|
|
|
|
|
let s = <String as Decode<Postgres>>::decode(value)?;
|
|
|
|
|
|
|
|
|
|
Ok(Ulid(ulid::Ulid::from_string(s.trim())?))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<'q> Encode<'q, Postgres> for Ulid {
|
|
|
|
|
fn encode_by_ref(
|
|
|
|
|
&self,
|
|
|
|
|
buf: &mut PgArgumentBuffer,
|
|
|
|
|
) -> Result<sqlx::encode::IsNull, Box<dyn std::error::Error + Send + Sync>> {
|
|
|
|
|
<String as Encode<Postgres>>::encode(self.0.to_string(), buf)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
impl PgHasArrayType for Ulid {
|
|
|
|
|
fn array_type_info() -> PgTypeInfo {
|
|
|
|
|
PgTypeInfo::with_name("_ULID")
|
|
|
|
|
}
|
|
|
|
|
}
|