Expand description
An orm that is simple to use and prevents runtime errors by using rust’s rich type system to enforce sql logic at compile time.
§Usage
The core of this crate is the Table
derive macro, which you can derive
on your rust structs to tell the orm that they represent tables in your
database.
§Example
#[derive(Debug, Table)]
pub struct Person {
id: i32,
name: String,
age: i32,
#[table(foreign_key(School))]
school_id: i32,
}
#[derive(Debug, Table)]
pub struct School {
id: i32,
name: String,
}
struct MyMigration;
migration! { MyMigration => school, person }
let pool =
DatabaseConnectionPool::connect("postgres://postgres:postgres@localhost/some_database")
.await?;
MyMigration::down(&pool).await?;
MyMigration::up(&pool).await?;
let school_id = school::new { name: "Stanford" }
.insert_returning_value(returning!(school::id), &pool)
.await?;
person::new {
name: "James",
age: &35,
school_id,
}
.insert(&pool)
.await?;
#[derive(FromQueryResult)]
struct PersonNameAndSchoolName {
person_name: String,
school_name: String,
}
let person_and_school_names = person::table
.inner_join(school::table)
.find()
.select(select_values!(
person::name as person_name,
school::name as school_name
))
.load_all::<PersonNameAndSchoolName>(&pool)
.await?;
struct AgeSumOfSchool {
school_name: String,
age_sum: i64,
}
let age_sum_of_each_school_from_highest_to_lowest = person::table
.inner_join(school::table)
.find()
.select(select_values!(
school::name as school_name,
person::age.sum() as age_sum
))
.group_by(school::id)
.order_by_selected_value_descending(selected_value_to_order_by!(age_sum))
.load_all::<AgeSumOfSchool>(&pool)
.await?;
let old_enough_people_ids = person::table
.find()
.filter(person::age.greater_equals(20))
.select(select_values!(person::id))
.load_all_values(&pool)
.await?;
For more examples, check out the examples directory.
§Migration Cli
If you want to create a cli to manage your migration, you can use the
migration_cli
feature flag. This feature flag will provide you the
migration_cli_main
function which you can call in your main function and
it will take care of the rest.
For an example of this, check out the migration_cli
example in the
examples directory.
Re-exports§
pub use sql::FromQueryResult;
pub use sql::Table;
pub use bytes;
pub use deadpool_postgres::tokio_postgres;
pub use futures;
Modules§
- execution
- Execution of sql statements.
- sql
- Sql logic encoded in rust’s type system.
- statements
- Implementation of different sql statements.
- util
- Utilities that are not directly related to the purpose of this crate.
Macros§
- migration
- Implements the
Migration
trait for some struct, given the tables that it should manage. - returning
- This macro is just another name for the
select_values!
macro, for usage information check out the documentation on that macro. - select_
values - A macro which allows selecting custom values from a query.
- selected_
value_ to_ order_ by - This macro provides a way to order the results of a query by a value
selected using the
select_values
macro. - update_
set - This macro allows providing a set of updates to perform on each row in an sql update statement.
Structs§
- Decimal
Decimal
represents a 128 bit representation of a fixed-precision decimal number. The finite set of values of typeDecimal
are of the form m / 10e, where m is an integer such that -296 < m < 296, and e is an integer between 0 and 28 inclusive.
Enums§
- Error
- An error types which encapsulates all errors which can occur while using this crate.
Type Aliases§
Attribute Macros§
Derive Macros§
- From
Query Result - Implements the
FromQueryResult
trait for some struct. - SqlEnum
- Implements the
SqlEnum
trait for some type. This allows using this type as a field in a table struct, and using it in sql expresions. - Table
- Implements the
Table
trait for the some struct, and creates a table module for it containing some useful items which allow performing operations on this table.