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::*;
#[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.
Re-exports§
Modules§
- derive
- Imports.
- docs
- Documentation.
- double_
option - Utility module for serde.
- dynamic
- Utility module for
#[sqly(dynamic)]
.
Macros§
- query
- Under development.
- query_
as - Under development.
- query_
as_ unchecked - Under development.
- query_
scalar - Under development.
- query_
scalar_ unchecked - Under development.
- query_
unchecked - Under development.
Traits§
- Check
- A
Table
which has its columns checked at compile time. - Delete
- A type which can delete rows from a table.
- Delete
Check - A type which has its
Delete
query checked at compile time. - Delete
Impl - A type which can delete rows from a table.
- Flat
- A table with a flattened representation.
- Insert
- A type which can insert rows into a table.
- Insert
Check - A type which has its
Insert
query checked at compile time. - Insert
Impl - A type which can insert rows into a table.
- Select
- A type which can select rows from a table.
- Select
Check - A type which has its
Select
query checked at compile time. - Select
Impl - A type which can select rows from a table.
- Table
- A type which represents a database table.
- Update
- A type which can update rows in a table.
- Update
Check - A type which has its
Update
query checked at compile time. - Update
Impl - A type which can update rows in a table.
Derive Macros§
- Delete
- Applied to types which are defined to delete rows from a table.
- Insert
- Applied to types which are defined to insert rows into a table.
- Select
- Applied to types which are defined to select rows from a table.
- Table
- Applied to types which are defined to represent a database table.
- Update
- Applied to types which are defined to update rows in a table.