Skip to content

Commit

Permalink
Clarify usage of Json/Jsonb in query macros (launchbadge#3447)
Browse files Browse the repository at this point in the history
* add information and example on using json in query macros

* run cargo format

* add missing bracket to docs of json

* wrap docs in hidden async function

* change no_run to ignore
  • Loading branch information
Lachstec authored and jrasanen committed Oct 14, 2024
1 parent d787a83 commit 5443536
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions sqlx-core/src/types/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,35 @@ 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
/// ```rust,ignore
/// # async fn example3() -> sqlx::Result<()> {
/// # let mut conn: sqlx::PgConnection = unimplemented!();
/// #[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(&mut conn)
/// .await?;
/// # Ok(())
/// # }
/// ```
#[derive(
Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Default, Serialize, Deserialize,
)]
Expand Down

0 comments on commit 5443536

Please sign in to comment.