-
-
Notifications
You must be signed in to change notification settings - Fork 453
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
More compact entity format - DeriveModel
, DeriveModelColumn
, DeriveModelPrimaryKey
, DeriveEntity
, DeriveRelation
#122
Conversation
SimpleModel
derive for simplified models
I'm thinking, instead of having For example: #[derive(Clone, Debug, SimpleModel)]
#[table(name = "users")]
pub struct User {
#[primary_key]
pub id: Uuid,
pub email: String,
pub password: String,
pub full_name: Option<String>,
pub age: Option<u64>,
}
#[derive(Clone, Debug, SimpleInput)]
#[input(model = "User")]
pub struct NewUser<'a> {
pub email: &'a str,
pub full_name: Option<&'a str>,
pub age: Option<&'a str>, // Compiler would fail because `u64` does not implement `From<&str>`
} The benefit of this, is that a check could be done at compile time to make sure that the types and fields in the |
Removes the need for prelude when using `SimpleModel`
Yes it makes sense. It's cleaner and more intuitive to separate the SimpleInput |
71f7f40
to
9a1b771
Compare
I've added the SimpleInput feature. It's usage is dependant on a struct derived with SimpleModel being in the same module, and it checks the types are compatible with the The following example will not compile: #[derive(SimpleModel)]
#[table(name = "users")]
pub(crate) struct User {
#[primary_key]
pub id: Uuid,
pub name: String,
}
#[derive(SimpleInput)]
#[input(model = User)]
pub(crate) struct NewSeo<'a> {
pub name: u64, // Error: the trait bound `String: From<u64>` is not satisfied
} The checking between types does not cause a compile error if one type is in an Option and the other is not. I initially started implementing |
SimpleModel
derive for simplified modelsSimpleModel
and SimpleInput
derive for simplified models
…veModelPrimaryKey`
SimpleModel
and SimpleInput
derive for simplified modelsDeriveModel
, DeriveModelColumn
, DeriveModelPrimaryKey
, DeriveRelation
fn type_to_column_type(ty: syn::Type) -> TokenStream { | ||
let ty_string = ty.into_token_stream().to_string(); | ||
match ty_string.as_str() { | ||
"std::string::String" | "string::String" | "String" => { | ||
quote!(ColumnType::String(None)) | ||
} | ||
"char" => quote!(ColumnType::Char(None)), | ||
"i8" => quote!(ColumnType::TinyInteger), | ||
"i16" => quote!(ColumnType::SmallInteger), | ||
"i32" => quote!(ColumnType::Integer), | ||
"i64" => quote!(ColumnType::BigInteger), | ||
"f32" => quote!(ColumnType::Float), | ||
"f64" => quote!(ColumnType::Double), | ||
"Json" => quote!(ColumnType::Json), | ||
"DateTime" => quote!(ColumnType::DateTime), | ||
"DateTimeWithTimeZone" => quote!(ColumnType::DateTimeWithTimeZone), | ||
"Decimal" => quote!(ColumnType::Decimal(None)), | ||
"Uuid" => quote!(ColumnType::Uuid), | ||
"Vec < u8 >" => quote!(ColumnType::Binary), | ||
"bool" => quote!(ColumnType::Boolean), | ||
_ => { | ||
let ty_name = ty_string.replace(' ', "").to_snake_case(); | ||
quote!(ColumnType::Custom(#ty_name.to_owned())) | ||
} | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rust types converting into ColumnType can be found here.
DeriveModel
, DeriveModelColumn
, DeriveModelPrimaryKey
, DeriveRelation
DeriveModel
, DeriveModelColumn
, DeriveModelPrimaryKey
, DeriveEntity
, DeriveRelation
@tyt2y3 @billy1624 I think the PR is done if you want to review it. |
Thank you so much for the big contribution. I am going through it. |
Thank you so much for the contribution. I really liked your coding style, especially for using a struct to hold all the parameters.
As for I will merge this to a separate branch, cherry-pick the bits, make some tweaks and integrate things together. |
Postgres chained concatenate expression
This PR aims to resolve #105.
It adds features to the existing:
DeriveModel
DeriveEntity
And adds new derives:
DeriveModelColumn
DeriveModelPrimaryKey
DeriveRelation
DeriveModel
changesDerive attributes:
Field attributes: none
Summary:
DeriveModel still works the same, but includes an additional attribute
entity
which allows you to specify theEntity
struct incase you've named it something other thanEntity
.Example:
DeriveEntity
changesDerive attributes:
Field attributes: none
Summary:
DeriveEntity
still works the same, but also implementsEntityName
if thetable_name
is set.Example:
DeriveModelColumn
addedDerive attributes:
Field attributes:
Summary:
Generate's the column enum based on the field names.
The column enum implements
ColumnTrait
,FromStr
,Iden
&IdenStatic
. A method.as_str()
is also added to it.ColumnTrait
is implemented on the column enum, and attempts to detect the type from field's Rust type. If thecolumn_type
attribute is added, it will use this type instead.Example:
Mapping for rust types into
ColumnDef
types can be found here: https://github.com/SeaQL/sea-orm/pull/122/files#r702886781DeriveModelPrimaryKey
addedDerive attributes:
Field attributes:
Summary:
Generates the
PrimaryKey
enum based on the fields marked withprimary_key
. ImplementsIden
,IdenStatic
,PrimaryKeyToColumn
&PrimaryKeyTrait
on the enum. Additionally adds a.as_str()
method.PrimaryKeyTrait
is implemented and uses the first field marked asprimary_key
for theValueType
.If there are multiple primary keys, auto_increment will be false, otherwise true by default with the ability to change this with the
auto_increment
attribute.Example:
DeriveRelation
addedDerive attributes:
Field attributes:
Summary:
Implements
RelationTrait
on the enum.Each variant is required to have the field attributes listed above, and generates the
RelationTrait
based on the variants.Example:
Example of all derives