| Crates.io | lorm-macros |
| lib.rs | lorm-macros |
| version | 0.0.8 |
| created_at | 2025-02-16 14:51:58.801904+00 |
| updated_at | 2025-08-04 13:04:18.686858+00 |
| description | Macros for lorm, a zero cost and lightweight ORM operations for SQLx. Not intended to be used directly. |
| homepage | |
| repository | https://github.com/remysaissy/lorm.git |
| max_upload_size | |
| id | 1557805 |
| size | 59,836 |
Lorm is an async and lightweight ORM for SQLx.
You can install lorm by adding the lorm dependency to your Cargo file.
It all starts by adding the #[derive(ToLOrm)] to a structure with SQLx's #[derive(FromRow)].
This will instrument the structure by generating traits and methods according to your needs.
It is then possible to call lorm generated methods using a database connection, whether it comes from a Pool or a Transaction. For eg.
let mut conn = pool.acquire().await.unwrap();
_test(&mut *conn).await;
// Recreate a DB or the second test fails as existing users conflict.
let pool = get_pool().await.expect("Failed to create pool");
let mut tx = pool.begin().await.unwrap();
_test(&mut *tx).await;
tx.commit().await.unwrap();
async fn _test(conn: &mut SqliteConnection) {
let id = Uuid::new_v4();
User::by_id(&mut *conn, &id).await;
}
Check out the lorm/tests directory for more examples.
Primary key
Add the #[lorm(pk)] annotation to the primary key field of your structure.
#[lorm(by)] field.#[lorm(new)] is specified, it will use its struct method to generate a new pk at insertion time#[lorm(is_set)] is specified, it will use its instance method against self to check if the pk is set. Otherwise it compares the pk value with its #[lorm(readonly)] is specified, it will ignore is_set #[lorm(new)] and #[lorm(is_set)] and let the database handles the fieldField of table renaming
Add the #[lorm(rename="name")] annotation
Field to be ignored for persistence
Add the #[lorm(transient)] annotation to ignore the field for all lorm methods.
It is recommended to use #[lorm(transient)] and #[sqlx(skip)] together as transient does not forcibly insert the sqlx skip annotation.
Field that can't be modified under any condition
Add the #[lorm(readonly)] annotation to indicate a field is provided but never updated by your code.
Special cases to consider:
CRUD operation using a specific field
Add the #[lorm(by)] annotation to generate with_
created_at support
Add the #[lorm(created_at)] annotation to mark the field as the created_at field.
#[lorm(new)] is specified, it will use its method to update the time upon insertion#[lorm(readonly)] is specified, it will ignore is_set #[lorm(new)] and let the database handles the fieldupdated_at support
Add the #[lorm(updated_at)] annotation to mark the field as the updated_at field.
#[lorm(new)] is specified, it will use its method to update the time upon insertion and update#[lorm(readonly)] is specified, it will ignore is_set #[lorm(new)] and let the database handles the fieldCustom new method
Add the #[lorm(new="module::path::class::new_custom()")] annotation to use a custom creation method.
Custom check
Add the #[lorm(is_set="is_nil()")] annotation to use a custom check.
It uses a specific function call to check if the returned value if the default value.
The function call is expected to return bool.
Defaults to class_type::default() which assumes both the Default and PartialEq trait are implemented.
Queries are run using the Class::select() method. This method returns a builder to configure the select.
Usage examples are documented in the test cases. Please refer to lorm/tests/main.rs for a concrete example of how to use each feature.
Licensed under Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
Unless you explicitly state otherwise, any Contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be licensed as above, without any additional terms or conditions.