DatabaseObject

Trait DatabaseObject 

Source
pub trait DatabaseObject: Sized + for<'r> FromRow<'r, SqliteRow> {
    type Id;

    // Required methods
    fn write_to_db<'e>(
        &'e self,
        executor: impl SqliteExecutor<'e> + 'e,
    ) -> BoxedFuture<'e, Result<()>>;
    fn from_db<'e>(
        id: Self::Id,
        executor: impl SqliteExecutor<'e> + 'e,
    ) -> BoxedFuture<'e, Result<Self>>;
    fn exists<'e>(
        &'e self,
        executor: impl SqliteExecutor<'e> + 'e,
    ) -> BoxedFuture<'e, Result<bool>>;
    fn all_names<'e>(
        executor: impl SqliteExecutor<'e> + 'e,
    ) -> BoxedFuture<'e, Result<Vec<Self>>>;
    fn remove_from_db<'e>(
        &'e self,
        executor: impl SqliteExecutor<'e> + 'e,
    ) -> BoxedFuture<'e, Result<()>>;
}
Expand description

This trait should be implemented on sub-properties of crate::metadata::Metadata such as crate::metadata::Category.

It provides a simple interface for database operations, used to simplify operations on crate::metadata::Metadata.

An update method is omitted on purpose. E.g. categories should be editable, instead new ones should be created/old ones removed.

The trait can be implemented using macros from the episko_derive crate.

§Example

use episko_lib::database::DatabaseObject;
use sqlx::FromRow;

#[derive(DatabaseObject, FromRow)]
#[db(table = "example_property")]
struct ExampleProperty {
    #[db(col = "id")]
    id: u32, // a field with the column name "id" required
    #[db(col = "name")]
    name: String, // all other fields are optional
    #[db(col = "version")]
    version: Option<String>,
}

Required Associated Types§

Required Methods§

Source

fn write_to_db<'e>( &'e self, executor: impl SqliteExecutor<'e> + 'e, ) -> BoxedFuture<'e, Result<()>>

Write the given instance of the object to the database.

Source

fn from_db<'e>( id: Self::Id, executor: impl SqliteExecutor<'e> + 'e, ) -> BoxedFuture<'e, Result<Self>>

Retrieve the given object from the database.

Source

fn exists<'e>( &'e self, executor: impl SqliteExecutor<'e> + 'e, ) -> BoxedFuture<'e, Result<bool>>

Check if the given object exists in the database

Source

fn all_names<'e>( executor: impl SqliteExecutor<'e> + 'e, ) -> BoxedFuture<'e, Result<Vec<Self>>>

Retrieve all unique names

Source

fn remove_from_db<'e>( &'e self, executor: impl SqliteExecutor<'e> + 'e, ) -> BoxedFuture<'e, Result<()>>

Remove the given object from the database

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§