Expand description
sqly is a lightweight macro system on top of sqlx.
See the README for additional information, Installation and Features.
§Example
use sqly::*;
#[derive(Table)]
#[sqly(table = "books")]
struct Book {
#[sqly(key)]
id: i32,
title: String,
}
#[derive(Table)]
#[sqly(table = "pages")]
#[sqly(insert, update, select)]
#[sqly(delete = DeleteAllPages)]
struct Page {
#[sqly(key, foreign)]
book: Book,
#[sqly(key, skip = delete)]
page_number: i32,
content: String,
#[sqly(skip = update)]
read: bool,
}
#[derive(Select)]
#[sqly(table = Page)]
struct GetBookPages {
book_id: i32,
}
#[derive(Update)]
#[sqly(table = Page)]
struct MarkAsRead {
#[sqly(key)]
book_id: i32,
#[sqly(key)]
page_number: i32,
read: bool,
}
async fn test(book: &Book, db: &sqlx::PgPool) -> Result<()> {
// struct instantiation will likely be done externally (for example by serde)
// and the structs should be passed as parameters (e.g. `page: &InsertPage`)
// this syntax is less ideal and only used for the sake of this example
Page::insert(&InsertPage { // insert a new page
book_id: book.id,
page_number: 1,
content: "The Wrong Content".into(),
read: false,
})
.execute(db)
.await?;
Page::update(&UpdatePage { // update the page content
book_id: book.id,
page_number: 1,
content: "The Right Content".into(),
})
.execute(db)
.await?;
Page::update(&MarkAsRead { // mark the page as read
book_id: book.id,
page_number: 1,
read: true,
})
.execute(db)
.await?;
let page = Page::select(&SelectPage { // select the updated page
book_id: book.id,
page_number: 1,
})
.fetch_one(db)
.await?;
assert_eq!(page.read, true); // confirm it is marked as read
assert_eq!(page.book.title, book.title); // the book is also fetched
Page::delete(&DeleteAllPages { // delete all pages from the book
book_id: page.book.id,
})
.execute(db)
.await?;
let pages = Page::select(&GetBookPages { // get all pages from the book
book_id: page.book.id,
})
.fetch_all(db)
.await?;
assert!(pages.is_empty()); // confirm no pages are left
Ok(())
}See #[derive(Table)] to get started.
Macros§
- Under development.
- Under development.
- Under development.
Traits§
- A type which can delete rows from a table.
- A type which can insert rows into a table.
- A type which can select rows from a table.
- A type which represents a database table.
- A type which can update rows in a table.
Derive Macros§
- Applied to types which are defined to delete rows from a table.
- Applied to types which are defined to insert rows into a table.
- Applied to types which are defined to select rows from a table.
- Applied to types which are defined to represent a database table.
- Applied to types which are defined to update rows in a table.