feat: add base database management
This commit is contained in:
parent
1d5a517395
commit
1e3c235b78
11 changed files with 132 additions and 20 deletions
|
|
@ -13,7 +13,10 @@ pub enum Error {
|
|||
|
||||
#[error(transparent)]
|
||||
Sqlx(#[from] sqlx::Error),
|
||||
|
||||
|
||||
#[error(transparent)]
|
||||
Migration(#[from] sqlx::migrate::MigrateError),
|
||||
|
||||
#[error(transparent)]
|
||||
Io(#[from] tokio::io::Error),
|
||||
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
use std::fs::File;
|
||||
use clap::Parser;
|
||||
use readwise_bulk_upload::config::Args;
|
||||
use readwise_bulk_upload::readwise::Document;
|
||||
use readwise_bulk_upload::readwise::DocumentPayload;
|
||||
use readwise_bulk_upload::sql::get_database;
|
||||
use readwise_bulk_upload::{Error, Result};
|
||||
|
||||
|
|
@ -15,7 +15,7 @@ async fn main() -> Result<()> {
|
|||
args.path().display()
|
||||
)))?;
|
||||
|
||||
let documents: Vec<Document> = serde_json::from_reader(file)?;
|
||||
let documents: Vec<DocumentPayload> = serde_json::from_reader(file)?;
|
||||
|
||||
let db = get_database().await?;
|
||||
|
||||
|
|
|
|||
|
|
@ -1,29 +1,23 @@
|
|||
use serde::{de, Deserialize, Deserializer};
|
||||
use chrono::{DateTime, Local};
|
||||
use serde::{Deserialize, Deserializer, de};
|
||||
use serde_json::Value;
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct Document {
|
||||
#[serde(deserialize_with = "str_to_int")]
|
||||
id: u64,
|
||||
title: String ,
|
||||
note: Option<String>,
|
||||
excerpt: Option<String>,
|
||||
pub struct DocumentPayload {
|
||||
title: String,
|
||||
summary: Option<String>,
|
||||
url: String,
|
||||
folder: String,
|
||||
#[serde(deserialize_with = "single_or_vec")]
|
||||
tags: Vec<String>,
|
||||
created: DateTime<Local>,
|
||||
cover: Option<String>,
|
||||
#[serde(deserialize_with = "str_to_bool")]
|
||||
favorite: bool
|
||||
published_date: DateTime<Local>,
|
||||
location: String,
|
||||
}
|
||||
|
||||
fn str_to_int<'de, D: Deserializer<'de>>(deserializer: D) -> Result<u64, D::Error> {
|
||||
Ok(match Value::deserialize(deserializer)? {
|
||||
Value::String(s) => s.parse().map_err(de::Error::custom)?,
|
||||
Value::Number(num) => num.as_u64().ok_or(de::Error::custom("Invalid number"))?,
|
||||
_ => return Err(de::Error::custom("wrong type"))
|
||||
_ => return Err(de::Error::custom("wrong type")),
|
||||
})
|
||||
}
|
||||
|
||||
|
|
@ -31,15 +25,15 @@ fn str_to_bool<'de, D: Deserializer<'de>>(deserializer: D) -> Result<bool, D::Er
|
|||
Ok(match Value::deserialize(deserializer)? {
|
||||
Value::String(s) => s.parse().map_err(de::Error::custom)?,
|
||||
Value::Bool(b) => b,
|
||||
_ => return Err(de::Error::custom("wrong type"))
|
||||
_ => return Err(de::Error::custom("wrong type")),
|
||||
})
|
||||
}
|
||||
|
||||
fn single_or_vec<'de, D: Deserializer<'de>>(deserializer: D) -> Result<Vec<String>, D::Error> {
|
||||
Ok(match Value::deserialize(deserializer)? {
|
||||
Value::String(s) => vec!(s.parse().map_err(de::Error::custom)?),
|
||||
Value::String(s) => vec![s.parse().map_err(de::Error::custom)?],
|
||||
Value::Array(arr) => arr.into_iter().map(|a| a.to_string()).collect(),
|
||||
Value::Null => Vec::new(),
|
||||
_ => return Err(de::Error::custom("wrong type"))
|
||||
_ => return Err(de::Error::custom("wrong type")),
|
||||
})
|
||||
}
|
||||
|
|
|
|||
|
|
@ -18,6 +18,8 @@ pub async fn get_database() -> crate::Result<SqlitePool> {
|
|||
.journal_mode(SqliteJournalMode::Wal);
|
||||
|
||||
let pool = SqlitePool::connect_with(opts).await?;
|
||||
|
||||
sqlx::migrate!("./migrations").run(&pool).await?;
|
||||
|
||||
Ok(pool)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue