1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
|
use ::sqlx::{FromRow, Result};
use crate::atuin_common::record::{EncryptedData, Host, Record};
use crate::atuin_server_database::models::{History, Session, User};
use sqlx::{Row, postgres::PgRow};
use time::PrimitiveDateTime;
pub(crate) struct DbUser(pub(crate) User);
pub(crate) struct DbSession(pub(crate) Session);
pub(crate) struct DbHistory(pub(crate) History);
pub(crate) struct DbRecord(pub(crate) Record<EncryptedData>);
impl<'a> FromRow<'a, PgRow> for DbUser {
fn from_row(row: &'a PgRow) -> Result<Self> {
Ok(Self(User {
id: row.try_get("id")?,
username: row.try_get("username")?,
email: row.try_get("email")?,
password: row.try_get("password")?,
}))
}
}
impl<'a> ::sqlx::FromRow<'a, PgRow> for DbSession {
fn from_row(row: &'a PgRow) -> ::sqlx::Result<Self> {
Ok(Self(Session {
id: row.try_get("id")?,
user_id: row.try_get("user_id")?,
token: row.try_get("token")?,
}))
}
}
impl<'a> ::sqlx::FromRow<'a, PgRow> for DbHistory {
fn from_row(row: &'a PgRow) -> ::sqlx::Result<Self> {
Ok(Self(History {
id: row.try_get("id")?,
client_id: row.try_get("client_id")?,
user_id: row.try_get("user_id")?,
hostname: row.try_get("hostname")?,
timestamp: row
.try_get::<PrimitiveDateTime, _>("timestamp")?
.assume_utc(),
data: row.try_get("data")?,
created_at: row
.try_get::<PrimitiveDateTime, _>("created_at")?
.assume_utc(),
}))
}
}
impl<'a> ::sqlx::FromRow<'a, PgRow> for DbRecord {
fn from_row(row: &'a PgRow) -> ::sqlx::Result<Self> {
let timestamp: i64 = row.try_get("timestamp")?;
let idx: i64 = row.try_get("idx")?;
let data = EncryptedData {
data: row.try_get("data")?,
content_encryption_key: row.try_get("cek")?,
};
Ok(Self(Record {
id: row.try_get("client_id")?,
host: Host::new(row.try_get("host")?),
idx: idx as u64,
timestamp: timestamp as u64,
version: row.try_get("version")?,
tag: row.try_get("tag")?,
data,
}))
}
}
impl From<DbRecord> for Record<EncryptedData> {
fn from(other: DbRecord) -> Record<EncryptedData> {
Record { ..other.0 }
}
}
|