gen-table is a tool that teaches mysql table structures to generate rust struct code,
which is easy for developers to use and automatically manage table structure generation.
shell
cargo install gen-table
or
shell
cargo install --git https://github.com/daheige/rs-tbox
``` gen-table -h Hello, welcome to gen-table gen-table for mysql table structures convert to rust code
Usage: gen-table [OPTIONS] --dsn Options:
-d, --dsn The generated rust code:
src/model/mod.rs
```
// code generated by gen-table. DO NOT EDIT!!!
pub mod news;
pub mod news_topics; ``` src/model/news.rs
```rust
// code generated by gen-table. DO NOT EDIT!!!
// gen code for news table.
use std::time::Duration;
// NEWSTABLE for news table
const NEWSTABLE: &str = "news"; // NewsEntity for news table pub struct NewsEntity {
pub id: i64,
pub createdat: Option // impl tablename method for NewsEntity
impl NewsEntity {
pub fn tablename(&self) -> String {
NEWSTABLE.tostring()
}
}
``` src/model/newstopics.rs
```rust
// code generated by gen-table. DO NOT EDIT!!!
// gen code for newstopics table.
// NEWSTOPICSTABLE for newstopics table
const NEWSTOPICSTABLE: &str = "newstopics"; // NewsTopicsEntity for news_topics table pub struct NewsTopicsEntity {
pub newsid: i64,
pub topicid: i64,
} // impl tablename method for NewsTopicsEntity
impl NewsTopicsEntity {
pub fn tablename(&self) -> String {
NEWSTOPICSTABLE.to_string()
}
}
``` gen-table -d=mysql://root:root1234@localhost/test -t=newstopics -s=true
```rust
// code generated by gen-table. DO NOT EDIT!!!
// gen code for newstopics table.
use serde::{Deserialize, Serialize}; // NEWSTOPICSTABLE for newstopics table
const NEWSTOPICSTABLE: &str = "newstopics"; // NewsTopicsEntity for news_topics table pub struct NewsTopicsEntity {
pub newsid: i64,
pub topicid: i64,
} // impl tablename method for NewsTopicsEntity
impl NewsTopicsEntity {
pub fn tablename(&self) -> String {
NEWSTOPICSTABLE.to_string()
}
}
``` When the code is generated, you need to add the following to your Cargo.toml:
(For the serde version, choose the corresponding version according to your project)
tables eg:orders,users
-e, --enabletabname
how to use
shell
gen-table -d=mysql://root:root1234@localhost/test -t=news,news_topics -o=src/model
Hello, welcome to gen-table
tables:news,news_topics enable_table_name:true no_null_field:false
gen tables:["news", "news_topics"] rust code
gen code for table:news
current field:created_at is null able,type:Duration
current field:updated_at is null able,type:Duration
current field:deleted_at is null able,type:Duration
current field:title is null able,type:String
current field:slug is null able,type:String
current field:content is null able,type:String
current field:status is null able,type:String
gen code for table:news finish
gen code for table:news_topics
gen code for table:news_topics finish
[derive(Debug, Default)]
[derive(Debug, Default)]
serde support
[derive(Debug, Default, Serialize, Deserialize)]
rust
serde = { version = "1.0.164",features = ["derive"]}
serde_json = "1.0.96"
serde_json is only used when doing json serialization, and is generally not used.
I sincerely hope that the above content will be helpful to you,Thank you.