refactor: Update public key column type from String
to PublicKey
#29
8 changed files with 31 additions and 17 deletions
|
@ -57,7 +57,7 @@ impl OutChatRequestsExt for DatabaseConnection {
|
|||
) -> ServerResult<Option<OutChatRequestsModel>> {
|
||||
requester
|
||||
.find_related(OutChatRequestsEntity)
|
||||
.filter(OutChatRequestsColumn::Recipient.eq(recipient.to_string()))
|
||||
.filter(OutChatRequestsColumn::Recipient.eq(recipient))
|
||||
.one(self)
|
||||
.await
|
||||
.map_err(Into::into)
|
||||
|
|
|
@ -63,7 +63,7 @@ impl UserTableExt for DatabaseConnection {
|
|||
#[logcall]
|
||||
async fn get_user_by_pubk(&self, public_key: &PublicKey) -> ServerResult<Option<UserModel>> {
|
||||
UserEntity::find()
|
||||
.filter(UserColumn::PublicKey.eq(public_key.to_string()))
|
||||
.filter(UserColumn::PublicKey.eq(public_key))
|
||||
.one(self)
|
||||
.await
|
||||
.map_err(Into::into)
|
||||
|
|
|
@ -297,7 +297,7 @@ async fn get_user_status(
|
|||
user.find_related(UsersStatusEntity)
|
||||
.filter(
|
||||
UsersStatusColumn::Target
|
||||
.eq(target_public_key.to_string())
|
||||
.eq(target_public_key)
|
||||
.and(UsersStatusColumn::Status.eq(status)),
|
||||
)
|
||||
.one(conn)
|
||||
|
|
|
@ -22,10 +22,8 @@
|
|||
//! Implemented SeaORM support for core types, enabling the use of these types
|
||||
//! as column types in SeaORM
|
||||
|
||||
use std::str::FromStr;
|
||||
|
||||
use sea_orm::{
|
||||
sea_query::{ArrayType, ValueType, ValueTypeErr},
|
||||
sea_query::{ArrayType, BlobSize, ValueType, ValueTypeErr},
|
||||
ColumnType,
|
||||
DbErr,
|
||||
QueryResult,
|
||||
|
@ -38,22 +36,38 @@ use super::PublicKey;
|
|||
|
||||
impl From<PublicKey> for Value {
|
||||
fn from(public_key: PublicKey) -> Self {
|
||||
Self::String(Some(Box::new(public_key.to_string())))
|
||||
public_key.as_bytes().as_slice().into()
|
||||
}
|
||||
}
|
||||
awiteb marked this conversation as resolved
|
||||
|
||||
impl From<&PublicKey> for Value {
|
||||
fn from(public_key: &PublicKey) -> Self {
|
||||
public_key.as_bytes().as_slice().into()
|
||||
}
|
||||
}
|
||||
|
||||
awiteb marked this conversation as resolved
Amjad50
commented
replace
replace `map` by `and_then` and you won't need `and_then(|res| res)`
```rust
<String as TryGetable>::try_get_by(res, idx).and_then(|v| {
PublicKey::from_str(&v).map_err(|err| TryGetError::DbErr(DbErr::Type(err.to_string())))
})
```
|
||||
impl TryGetable for PublicKey {
|
||||
fn try_get_by<I: sea_orm::ColIdx>(res: &QueryResult, idx: I) -> Result<Self, TryGetError> {
|
||||
<String as TryGetable>::try_get_by(res, idx).and_then(|v| {
|
||||
PublicKey::from_str(&v).map_err(|err| TryGetError::DbErr(DbErr::Type(err.to_string())))
|
||||
let db_err = |err: &str| TryGetError::DbErr(DbErr::Type(err.to_owned()));
|
||||
|
||||
<Vec<u8> as TryGetable>::try_get_by(res, idx).and_then(|v| {
|
||||
v.try_into()
|
||||
.map_err(|_| db_err("Invalid binary length"))
|
||||
.and_then(|bytes| {
|
||||
<PublicKey as TryFrom<[u8; 33]>>::try_from(bytes)
|
||||
.map_err(|_| db_err("Invalid Public Key"))
|
||||
})
|
||||
awiteb marked this conversation as resolved
Amjad50
commented
same as above, same as above, `and_then` instead of `map`
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl ValueType for PublicKey {
|
||||
fn try_from(v: Value) -> Result<Self, ValueTypeErr> {
|
||||
<String as ValueType>::try_from(v)
|
||||
.and_then(|v| PublicKey::from_str(&v).map_err(|_| ValueTypeErr))
|
||||
<Vec<u8> as ValueType>::try_from(v).and_then(|v| {
|
||||
v.try_into().map_err(|_| ValueTypeErr).and_then(|bytes| {
|
||||
<PublicKey as TryFrom<[u8; 33]>>::try_from(bytes).map_err(|_| ValueTypeErr)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
fn type_name() -> String {
|
||||
|
@ -61,10 +75,10 @@ impl ValueType for PublicKey {
|
|||
}
|
||||
|
||||
fn array_type() -> ArrayType {
|
||||
ArrayType::String
|
||||
ArrayType::Bytes
|
||||
}
|
||||
|
||||
fn column_type() -> ColumnType {
|
||||
ColumnType::String(None)
|
||||
ColumnType::Binary(BlobSize::Blob(None))
|
||||
}
|
||||
}
|
||||
|
|
|
@ -57,7 +57,7 @@ impl MigrationTrait for Migration {
|
|||
.on_update(ForeignKeyAction::NoAction)
|
||||
.on_delete(ForeignKeyAction::Cascade),
|
||||
)
|
||||
.col(ColumnDef::new(InChatRequests::Sender).string().not_null())
|
||||
.col(ColumnDef::new(InChatRequests::Sender).binary().not_null())
|
||||
.col(
|
||||
ColumnDef::new(InChatRequests::InOn)
|
||||
.timestamp_with_time_zone()
|
||||
|
|
|
@ -59,7 +59,7 @@ impl MigrationTrait for Migration {
|
|||
)
|
||||
.col(
|
||||
ColumnDef::new(OutChatRequests::Recipient)
|
||||
.string()
|
||||
.binary()
|
||||
.not_null(),
|
||||
)
|
||||
.col(
|
||||
|
|
|
@ -65,7 +65,7 @@ impl MigrationTrait for Migration {
|
|||
.on_update(ForeignKeyAction::NoAction)
|
||||
.on_delete(ForeignKeyAction::Cascade),
|
||||
)
|
||||
.col(ColumnDef::new(UsersStatus::Target).string().not_null())
|
||||
.col(ColumnDef::new(UsersStatus::Target).binary().not_null())
|
||||
.col(
|
||||
ColumnDef::new(UsersStatus::Status)
|
||||
.enumeration(
|
||||
|
|
|
@ -43,7 +43,7 @@ impl MigrationTrait for Migration {
|
|||
)
|
||||
.col(
|
||||
ColumnDef::new(Users::PublicKey)
|
||||
.string()
|
||||
.binary()
|
||||
.not_null()
|
||||
.unique_key(),
|
||||
)
|
||||
|
|
Loading…
Reference in a new issue
Wouldn't it be better to use
Bytes
type? easier to convert as its just copying bytes and making sure its the length expected and maybe more efficientYes I think so, it is actually a good idea.