Comments (1)
It is because FromSql/ToSql has been changed to Decode/Encode. It would be nice to have it updated for latest sqlx
In the meantime I made a wrapper based on uuid in sqlx
https://github.com/launchbadge/sqlx/blob/main/sqlx-postgres/src/types/uuid.rs
use serde::{Serialize, Deserialize};
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct Ulid(ulid::Ulid);
impl Ulid {
pub fn new() -> Self {
Self( ulid::Ulid::new() )
}
}
impl sqlx::Type<sqlx::Postgres> for Ulid {
fn type_info() -> sqlx::postgres::PgTypeInfo {
<&uuid::Uuid as sqlx::Type<sqlx::Postgres>>::type_info()
}
fn compatible(ty: &<sqlx::Postgres as sqlx::Database>::TypeInfo) -> bool {
<&uuid::Uuid as sqlx::Type<sqlx::Postgres>>::compatible(ty)
}
}
impl sqlx::postgres::PgHasArrayType for Ulid {
fn array_type_info() -> sqlx::postgres::PgTypeInfo {
<&Vec<uuid::Uuid> as sqlx::Type<sqlx::Postgres>>::type_info()
}
fn array_compatible(ty: &sqlx::postgres::PgTypeInfo) -> bool {
<&uuid::Uuid as sqlx::Type<sqlx::Postgres>>::compatible(ty)
}
}
impl sqlx::Encode<'_, sqlx::Postgres> for Ulid {
fn encode_by_ref(&self, buf: &mut sqlx::postgres::PgArgumentBuffer) -> sqlx::encode::IsNull {
buf.extend_from_slice(&self.0.to_bytes());
sqlx::encode::IsNull::No
}
}
impl sqlx::Decode<'_, sqlx::Postgres> for Ulid {
fn decode(value: sqlx::postgres::PgValueRef<'_>) -> Result<Self, sqlx::error::BoxDynError> {
let ulid = match value.format() {
sqlx::postgres::PgValueFormat::Binary => ulid::Ulid::from_bytes(value.as_bytes()?.try_into()?),
sqlx::postgres::PgValueFormat::Text => ulid::Ulid::from_string(value.as_str()?)?,
};
Ok(Ulid(ulid))
}
}
from ulid-rs.
Related Issues (20)
- doc suggestion: use generator in quickstart guide?
- Consider lib upgrade HOT 1
- UUIDv7 conversions HOT 1
- Investigate chrono -> time perf regressions HOT 1
- Recommended Storage For MySQL and Postgres HOT 2
- v0.6 WASM runtime error: 'time not implemented on this platform' HOT 11
- What's the difference between this and rusty_ulid? HOT 2
- Unable to compile the combination of `no_std` and the `serde` feature
- Is there a method to validate that a ULID is valid? HOT 1
- Figure out what to do with overflowing ulid values
- Add MAX and MIN Ulid values
- Expose base32 implementation? HOT 2
- postgres traits HOT 1
- to_str variable buffer size HOT 1
- Infallible `Generator` generation HOT 2
- web_time resolve error with --target wasm32-wasi HOT 2
- CVEs in CLI HOT 1
- Add support for Encode and Decode traits from both bincode and bitcode
- Generator doesn't actually guarantee 80-bits of UIDs per millisecond? HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ulid-rs.