Code Monkey home page Code Monkey logo

Comments (1)

KnowZero avatar KnowZero commented on May 31, 2024 3

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.