Atproto, add sqlx support

Moves sqlx to workspace so this touches DB and workspace
This commit is contained in:
Julia Lange 2025-06-16 15:54:20 -07:00
parent 8195ded307
commit c1b5b774d5
Signed by: Julia
SSH key fingerprint: SHA256:5DJcfxa5/fKCYn57dcabJa2vN2e6eT0pBerYi5SUbto
6 changed files with 49 additions and 0 deletions

2
Cargo.lock generated
View file

@ -118,6 +118,7 @@ dependencies = [
"lazy-regex", "lazy-regex",
"serde", "serde",
"serde_json", "serde_json",
"sqlx",
"thiserror 2.0.12", "thiserror 2.0.12",
"time", "time",
"tracing", "tracing",
@ -566,6 +567,7 @@ name = "db"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"async-trait", "async-trait",
"atproto",
"sqlx", "sqlx",
"tokio", "tokio",
] ]

View file

@ -7,6 +7,7 @@ async-trait = "0.1.88"
atproto = { path = "./atproto" } atproto = { path = "./atproto" }
serde = "1.0.219" serde = "1.0.219"
serde_json = "1.0.140" serde_json = "1.0.140"
sqlx = { version = "0.8.6", features = ["postgres", "runtime-tokio"] }
thiserror = "2.0.12" thiserror = "2.0.12"
tokio = { version = "1.45.0", features = ["macros", "rt-multi-thread"] } tokio = { version = "1.45.0", features = ["macros", "rt-multi-thread"] }
tracing = "0.1.41" tracing = "0.1.41"

View file

@ -8,7 +8,12 @@ atrium-api = { version = "0.25.3", default-features = false }
lazy-regex = "3.4.1" lazy-regex = "3.4.1"
serde.workspace = true serde.workspace = true
serde_json.workspace = true serde_json.workspace = true
sqlx = { workspace = true, optional = true }
time = { version = "0.3.41", features = ["parsing", "formatting"] } time = { version = "0.3.41", features = ["parsing", "formatting"] }
tracing-subscriber.workspace = true tracing-subscriber.workspace = true
tracing.workspace = true tracing.workspace = true
thiserror.workspace = true thiserror.workspace = true
[features]
default = []
sqlx-support = ["dep:sqlx"]

View file

@ -1,3 +1,5 @@
pub mod lexicons; pub mod lexicons;
pub mod types; pub mod types;
pub mod error; pub mod error;
#[cfg(feature = "sqlx-support")]
pub mod sqlx;

38
atproto/src/sqlx.rs Normal file
View file

@ -0,0 +1,38 @@
use crate::types::{
Did,
Cid,
Uri,
Handle,
Datetime,
};
macro_rules! implement_sqlx_for_string_type {
($name:ident) => {
impl sqlx::Type<sqlx::Postgres> for $name {
fn type_info() -> sqlx::postgres::PgTypeInfo {
<String as sqlx::Type<sqlx::Postgres>>::type_info()
}
}
impl<'q> sqlx::Encode<'q, sqlx::Postgres> for $name {
fn encode_by_ref(
&self, buf: &mut sqlx::postgres::PgArgumentBuffer
) -> Result<sqlx::encode::IsNull, sqlx::error::BoxDynError> {
<String as sqlx::Encode<sqlx::Postgres>>::encode_by_ref(&self.to_string(), buf)
}
}
impl<'r> sqlx::Decode<'r, sqlx::Postgres> for $name {
fn decode(
value: sqlx::postgres::PgValueRef<'r>
) -> Result<Self, sqlx::error::BoxDynError> {
let s = <String as sqlx::Decode<sqlx::Postgres>>::decode(value)?;
s.parse::<$name>().map_err(|e| Box::new(e) as sqlx::error::BoxDynError)
}
}
}
}
implement_sqlx_for_string_type!(Did);
implement_sqlx_for_string_type!(Cid);
implement_sqlx_for_string_type!(Uri);
implement_sqlx_for_string_type!(Handle);
implement_sqlx_for_string_type!(Datetime);

View file

@ -5,5 +5,6 @@ edition = "2024"
[dependencies] [dependencies]
async-trait.workspace = true async-trait.workspace = true
atproto = { workspace = true, features = ["sqlx-support"] }
sqlx = { version = "0.8.6", features = ["postgres", "runtime-tokio"] } sqlx = { version = "0.8.6", features = ["postgres", "runtime-tokio"] }
tokio.workspace = true tokio.workspace = true