| Crates.io | diesel_pgrx_derive |
| lib.rs | diesel_pgrx_derive |
| version | 0.1.0 |
| created_at | 2025-11-04 09:50:46.360133+00 |
| updated_at | 2025-11-04 09:50:46.360133+00 |
| description | Derive macros for diesel_pgrx - Diesel ORM integration with PGRX |
| homepage | |
| repository | https://github.com/earth-metabolome-initiative/emi-monorepo |
| max_upload_size | |
| id | 1916052 |
| size | 37,118 |
Integration layer enabling Diesel ORM to work with custom Rust types defined in PGRX PostgreSQL extensions. The crate provides a derive macro that implements Diesel's serialization traits (ToSql and FromSql) using PGRX's binary protocol, which internally uses CBOR serialization via serde_cbor. This allows types decorated with PGRX's PostgresType and #[pg_binary_protocol] to be seamlessly queried through Diesel. Additional backend support includes SQLite using the same CBOR serialization format.
Add this to your Cargo.toml:
[dependencies]
diesel_pgrx = "0.1"
diesel = { version = "2.2", features = ["postgres"] }
serde = { version = "1.0", features = ["derive"] }
After building your PGRX extension, install it in PostgreSQL:
CREATE EXTENSION your_extension;
Derive DieselPGRX for types that implement serde::Serialize and serde::Deserialize. The type must also use PGRX's PostgresType and pg_binary_protocol attributes:
#[derive(
Debug,
serde::Serialize,
serde::Deserialize,
diesel_pgrx::DieselPGRX,
diesel::FromSqlRow,
diesel::AsExpression
)]
#[cfg_attr(feature = "pgrx", derive(pgrx::PostgresType))]
#[cfg_attr(feature = "pgrx", pg_binary_protocol)]
#[diesel(sql_type = diesel_impls::MyCustomType)]
pub struct MyCustomType {
pub field: i32,
}
PGRX provides optional derives for comparison and hashing operations. When using these, implement the corresponding Rust traits:
#[derive(serde::Serialize, serde::Deserialize, diesel_pgrx::DieselPGRX)]
#[cfg_attr(
feature = "pgrx",
derive(pgrx::PostgresType, pgrx::PostgresEq, pgrx::PostgresOrd, pgrx::PostgresHash)
)]
#[cfg_attr(feature = "pgrx", pg_binary_protocol)]
#[derive(
Debug,
Copy,
Clone,
PartialEq,
Eq,
PartialOrd,
Ord,
Hash,
diesel::FromSqlRow,
diesel::AsExpression,
)]
#[diesel(sql_type = diesel_impls::MyCustomType)]
pub struct MyCustomType {
pub field: i32,
}
postgres (not default): Enables PostgreSQL backend support with binary protocol serializationsqlite (not default): Enables SQLite backend support using CBOR-encoded BINARY typepgrx: Used for documentation tests onlyThe serde_cbor crate is archived but remains the CBOR implementation used by PGRX. No known issues exist, and it will be used until PGRX adopts an alternative.