Clarify usage of Json/Jsonb in query macros (#3447)

* 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
This commit is contained in:
Leon Lux 2024-08-25 00:59:40 +02:00 committed by GitHub
parent 1fb255935f
commit f69f370f25
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -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,
)]