Crates.io | dbspec |
lib.rs | dbspec |
version | 0.2.0 |
source | src |
created_at | 2024-05-01 21:36:37.620318 |
updated_at | 2024-05-01 22:17:50.950501 |
description | Database introspection and model generation |
homepage | |
repository | |
max_upload_size | |
id | 1227224 |
size | 75,748 |
A database introspection and generator library.
This crate gives you the ability to create fake data for your living live database.
To use this crate, ensure you have a PostgreSQL or MySQL database running and accessible. You can specify the connection string to your database when running the main application, which utilizes this crate to fetch and display the database schema information.
To get a list of all your tables, use the get_tables()
function:
use dbspec::get_tables;
#[tokio::main]
async fn main() {
let all_my_tables = get_tables("connection_string").await;
// ...
}
You'll have the ability to generate fake data by using the faking!()
macro.
use dbspec::faking;
use dbspec::fakeit; // or your choice of faking libraries
faking!(
Id, id: String, || unique::uuid_v4();
CreatedAt, created_at: chrono::DateTime<Utc>, random_datetime;
UpdatedAt, updated_at: chrono::DateTime<Utc>, random_datetime;
Gender, gender: String, || gender();
Age, age: i32, || random_number_between(18, 100);
);
fn random_number_between(start: i32, end: i32) -> i32 {
rand::thread_rng().gen_range(start..=end)
}
fn random_datetime() -> DateTime<Utc> {
let data = datetime::date();
DateTime::from_timestamp(data.secs, data.nsecs).expect("invalid or out-of-range datetime")
}
Where this really comes in handy is mapping columns to get a random value of your design, for instance:
async fn generate_new_user() {
for i in 0..10 {
let user = User {
id: match_struct("id"),
username: match_struct("username"),
}
}
}