6个版本
0.1.5 | 2024年2月21日 |
---|---|
0.1.4 | 2024年2月21日 |
0.1.2 | 2023年6月11日 |
在 数据库接口 中排名第 406
每月下载量 75 次
24KB
465 行
gen-table
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.
安装
cargo install gen-table
或
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 <dsn> --table <table>
Options:
-d, --dsn <dsn>
mysql dsn,eg:mysql://root:root1234@localhost/test
-o, --out_dir <out_dir>
gen code output dir [default: src/model]
-t, --table <table>
tables eg:orders,users
-e, --enable_tab_name <enable_tab_name>
whether to generate table_name method for struct [default: true]
-n, --no_null <no_null_field>
whether to allow a field of null type [default: false]
-s, --serde <is_serde>
whether to use serde serialization and deserialization [default: false]
-h, --help
Print help
如何使用
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
生成的Rust代码:src/model/mod.rs
// code generated by gen-table. DO NOT EDIT!!!
pub mod news;
pub mod news_topics;
src/model/news.rs
// code generated by gen-table. DO NOT EDIT!!!
// gen code for news table.
use std::time::Duration;
// NEWS_TABLE for news table
const NEWS_TABLE: &str = "news";
// NewsEntity for news table
#[derive(Debug, Default)]
pub struct NewsEntity {
pub id: i64,
pub created_at: Option<Duration>,
pub updated_at: Option<Duration>,
pub deleted_at: Option<Duration>,
pub title: Option<String>,
pub slug: Option<String>,
pub content: Option<String>,
pub status: Option<String>,
}
// impl table_name method for NewsEntity
impl NewsEntity {
pub fn table_name(&self) -> String {
NEWS_TABLE.to_string()
}
}
src/model/news_topics.rs
// code generated by gen-table. DO NOT EDIT!!!
// gen code for news_topics table.
// NEWS_TOPICS_TABLE for news_topics table
const NEWS_TOPICS_TABLE: &str = "news_topics";
// NewsTopicsEntity for news_topics table
#[derive(Debug, Default)]
pub struct NewsTopicsEntity {
pub news_id: i64,
pub topic_id: i64,
}
// impl table_name method for NewsTopicsEntity
impl NewsTopicsEntity {
pub fn table_name(&self) -> String {
NEWS_TOPICS_TABLE.to_string()
}
}
支持serde
gen-table -d=mysql://root:root1234@localhost/test -t=news_topics -s=true
// code generated by gen-table. DO NOT EDIT!!!
// gen code for news_topics table.
use serde::{Deserialize, Serialize};
// NEWS_TOPICS_TABLE for news_topics table
const NEWS_TOPICS_TABLE: &str = "news_topics";
// NewsTopicsEntity for news_topics table
#[derive(Debug, Default, Serialize, Deserialize)]
pub struct NewsTopicsEntity {
pub news_id: i64,
pub topic_id: i64,
}
// impl table_name method for NewsTopicsEntity
impl NewsTopicsEntity {
pub fn table_name(&self) -> String {
NEWS_TOPICS_TABLE.to_string()
}
}
当代码生成时,您需要在您的Cargo.toml中添加以下内容:(对于serde版本,请根据您的项目选择相应的版本)
serde = { version = "1.0.196",features = ["derive"]}
serde_json = "1.0.114"
serde_json仅在执行json序列化时使用,通常不使用。衷心希望上述内容对您有所帮助,谢谢。
依赖关系
~23–36MB
~659K SLoC