Changed deserialization from surrealdb to rust code
This commit is contained in:
parent
24008ed668
commit
aa1063a344
@ -17,7 +17,7 @@ axum = { version = "0.7", optional = true }
|
|||||||
axum-extra = { version = "0.9", features = ["cookie"], optional = true }
|
axum-extra = { version = "0.9", features = ["cookie"], optional = true }
|
||||||
time = { version = "0.3", optional = true }
|
time = { version = "0.3", optional = true }
|
||||||
once_cell = { version = "1.19", optional = true }
|
once_cell = { version = "1.19", optional = true }
|
||||||
surrealdb = { version = "2.0", features = [], optional = true }
|
surrealdb = { version = "2.0", features = ["kv-rocksdb"], optional = true }
|
||||||
thiserror = { version = "1.0" }
|
thiserror = { version = "1.0" }
|
||||||
|
|
||||||
csv = { version = "1.3", optional = true }
|
csv = { version = "1.3", optional = true }
|
||||||
@ -29,5 +29,5 @@ manganis = "0.2"
|
|||||||
|
|
||||||
[features]
|
[features]
|
||||||
default = []
|
default = []
|
||||||
server = [ "dioxus/axum", "tokio", "axum", "axum-extra", "time", "once_cell", "surrealdb/kv-rocksdb", "csv" ]
|
server = [ "dioxus/axum", "tokio", "axum", "axum-extra", "time", "once_cell", "surrealdb", "csv" ]
|
||||||
web = ["dioxus/web", "surrealdb"]
|
web = ["dioxus/web"]
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
#[cfg(feature = "server")]
|
#[cfg(feature = "server")]
|
||||||
use crate::util::surrealdb::DB;
|
use crate::util::surrealdb::{thing_to_string, DB};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
#[cfg(feature = "server")]
|
#[cfg(feature = "server")]
|
||||||
@ -7,6 +7,7 @@ mod migration;
|
|||||||
|
|
||||||
#[derive(Debug, Deserialize, Serialize, PartialEq, Eq, Clone)]
|
#[derive(Debug, Deserialize, Serialize, PartialEq, Eq, Clone)]
|
||||||
pub struct Member {
|
pub struct Member {
|
||||||
|
#[cfg_attr(feature = "server", serde(deserialize_with = "thing_to_string"))]
|
||||||
pub id: String,
|
pub id: String,
|
||||||
pub name: Name,
|
pub name: Name,
|
||||||
pub hours: Vec<String>,
|
pub hours: Vec<String>,
|
||||||
@ -31,7 +32,7 @@ pub struct MembersMigration {
|
|||||||
#[cfg(feature = "server")]
|
#[cfg(feature = "server")]
|
||||||
impl Member {
|
impl Member {
|
||||||
pub async fn fetch_all() -> Result<Vec<Self>, surrealdb::Error> {
|
pub async fn fetch_all() -> Result<Vec<Self>, surrealdb::Error> {
|
||||||
let mut res = DB.query("SELECT record::id(id) as id, name.first, name.full, hours, groups, diploma, registration_token FROM member").await?;
|
let mut res = DB.query("SELECT id, name.first, name.full, hours, groups, diploma, registration_token FROM member").await?;
|
||||||
|
|
||||||
let members: Vec<Self> = res.take(0)?;
|
let members: Vec<Self> = res.take(0)?;
|
||||||
|
|
||||||
@ -42,7 +43,7 @@ impl Member {
|
|||||||
registration_token: String,
|
registration_token: String,
|
||||||
) -> Result<Option<Self>, surrealdb::Error> {
|
) -> Result<Option<Self>, surrealdb::Error> {
|
||||||
let mut res = DB
|
let mut res = DB
|
||||||
.query("SELECT record::id(id) as id, name.first, name.full, hours, groups, diploma, registration_token FROM ONLY member WHERE registration_token = $registration_token LIMIT 1")
|
.query("SELECT id, name.first, name.full, hours, groups, diploma, registration_token FROM ONLY member WHERE registration_token = $registration_token LIMIT 1")
|
||||||
.bind(("registration_token", registration_token))
|
.bind(("registration_token", registration_token))
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
@ -54,7 +55,7 @@ impl Member {
|
|||||||
pub async fn fetch_from_user(user_id: String) -> Result<Vec<Self>, crate::Error> {
|
pub async fn fetch_from_user(user_id: String) -> Result<Vec<Self>, crate::Error> {
|
||||||
let mut res = DB
|
let mut res = DB
|
||||||
.query(
|
.query(
|
||||||
"SELECT VALUE array::map(->user_to_member->member.*, |$obj| {{ id: record::id($obj.id), }}) FROM ONLY type::thing('user', $user_id)",
|
"SELECT VALUE ->user_to_member->member.* FROM ONLY type::thing('user', $user_id)",
|
||||||
)
|
)
|
||||||
.bind(("user_id", user_id))
|
.bind(("user_id", user_id))
|
||||||
.await?;
|
.await?;
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
#[cfg(feature = "server")]
|
#[cfg(feature = "server")]
|
||||||
use crate::util::surrealdb::DB;
|
use crate::util::surrealdb::{thing_to_string, DB};
|
||||||
use dioxus::prelude::*;
|
use dioxus::prelude::*;
|
||||||
|
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
@ -8,7 +8,9 @@ use super::user::User;
|
|||||||
|
|
||||||
#[derive(Debug, Deserialize, Serialize, PartialEq, Eq, Clone)]
|
#[derive(Debug, Deserialize, Serialize, PartialEq, Eq, Clone)]
|
||||||
pub struct Session {
|
pub struct Session {
|
||||||
|
#[cfg_attr(feature = "server", serde(deserialize_with = "thing_to_string"))]
|
||||||
id: String,
|
id: String,
|
||||||
|
#[cfg_attr(feature = "server", serde(deserialize_with = "thing_to_string"))]
|
||||||
user_id: String,
|
user_id: String,
|
||||||
expires: i64,
|
expires: i64,
|
||||||
token: String,
|
token: String,
|
||||||
@ -18,7 +20,7 @@ pub struct Session {
|
|||||||
impl Session {
|
impl Session {
|
||||||
pub async fn new(user_id: String) -> Result<Self, crate::Error> {
|
pub async fn new(user_id: String) -> Result<Self, crate::Error> {
|
||||||
let mut res = DB
|
let mut res = DB
|
||||||
.query("CREATE ONLY session SET user = type::thing('user', $user_id) RETURN record::id(id) as id, record::id(user) as user_id, time::unix(expires) as expires, token;")
|
.query("CREATE ONLY session SET user = type::thing('user', $user_id) RETURN id, user as user_id, time::unix(expires) as expires, token;")
|
||||||
.bind(("user_id", user_id))
|
.bind(("user_id", user_id))
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
@ -59,7 +61,9 @@ impl Session {
|
|||||||
|
|
||||||
pub async fn fetch_user_from_token(session_token: String) -> Result<User, crate::Error> {
|
pub async fn fetch_user_from_token(session_token: String) -> Result<User, crate::Error> {
|
||||||
let mut res = DB
|
let mut res = DB
|
||||||
.query("SELECT record::id(user) as id, user.email as email FROM session WHERE token = $session_token")
|
.query(
|
||||||
|
"SELECT user as id, user.email as email FROM session WHERE token = $session_token",
|
||||||
|
)
|
||||||
.bind(("session_token", session_token))
|
.bind(("session_token", session_token))
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
|
@ -1,12 +1,13 @@
|
|||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
#[cfg(feature = "server")]
|
#[cfg(feature = "server")]
|
||||||
use crate::util::surrealdb::DB;
|
use crate::util::surrealdb::{thing_to_string, DB};
|
||||||
#[cfg(feature = "server")]
|
#[cfg(feature = "server")]
|
||||||
use surrealdb::sql::statements::{BeginStatement, CommitStatement};
|
use surrealdb::sql::statements::{BeginStatement, CommitStatement};
|
||||||
|
|
||||||
#[derive(Debug, Deserialize, Serialize, PartialEq, Eq, Clone)]
|
#[derive(Debug, Deserialize, Serialize, PartialEq, Eq, Clone)]
|
||||||
pub struct User {
|
pub struct User {
|
||||||
|
#[cfg_attr(feature = "server", serde(deserialize_with = "thing_to_string"))]
|
||||||
pub id: String,
|
pub id: String,
|
||||||
pub email: String,
|
pub email: String,
|
||||||
password: Option<String>,
|
password: Option<String>,
|
||||||
@ -21,7 +22,7 @@ impl User {
|
|||||||
) -> Result<Self, crate::Error> {
|
) -> Result<Self, crate::Error> {
|
||||||
// Create user record
|
// Create user record
|
||||||
let mut transaction = DB.query(BeginStatement::default())
|
let mut transaction = DB.query(BeginStatement::default())
|
||||||
.query("let $user = CREATE ONLY user SET email = $email, password = crypto::argon2::generate($password) RETURN record::id(id) as id, email;")
|
.query("let $user = CREATE ONLY user SET email = $email, password = crypto::argon2::generate($password) RETURN id, email;")
|
||||||
.bind(("email", email))
|
.bind(("email", email))
|
||||||
.bind(("password", password));
|
.bind(("password", password));
|
||||||
|
|
||||||
@ -49,7 +50,7 @@ impl User {
|
|||||||
password: String,
|
password: String,
|
||||||
) -> Result<String, crate::Error> {
|
) -> Result<String, crate::Error> {
|
||||||
let mut res = DB
|
let mut res = DB
|
||||||
.query("SELECT VALUE record::id(id) as id FROM user WHERE email = $email AND crypto::argon2::compare(password, $password)")
|
.query("SELECT VALUE id FROM user WHERE email = $email AND crypto::argon2::compare(password, $password)")
|
||||||
.bind(("email", email)).bind(("password", password))
|
.bind(("email", email)).bind(("password", password))
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
|
@ -1,9 +1,19 @@
|
|||||||
use once_cell::sync::Lazy;
|
use once_cell::sync::Lazy;
|
||||||
|
use serde::{Deserialize, Deserializer};
|
||||||
use surrealdb::engine::local::{Db, RocksDb};
|
use surrealdb::engine::local::{Db, RocksDb};
|
||||||
|
use surrealdb::sql::Thing;
|
||||||
use surrealdb::Surreal;
|
use surrealdb::Surreal;
|
||||||
|
|
||||||
pub static DB: Lazy<Surreal<Db>> = Lazy::new(|| Surreal::init());
|
pub static DB: Lazy<Surreal<Db>> = Lazy::new(|| Surreal::init());
|
||||||
|
|
||||||
|
pub fn thing_to_string<'de, D>(deserializer: D) -> Result<String, D::Error>
|
||||||
|
where
|
||||||
|
D: Deserializer<'de>,
|
||||||
|
{
|
||||||
|
let t = Thing::deserialize(deserializer)?;
|
||||||
|
Ok(t.id.to_raw())
|
||||||
|
}
|
||||||
|
|
||||||
pub async fn initialize() -> surrealdb::Result<()> {
|
pub async fn initialize() -> surrealdb::Result<()> {
|
||||||
DB.connect::<RocksDb>("./database").await?;
|
DB.connect::<RocksDb>("./database").await?;
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user