entgen is cli tool to generate entity and repository for sqlx. This project is work-in-progress. Stay tuned.
$ target/debug/entgen --help
entgen 0.0.1
Hikaru Miyahara
Entity generator for sqlx
USAGE:
entgen [OPTIONS]
OPTIONS:
-f, --file <FILE> Set entgen config file [default: entgen.toml]
-h, --help Print help information
-V, --version Print version information
You must define entgen.toml file if you use entgen cli. Config file format is below.
output_dir = "src/entity"
[postgres]
user = "<user>"
password = "<password>"
host = "<host>"
port = <port>
db = "<db>"
At first, you must define db model.
CREATE TABLE users (
id uuid PRIMARY KEY,
name varchar(255) NOT NULL,
nickname varchar(255),
created_at timestamp NOT NULL
);
If you ware defined db model like above, you can get Rust model to use cli.
// DO NOT EDIT
// This code was generated by github.com/hikaru7719/entgen
#[derive(sqlx::FromRow, Debug)]
pub struct Users {
pub id: sqlx::types::Uuid,
pub name: String,
pub nickname: Option<String>,
pub created_at: sqlx::types::chrono::NaiveDateTime,
}
use std::ops::Deref;
use std::sync::Arc;
pub struct UsersRepository {
pool: Arc<sqlx::PgPool>,
}
impl UsersRepository {
pub fn new(pool: Arc<sqlx::PgPool>) -> Self {
UsersRepository { pool: pool }
}
pub async fn insert(&self, users: &Users) -> Result<(), sqlx::Error> {
sqlx::query("INSERT INTO users (id, name, nickname, created_at) VALUES ($1, $2, $3, $4)")
.bind(users.id)
.bind(users.name.clone())
.bind(users.nickname.as_ref())
.bind(users.created_at)
.execute(self.pool.deref())
.await?;
Ok(())
}
pub async fn find_all(&self) -> Result<Vec<Users>, sqlx::Error> {
Ok(sqlx::query_as::<_, Users>("SELECT * FROM users")
.fetch_all(self.pool.deref())
.await?)
}
pub async fn find_by_id(&self, id: &sqlx::types::Uuid) -> Result<Users, sqlx::Error> {
Ok(
sqlx::query_as::<_, Users>("SELECT * FROM users WHERE id = $1")
.bind(id)
.fetch_one(self.pool.deref())
.await?,
)
}
pub async fn delete(&self, id: &sqlx::types::Uuid) -> Result<(), sqlx::Error> {
sqlx::query("DELETE FROM users WHERE id = $1")
.bind(id)
.execute(self.pool.deref())
.await?;
Ok(())
}
}
Example is here.
This cli only supports PostgreSQL now.