ActiveModelBehavior
This commit is contained in:
parent
c482b0cbc6
commit
6a26b8cc17
@ -31,11 +31,17 @@ pub fn expand_derive_active_model(ident: Ident, data: Data) -> syn::Result<Token
|
|||||||
let ty: Vec<Type> = fields.into_iter().map(|Field { ty, .. }| ty).collect();
|
let ty: Vec<Type> = fields.into_iter().map(|Field { ty, .. }| ty).collect();
|
||||||
|
|
||||||
Ok(quote!(
|
Ok(quote!(
|
||||||
#[derive(Clone, Debug, Default)]
|
#[derive(Clone, Debug)]
|
||||||
pub struct ActiveModel {
|
pub struct ActiveModel {
|
||||||
#(pub #field: sea_orm::ActiveValue<#ty>),*
|
#(pub #field: sea_orm::ActiveValue<#ty>),*
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Default for ActiveModel {
|
||||||
|
fn default() -> Self {
|
||||||
|
<Self as sea_orm::ActiveModelBehavior>::new()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl From<#ident> for ActiveModel {
|
impl From<#ident> for ActiveModel {
|
||||||
fn from(m: #ident) -> Self {
|
fn from(m: #ident) -> Self {
|
||||||
Self {
|
Self {
|
||||||
@ -70,6 +76,12 @@ pub fn expand_derive_active_model(ident: Ident, data: Data) -> syn::Result<Token
|
|||||||
#(<Self::Entity as EntityTrait>::Column::#name => self.#field = sea_orm::ActiveValue::unset()),*
|
#(<Self::Entity as EntityTrait>::Column::#name => self.#field = sea_orm::ActiveValue::unset()),*
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn default() -> Self {
|
||||||
|
Self {
|
||||||
|
#(#field: sea_orm::ActiveValue::unset()),*
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
@ -44,7 +44,7 @@ where
|
|||||||
ActiveValue::unchanged(value)
|
ActiveValue::unchanged(value)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait ActiveModelTrait: Clone + Debug + Default {
|
pub trait ActiveModelTrait: Clone + Debug {
|
||||||
type Entity: EntityTrait;
|
type Entity: EntityTrait;
|
||||||
|
|
||||||
fn take(&mut self, c: <Self::Entity as EntityTrait>::Column) -> ActiveValue<Value>;
|
fn take(&mut self, c: <Self::Entity as EntityTrait>::Column) -> ActiveValue<Value>;
|
||||||
@ -54,6 +54,16 @@ pub trait ActiveModelTrait: Clone + Debug + Default {
|
|||||||
fn set(&mut self, c: <Self::Entity as EntityTrait>::Column, v: Value);
|
fn set(&mut self, c: <Self::Entity as EntityTrait>::Column, v: Value);
|
||||||
|
|
||||||
fn unset(&mut self, c: <Self::Entity as EntityTrait>::Column);
|
fn unset(&mut self, c: <Self::Entity as EntityTrait>::Column);
|
||||||
|
|
||||||
|
fn default() -> Self;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub trait ActiveModelBehavior: ActiveModelTrait {
|
||||||
|
type Entity: EntityTrait;
|
||||||
|
|
||||||
|
fn new() -> Self {
|
||||||
|
<Self as ActiveModelTrait>::default()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<V> ActiveValue<V>
|
impl<V> ActiveValue<V>
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
pub use crate::{
|
pub use crate::{
|
||||||
ActiveModelTrait, ColumnTrait, ColumnType, DeriveActiveModel, DeriveColumn, DeriveEntity,
|
ActiveModelBehavior, ActiveModelTrait, ColumnTrait, ColumnType, DeriveActiveModel,
|
||||||
DeriveModel, DerivePrimaryKey, EntityName, EntityTrait, EnumIter, Iden, IdenStatic, ModelTrait,
|
DeriveColumn, DeriveEntity, DeriveModel, DerivePrimaryKey, EntityName, EntityTrait, EnumIter,
|
||||||
PrimaryKeyToColumn, PrimaryKeyTrait, QueryFilter, QueryResult, Related, RelationDef,
|
Iden, IdenStatic, ModelTrait, PrimaryKeyToColumn, PrimaryKeyTrait, QueryFilter, QueryResult,
|
||||||
RelationTrait, Select, TypeErr, Value,
|
Related, RelationDef, RelationTrait, Select, TypeErr, Value,
|
||||||
};
|
};
|
||||||
|
@ -74,3 +74,7 @@ impl Model {
|
|||||||
Entity::find_related().belongs_to::<Entity>(self)
|
Entity::find_related().belongs_to::<Entity>(self)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl ActiveModelBehavior for ActiveModel {
|
||||||
|
type Entity = Entity;
|
||||||
|
}
|
||||||
|
@ -54,3 +54,7 @@ impl RelationTrait for Relation {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl ActiveModelBehavior for ActiveModel {
|
||||||
|
type Entity = Entity;
|
||||||
|
}
|
||||||
|
@ -57,3 +57,7 @@ impl Model {
|
|||||||
Entity::find_related().belongs_to::<Entity>(self)
|
Entity::find_related().belongs_to::<Entity>(self)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl ActiveModelBehavior for ActiveModel {
|
||||||
|
type Entity = Entity;
|
||||||
|
}
|
||||||
|
@ -44,3 +44,7 @@ impl RelationTrait for Relation {
|
|||||||
panic!()
|
panic!()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl ActiveModelBehavior for ActiveModel {
|
||||||
|
type Entity = Entity;
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user