Crates.io | db-helpers |
lib.rs | db-helpers |
version | 1.5.0 |
source | src |
created_at | 2021-11-03 04:14:46.409055 |
updated_at | 2022-04-08 18:08:28.545259 |
description | Various macros to help with database queries and tables |
homepage | |
repository | https://github.com/NikosEfthias/db-helpers |
max_upload_size | |
id | 475988 |
size | 555,361 |
db_helpers provide various helpers to simplify and make safer to interact with databases.
This is not an orm library the idea is simply to be able to replace inline queries and start getting benefits without learning a new library
use db_helpers::{Table,Q};
#[derive(Table)]
//index is optional
//__TABLE__ key is automatically replaced with table_name
#[table(name = "users", index = "create unique index if not exists unique_usernames_of_users on __TABLE__ (username)")]
struct User
{
//if name is not specified lowercase fieldname is used by default
//q is mandatory
#[table(name = "id", q = "bigserial primary key not null")]
_id: i64,
//name of this field is assumed username
#[table( q = "text")]
username: String,
}
#[derive(Table)]
//index is optional
#[table(name = "ops")]
struct Op{
#[table(q="bigserial not null primary key")]
id:i64,
#[table(q="bigint not null references")]
user_id:i64,
}
db.batch_execure(
//Available if pg feature is enabled
[User::__pg_create_table_str(),User::__pg_index()].join(";")
).await;
//unfortunately for the time being `<struct>::{` part cannot contain spaces smarter parsing is in the todo list
let User:User = db.query_one(Q!("select User::{_id,username} from User::__TABLE__"),params!()).await.unwrap();
db.execute(Q!("insert into ( Foo::{username} ) VALUES ($1)"),params!("superman")).await.unwrap();
//you can also use tablename.fieldname format using > in the beginning of the field
//produces `select id , users.username from users`
let User:User = db.query_one(Q!("select User::{_id,>username} from User::__TABLE__"),params!()).await.unwrap();
let ops : Vec<Op> = db
.query(Q!("select * from Op::__TABLE__ where Op::{user_id} = (select Foo::{_id} from Foo::__TABLE__ where Foo::{username} = $1)"),
params!("superman"))
.await.unwrap().iter(Into::into).collect();
//it also supports runtime args using format macro
//if no additional arguments provided Q produces a &'static str otherwise it passes everything to format! macro
Q!(
"select Foo::{>username} from Foo::__TABLE__ where Foo::{>_id} in ({})",
["1", "2", "3"].join(",")
)
Please check changelog for details
Users::{*-password}
q
argument optionalQ
smarter to allow using spaces in more places as well as no spaces in places like inserts