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§
Sourcetype Id: DeserializeOwned + Serialize + Clone + Debug + Send + Sync + Into<Bson>
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§
Sourcefn from_document(
document: Document,
collection: Option<Collection<Self>>,
) -> MResult<Self>
fn from_document( document: Document, collection: Option<Collection<Self>>, ) -> MResult<Self>
Parses a model from a bson::Document, attaching the provided collection.
Sourcefn collection_name() -> String
fn collection_name() -> String
Returns the collection name
Sourcefn own_collection(&self) -> Option<Collection<Self>>
fn own_collection(&self) -> Option<Collection<Self>>
Returns the local collection, if present
Sourcefn generate_id() -> Self::Id
fn generate_id() -> Self::Id
Generates a new instance of this Model’s ID type
Sourcefn attach_collection(&mut self, collection: Collection<Self>)
fn attach_collection(&mut self, collection: Collection<Self>)
Sets the local collection
Provided Methods§
Sourcefn collection(&self) -> Collection<Self>
fn collection(&self) -> Collection<Self>
Gets the local collection if present, otherwise attempts to use the global client. Panics if neither is defined.
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.