Crates.io | rust_orm_gen |
lib.rs | rust_orm_gen |
version | 0.1.3 |
source | src |
created_at | 2024-07-24 21:28:20.655136 |
updated_at | 2024-07-26 22:13:14.043936 |
description | A Rust ORM generator |
homepage | https://github.com/tomblanchard312/rust_orm_gen |
repository | https://github.com/tomblanchard312/rust_orm_gen |
max_upload_size | |
id | 1314432 |
size | 87,966 |
rust_orm_gen is a Rust library designed to reverse engineer PostgreSQL databases and automatically generate Rust structs, CRUD operations, and manage database migrations. This tool simplifies the process of interacting with a PostgreSQL database in Rust, ensuring that your code is clean, maintainable, and efficient.
To install rust_orm_gen, add it to your Rust project by modifying your Cargo.toml
file:
[dependencies]
rust_orm_gen = { git = "https://github.com/tomblanchard312/rust_orm_gen.git" }
tokio = { version = "1", features = ["full"] }
dotenv = "0.15.0"
## Configuration
Ensure your .env file is correctly configured with the database URL:
```text
DATABASE_URL=postgres://user:password@localhost/mydb
Replace user, password, and mydb with your actual PostgreSQL credentials and database name.
mod context;
mod metadata;
mod generator;
mod crud;
mod db;
mod migrations;
mod relationships;
mod query_builder;
mod validation;
use crate::context::DbContext;
use dotenv::dotenv;
use std::env;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
dotenv().ok();
let database_url = env::var("DATABASE_URL")?;
let db_context = DbContext::new(&database_url).await?;
db_context.reverse_engineer("db").await?;
Ok(())
}
cargo run
In your Cargo.toml file add the following:
[dependencies]
rust_orm_gen = "0.1.0"
Add the connection to the Postgresql database in your .env file:
DATABASE_URL=postgres://postgres:yourpassword@localhost/yourdatabase
To use the library in your code, import the library in your main.rs file:
mod db {
pub mod users;
pub mod users_crud;
}
use db::users::Users;
use db::users_crud::{create_users, get_users, update_users, delete_users};
use tokio_postgres::Client;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
dotenv().ok();
let database_url = env::var("DATABASE_URL").expect("DATABASE_URL must be set");
let (client, connection) = tokio_postgres::connect(database_url, tokio_postgres::NoTls).await?;
tokio::spawn(async move {
if let Err(e) = connection.await {
eprintln!("connection error: {}", e);
}
});
// Example usage of generated ORM code
let new_user = Users {
id: 1,
first_name: "John".to_string(),
last_name: "Doe".to_string(),
};
let created_user = create_users(&client, &new_user).await?;
println!("Created user: {:?}", created_user);
let fetched_user = get_users(&client, 1).await?;
println!("Fetched user: {:?}", fetched_user);
Ok(())
}
Detailed documentation for rust_orm_gen can be found in the documentation folder at the project root. The main documentation file is named "rust_orm_gen_documentation.pdf".
Changes version 0.1.2: Added better error handling and the ability to call from the command-line for integration with VSCode extension. version 0.1.2: Introduced features for handling database migrations, relationships, and complex query building.
Example code:
rust_orm_gen "postgres://postgres:password@localhost:5432/yourdb" "C:\GitProjects\rust\rustsample\db"
This example will output all of the database structs and methods to the db folder in your project.