Struct DbEntity

Source
pub struct DbEntity<T>{
    pub id: Uuid,
    pub version: i32,
    pub data: T,
}
Expand description

This struct is used to create a mapping for a data table.

Fields§

§id: Uuid

This is the effective primary key of the record. Its also used to build relations with other tables

§version: i32

This field is used as a record check and identifies possible conflicts for parallel operations. version should always autoinc on record update

§data: T

The real information that a data table record is containing

Implementations§

Source§

impl<T> DbEntity<T>

Source

pub fn new(id: Uuid, version: i32, data: T) -> Self

Simple method used to create a new record

Source

pub fn from_data(data: T) -> Self

Given a data this method uses DbData#find_table_id_and_version to find a possible candidate for record or creates a new one that will need to be persisted with the insert method.

Source

pub fn from_row(row: &Row) -> Result<Self, DbError>

Given a database row (id, version, data) returns a DbEntity.

Source

pub fn from_rows(rows: &[Row]) -> Result<Vec<Self>, DbError>

Given a database rows of (id, version, data) tuples returns a Vec of DbEntity.

Source

pub async fn insert<'a>(&mut self, conn: &Connection) -> Result<(), DbError>

Inserts a new record into the associated table

Source

pub async fn update(&mut self, conn: &Connection) -> Result<(), DbError>

Persists the record

Source

pub async fn delete(&mut self, conn: &Connection) -> Result<(), DbError>

Performs a record deletion

Source

pub async fn find_by( conn: &Connection, filter: (&str, &[&(dyn ToSql + Sync)]), ) -> Result<Option<Self>, DbError>

Searches for a record where filter over data column (JSONB) matches provided parameters.

§Example
DbEntity::<User>::find_by(db_conn, ("data->>'user_name'=$1", &["some_name"]));
Source

pub async fn find_all( conn: &Connection, filter: Option<(&str, &[&(dyn ToSql + Sync)])>, sorting: Option<&[&str]>, offset: i64, limit: i64, ) -> Result<Vec<Self>, DbError>

Searching all matching records defined by filter clause (first element of the filter tuple)
A sorting clause can be given.
Limit and offset define the perimeter of the query result.

§Example
DbEntity::<User>::find_all(
   db_conn,
   (
       "data->>'user_name'=$1",
       &["some_name"],
       Some(&["data->>'user_name' DESC"]),
       0,
       100,
   ),
);

Trait Implementations§

Source§

impl<T> Deref for DbEntity<T>

Source§

type Target = T

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Self::Target

Dereferences the value.

Auto Trait Implementations§

§

impl<T> Freeze for DbEntity<T>
where T: Freeze,

§

impl<T> RefUnwindSafe for DbEntity<T>
where T: RefUnwindSafe,

§

impl<T> Send for DbEntity<T>
where T: Send,

§

impl<T> Sync for DbEntity<T>
where T: Sync,

§

impl<T> Unpin for DbEntity<T>
where T: Unpin,

§

impl<T> UnwindSafe for DbEntity<T>
where T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V