Manage schema_name attribute, add prelude export, update example
This commit is contained in:
parent
a4b4f4925f
commit
c273cf0a39
@ -1,37 +1,13 @@
|
||||
use sea_orm::entity::prelude::*;
|
||||
|
||||
#[derive(Copy, Clone, Default, Debug, DeriveEntity)]
|
||||
pub struct Entity;
|
||||
|
||||
impl EntityName for Entity {
|
||||
fn table_name(&self) -> &str {
|
||||
"cake"
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, DeriveModel, DeriveActiveModel)]
|
||||
#[derive(Clone, Debug, PartialEq, DeriveModel, DeriveActiveModel, EntityModel)]
|
||||
#[sea_orm(table_name = "cake")]
|
||||
pub struct Model {
|
||||
#[sea_orm(primary_key)]
|
||||
pub id: i32,
|
||||
pub name: String,
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, Debug, EnumIter, DeriveColumn)]
|
||||
pub enum Column {
|
||||
Id,
|
||||
Name,
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, Debug, EnumIter, DerivePrimaryKey)]
|
||||
pub enum PrimaryKey {
|
||||
Id,
|
||||
}
|
||||
|
||||
impl PrimaryKeyTrait for PrimaryKey {
|
||||
fn auto_increment() -> bool {
|
||||
true
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, Debug, EnumIter)]
|
||||
pub enum Relation {
|
||||
Fruit,
|
||||
|
@ -16,7 +16,7 @@ path = "src/lib.rs"
|
||||
proc-macro = true
|
||||
|
||||
[dependencies]
|
||||
syn = { version = "^1", default-features = false, features = [ "full", "derive", "clone-impls", "parsing", "proc-macro", "printing" ] }
|
||||
syn = { version = "^1", default-features = false, features = [ "full", "derive", "clone-impls", "parsing", "proc-macro", "printing", "extra-traits" ] }
|
||||
quote = "^1"
|
||||
heck = "^0.3"
|
||||
proc-macro2 = "^1"
|
||||
|
@ -6,33 +6,44 @@ use syn::{Attribute, Data, Fields, Lit, Meta, parse::Error, punctuated::Punctuat
|
||||
use convert_case::{Case, Casing};
|
||||
|
||||
pub fn expand_derive_entity_model(data: Data, attrs: Vec<Attribute>) -> syn::Result<TokenStream> {
|
||||
// if #[sea_orm(table_name = "foo")] specified, create Entity struct
|
||||
let table_name = attrs.iter().filter_map(|attr| {
|
||||
if attr.path.get_ident()? != "sea_orm" {
|
||||
return None;
|
||||
// if #[sea_orm(table_name = "foo", schema_name = "bar")] specified, create Entity struct
|
||||
let mut table_name = None;
|
||||
let mut schema_name = quote! { None };
|
||||
attrs.iter().for_each(|attr| {
|
||||
if attr.path.get_ident().map(|i| i == "sea_orm") != Some(true) {
|
||||
return;
|
||||
}
|
||||
|
||||
let list = attr.parse_args_with(Punctuated::<Meta, Comma>::parse_terminated).ok()?;
|
||||
for meta in list.iter() {
|
||||
if let Meta::NameValue(nv) = meta {
|
||||
if nv.path.get_ident()? == "table_name" {
|
||||
let table_name = &nv.lit;
|
||||
return Some(quote! {
|
||||
#[derive(Copy, Clone, Default, Debug, sea_orm::prelude::DeriveEntity)]
|
||||
pub struct Entity;
|
||||
|
||||
impl sea_orm::prelude::EntityName for Entity {
|
||||
fn table_name(&self) -> &str {
|
||||
#table_name
|
||||
}
|
||||
}
|
||||
});
|
||||
if let Ok(list) = attr.parse_args_with(Punctuated::<Meta, Comma>::parse_terminated) {
|
||||
for meta in list.iter() {
|
||||
if let Meta::NameValue(nv) = meta {
|
||||
if let Some(ident) = nv.path.get_ident() {
|
||||
if ident == "table_name" {
|
||||
table_name = Some(nv.lit.clone());
|
||||
}
|
||||
else if ident == "schema_name" {
|
||||
let name = &nv.lit;
|
||||
schema_name = quote! { Some(#name) };
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
let entity_def = table_name.map(|table_name| quote! {
|
||||
#[derive(Copy, Clone, Default, Debug, sea_orm::prelude::DeriveEntity)]
|
||||
pub struct Entity;
|
||||
|
||||
impl sea_orm::prelude::EntityName for Entity {
|
||||
fn schema_name(&self) -> &str {
|
||||
#schema_name
|
||||
}
|
||||
|
||||
None
|
||||
}).next().unwrap_or_default();
|
||||
fn table_name(&self) -> &str {
|
||||
#table_name
|
||||
}
|
||||
}
|
||||
}).unwrap_or_default();
|
||||
|
||||
// generate Column enum and it's ColumnTrait impl
|
||||
let mut columns_enum: Punctuated<_, Comma> = Punctuated::new();
|
||||
@ -208,7 +219,7 @@ impl sea_orm::prelude::ColumnTrait for Column {
|
||||
}
|
||||
}
|
||||
|
||||
#table_name
|
||||
#entity_def
|
||||
|
||||
#primary_key
|
||||
})
|
||||
|
@ -1,7 +1,7 @@
|
||||
pub use crate::{
|
||||
error::*, ActiveModelBehavior, ActiveModelTrait, ColumnDef, ColumnTrait, ColumnType,
|
||||
DeriveActiveModel, DeriveActiveModelBehavior, DeriveColumn, DeriveCustomColumn, DeriveEntity,
|
||||
DeriveModel, DerivePrimaryKey, EntityName, EntityTrait, EnumIter, Iden, IdenStatic, ModelTrait,
|
||||
DeriveModel, DerivePrimaryKey, EntityModel, EntityName, EntityTrait, EnumIter, Iden, IdenStatic, ModelTrait,
|
||||
PrimaryKeyToColumn, PrimaryKeyTrait, QueryFilter, QueryResult, Related, RelationDef,
|
||||
RelationTrait, Select, Value,
|
||||
};
|
||||
|
@ -219,7 +219,7 @@ pub use query::*;
|
||||
|
||||
pub use sea_orm_macros::{
|
||||
DeriveActiveModel, DeriveActiveModelBehavior, DeriveColumn, DeriveCustomColumn, DeriveEntity,
|
||||
DeriveModel, DerivePrimaryKey, FromQueryResult,
|
||||
DeriveModel, DerivePrimaryKey, FromQueryResult, EntityModel,
|
||||
};
|
||||
|
||||
pub use sea_query;
|
||||
|
Loading…
x
Reference in New Issue
Block a user