Skip to main content

PgEntity

Trait PgEntity 

Source
pub trait PgEntity:
    Send
    + Sync
    + Sized {
    type Id: Send + Sync;

    const TABLE: &'static str;
    const COLUMNS: &'static [&'static str];
    const PK_COLUMNS: &'static [&'static str];
    const SCHEMA: &'static str = "default";

    // Required methods
    fn id(&self) -> Self::Id;
    fn from_row(row: PgRow) -> Self;
}
Expand description

Trait that defines entity metadata for database operations.

This trait is typically derived using #[derive(PgEntity)] from gearbox-rs-macros.

§Example

use gearbox_rs_macros::PgEntity;

#[derive(PgEntity)]
#[table("users")]
pub struct User {
    #[primary_key]
    pub id: String,
    pub name: String,
    pub email: String,
}

Required Associated Constants§

Source

const TABLE: &'static str

The database table name (may include schema, e.g., “public.users”)

Source

const COLUMNS: &'static [&'static str]

All column names that map to struct fields (excludes #[skip] fields)

Source

const PK_COLUMNS: &'static [&'static str]

Primary key column name(s)

Provided Associated Constants§

Source

const SCHEMA: &'static str = "default"

The schema key used to resolve the correct connection pool. Defaults to “default” for backward compatibility with single-schema configs.

Required Associated Types§

Source

type Id: Send + Sync

The primary key type.

  • Single field: that field’s type (e.g., String, i64, Uuid)
  • Multiple fields: tuple of types (e.g., (i64, i64), (Uuid, String))

Required Methods§

Source

fn id(&self) -> Self::Id

Returns the entity’s primary key value

Source

fn from_row(row: PgRow) -> Self

Constructs an entity from a database row

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§