Trait Model

Source
pub trait Model:
    Serialize
    + DeserializeOwned
    + Clone
    + Debug
    + Send
    + Sync {
    type Id: DeserializeOwned + Serialize + Clone + Debug + Send + Sync + Into<Bson>;

    // Required methods
    fn from_document(
        document: Document,
        collection: Option<Collection<Self>>,
    ) -> MResult<Self>;
    fn collection_name() -> String;
    fn own_collection(&self) -> Option<Collection<Self>>;
    fn id(&self) -> Self::Id;
    fn generate_id() -> Self::Id;
    fn attach_collection(&mut self, collection: Collection<Self>);

    // Provided methods
    fn collection(&self) -> Collection<Self> { ... }
    fn save<'life0, 'async_trait>(
        &'life0 self,
    ) -> Pin<Box<dyn Future<Output = MResult<()>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait { ... }
    fn delete<'async_trait>(
        self,
    ) -> Pin<Box<dyn Future<Output = MResult<()>> + Send + 'async_trait>>
       where Self: 'async_trait { ... }
}
Expand description

A model trait. Likely should not be directly implemented, but instead generated with the #[schema(...)] attribute.

Required Associated Types§

Source

type Id: DeserializeOwned + Serialize + Clone + Debug + Send + Sync + Into<Bson>

The type of this Model’s _id field. A bson::oid::ObjectId, uuid::Uuid, or String are probably the best choices here, and bson::oid::ObjectId is the macro default.

Required Methods§

Source

fn from_document( document: Document, collection: Option<Collection<Self>>, ) -> MResult<Self>

Parses a model from a bson::Document, attaching the provided collection.

Source

fn collection_name() -> String

Returns the collection name

Source

fn own_collection(&self) -> Option<Collection<Self>>

Returns the local collection, if present

Source

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

Returns this document’s ID

Source

fn generate_id() -> Self::Id

Generates a new instance of this Model’s ID type

Source

fn attach_collection(&mut self, collection: Collection<Self>)

Sets the local collection

Provided Methods§

Source

fn collection(&self) -> Collection<Self>

Gets the local collection if present, otherwise attempts to use the global client. Panics if neither is defined.

Source

fn save<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = MResult<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Utility function to update/save this record in the database

Source

fn delete<'async_trait>( self, ) -> Pin<Box<dyn Future<Output = MResult<()>> + Send + 'async_trait>>
where Self: 'async_trait,

Utility function to delete this record from the database. Drops the Model instance.

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§