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>
impl<T> DbEntity<T>
Sourcepub fn from_data(data: T) -> Self
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.
Sourcepub fn from_row(row: &Row) -> Result<Self, DbError>
pub fn from_row(row: &Row) -> Result<Self, DbError>
Given a database row (id, version, data) returns a DbEntity.
Sourcepub fn from_rows(rows: &[Row]) -> Result<Vec<Self>, DbError>
pub fn from_rows(rows: &[Row]) -> Result<Vec<Self>, DbError>
Given a database rows of (id, version, data) tuples returns a Vec of DbEntity.
Sourcepub async fn insert<'a>(&mut self, conn: &Connection) -> Result<(), DbError>
pub async fn insert<'a>(&mut self, conn: &Connection) -> Result<(), DbError>
Inserts a new record into the associated table
Sourcepub async fn delete(&mut self, conn: &Connection) -> Result<(), DbError>
pub async fn delete(&mut self, conn: &Connection) -> Result<(), DbError>
Performs a record deletion
Sourcepub async fn find_by(
conn: &Connection,
filter: (&str, &[&(dyn ToSql + Sync)]),
) -> Result<Option<Self>, DbError>
pub async fn find_by( conn: &Connection, filter: (&str, &[&(dyn ToSql + Sync)]), ) -> Result<Option<Self>, DbError>
Sourcepub async fn find_all(
conn: &Connection,
filter: Option<(&str, &[&(dyn ToSql + Sync)])>,
sorting: Option<&[&str]>,
offset: i64,
limit: i64,
) -> Result<Vec<Self>, DbError>
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,
),
);