gearbox_rs_postgres/entity.rs
1use sqlx::postgres::PgRow;
2
3/// Trait that defines entity metadata for database operations.
4///
5/// This trait is typically derived using `#[derive(PgEntity)]` from gearbox-rs-macros.
6///
7/// # Example
8///
9/// ```ignore
10/// use gearbox_rs_macros::PgEntity;
11///
12/// #[derive(PgEntity)]
13/// #[table("users")]
14/// pub struct User {
15/// #[primary_key]
16/// pub id: String,
17/// pub name: String,
18/// pub email: String,
19/// }
20/// ```
21pub trait PgEntity: Send + Sync + Sized {
22 /// The primary key type.
23 /// - Single field: that field's type (e.g., `String`, `i64`, `Uuid`)
24 /// - Multiple fields: tuple of types (e.g., `(i64, i64)`, `(Uuid, String)`)
25 type Id: Send + Sync;
26
27 /// The database table name (may include schema, e.g., "public.users")
28 const TABLE: &'static str;
29
30 /// The schema key used to resolve the correct connection pool.
31 /// Defaults to "default" for backward compatibility with single-schema configs.
32 const SCHEMA: &'static str = "default";
33
34 /// All column names that map to struct fields (excludes `#[skip]` fields)
35 const COLUMNS: &'static [&'static str];
36
37 /// Primary key column name(s)
38 const PK_COLUMNS: &'static [&'static str];
39
40 /// Returns the entity's primary key value
41 fn id(&self) -> Self::Id;
42
43 /// Constructs an entity from a database row
44 fn from_row(row: PgRow) -> Self;
45}