Skip to content

Commit

Permalink
add information and example on using json in query macros
Browse files Browse the repository at this point in the history
  • Loading branch information
Lachstec committed Aug 20, 2024
1 parent 6f29056 commit b7c7da7
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions sqlx-core/src/types/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,32 @@ use crate::types::Type;
/// dewey_decimal: sqlx::types::Json<HashMap<String, Book>>
/// }
/// ```
///
/// If the query macros are used, it is necessary to tell the macro to use
/// the `Json` adapter by using the type override syntax
/// ```
/// #[derive(sqlx::FromRow]
/// struct Book {
/// title: String,
/// }
///
/// #[derive(sqlx::FromRow)]
/// struct Author {
/// name: String,
/// books: sqlx::types::Json<Book>,
/// }
/// // Note the type override in the query string
/// let authors = sqlx::query_as!(
/// Author,
/// r#"
/// SELECT name, books as "books: Json<Book>"
/// FROM authors
/// "#
/// )
/// .fetch_all(pool)
/// .await?;
///
/// ```
#[derive(
Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Default, Serialize, Deserialize,
)]
Expand Down

0 comments on commit b7c7da7

Please sign in to comment.