Skip to content

Commit

Permalink
🏗️ Split providers into sql and kv.
Browse files Browse the repository at this point in the history
  • Loading branch information
langyo committed Jul 13, 2024
1 parent cb50c9b commit 7501b59
Show file tree
Hide file tree
Showing 13 changed files with 325 additions and 22 deletions.
3 changes: 2 additions & 1 deletion examples/guest-side/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ serde = { version = "^1", features = ["derive"] }
serde_json = "^1"
anyhow = "^1"
tokio = { version = "^1", features = ["macros", "rt", "time"] }
async-trait = "^0.1"

sqlparser = "^0.45"
sea-orm = { version = "1.0.0-rc.7", features = ["proxy"] }
yew = { version = "^0.21", features = ["ssr"] }
yew-router = "^0.18"

wasi = "0.13.0"
wasi = "0.13.1"
19 changes: 7 additions & 12 deletions examples/guest-side/src/model/mod.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
pub mod entity;

use serde_json::Value;
use std::{
collections::BTreeMap,
sync::{Arc, Mutex},
};
use std::{collections::BTreeMap, sync::Arc};

use sea_orm::{
ActiveValue::Set, Database, DbBackend, DbErr, EntityTrait, ProxyDatabaseTrait, ProxyExecResult,
Expand All @@ -17,8 +14,9 @@ use tairitsu_utils::types::proto::backend::Msg;
#[derive(Debug)]
struct ProxyDb {}

#[async_trait::async_trait]
impl ProxyDatabaseTrait for ProxyDb {
fn query(&self, statement: Statement) -> Result<Vec<ProxyRow>, DbErr> {
async fn query(&self, statement: Statement) -> Result<Vec<ProxyRow>, DbErr> {
let sql = statement.sql.clone();
println!(
"{}",
Expand Down Expand Up @@ -65,7 +63,7 @@ impl ProxyDatabaseTrait for ProxyDb {
Ok(rows)
}

fn execute(&self, statement: Statement) -> Result<ProxyExecResult, DbErr> {
async fn execute(&self, statement: Statement) -> Result<ProxyExecResult, DbErr> {
let sql = {
if let Some(values) = statement.values {
// Replace all the '?' with the statement values
Expand Down Expand Up @@ -149,12 +147,9 @@ impl ProxyDatabaseTrait for ProxyDb {
}

pub async fn init() {
let db = Database::connect_proxy(
DbBackend::Sqlite,
Arc::new(Mutex::new(Box::new(ProxyDb {}))),
)
.await
.unwrap();
let db = Database::connect_proxy(DbBackend::Sqlite, Arc::new(Box::new(ProxyDb {})))
.await
.unwrap();

let data = ActiveModel {
title: Set("Homo".to_owned()),
Expand Down
13 changes: 8 additions & 5 deletions packages/database/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,23 @@ uuid = { version = "^1", features = [
async-trait = "^0.1"
oneshot = "^0.1"

[dependencies.sea-orm]
features = ["with-uuid", "with-chrono", "with-json"]
version = "1.0.0-rc.7"
tokio = { version = "^1", optional = true }
wasi = { version = "0.13.1", optional = true }

sea-orm = "1.0.0-rc.7"
sqlparser = { version = "^0.48", optional = true }

[target.'cfg(all(target_arch = "wasm32", not(target_os = "wasi")))'.dependencies]
wasm-bindgen = { version = "0.2.87", optional = true }
wasm-bindgen-futures = { version = "^0.4", optional = true }
worker = { version = "0.3.0", features = ["d1"], optional = true }

[features]
default = ["cloudflare"]
default = []
cloudflare = [
"dep:wasm-bindgen",
"dep:wasm-bindgen-futures",
"dep:worker",
"sea-orm/proxy",
]
native = ["sea-orm/sqlx-sqlite", "sea-orm/runtime-async-std-rustls"]
wasi = ["dep:sqlparser", "dep:wasi", "sea-orm/proxy"]
12 changes: 12 additions & 0 deletions packages/database/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1 +1,13 @@
pub mod providers;

#[cfg(any(
all(feature = "cloudflare", feature = "native"),
all(feature = "cloudflare", feature = "wasi"),
all(feature = "native", feature = "wasi")
))]
compile_error!(
"Only one of the `cloudflare`, `native`, or `wasi` features can be enabled at a time."
);

#[cfg(not(any(feature = "cloudflare", feature = "native", feature = "wasi")))]
compile_error!("At least one of the `cloudflare`, `native`, or `wasi` features must be enabled.");
25 changes: 25 additions & 0 deletions packages/database/src/providers/kv/cloudflare.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use std::sync::Arc;

use worker::Env;

use super::KVStore;

pub struct ProxyKV {
env: Arc<Env>,
db_name: String,
}

#[async_trait::async_trait]
impl KVStore for ProxyKV {
async fn set(&self, key: impl ToString, value: impl ToString) {
todo!()
}

async fn get(&self, key: impl ToString) -> Option<String> {
todo!()
}

async fn delete(&self, key: impl ToString) {
todo!()
}
}
19 changes: 19 additions & 0 deletions packages/database/src/providers/kv/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#[cfg(feature = "cloudflare")]
pub mod cloudflare;
#[cfg(feature = "native")]
pub mod sled;
#[cfg(feature = "wasi")]
pub mod wasmtime_wasi;

#[cfg(feature = "cloudflare")]
pub use cloudflare::*;
#[cfg(feature = "native")]
pub use sled::*;
#[cfg(feature = "wasi")]
pub use wasmtime_wasi::*;

pub trait KVStore {
async fn get(&self, key: impl ToString) -> Option<String>;
async fn set(&self, key: impl ToString, value: impl ToString);
async fn delete(&self, key: impl ToString);
}
20 changes: 20 additions & 0 deletions packages/database/src/providers/kv/sled.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
use super::KVStore;

pub struct ProxyKV {
path: String,
}

#[async_trait::async_trait]
impl KVStore for ProxyKV {
async fn set(&self, key: impl ToString, value: impl ToString) {
todo!()
}

async fn get(&self, key: impl ToString) -> Option<String> {
todo!()
}

async fn delete(&self, key: impl ToString) {
todo!()
}
}
18 changes: 18 additions & 0 deletions packages/database/src/providers/kv/wasmtime_wasi.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use super::KVStore;

pub struct ProxyKV {}

#[async_trait::async_trait]
impl KVStore for ProxyKV {
async fn set(&self, key: impl ToString, value: impl ToString) {
todo!()
}

async fn get(&self, key: impl ToString) -> Option<String> {
todo!()
}

async fn delete(&self, key: impl ToString) {
todo!()
}
}
4 changes: 2 additions & 2 deletions packages/database/src/providers/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
#[cfg(feature = "cloudflare")]
pub mod cloudflare;
pub mod kv;
pub mod sql;
Original file line number Diff line number Diff line change
Expand Up @@ -216,10 +216,13 @@ impl ProxyDatabaseTrait for ProxyDb {
}
}

pub async fn init_db(env: Arc<Env>, db_name: String) -> Result<DatabaseConnection> {
pub async fn init_db(env: Arc<Env>, db_name: impl ToString) -> Result<DatabaseConnection> {
let db = Database::connect_proxy(
DbBackend::Sqlite,
Arc::new(Box::new(ProxyDb { env, db_name })),
Arc::new(Box::new(ProxyDb {
env,
db_name: db_name.to_string(),
})),
)
.await
.context("Failed to connect to database")?;
Expand Down
13 changes: 13 additions & 0 deletions packages/database/src/providers/sql/libsqlite.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use anyhow::{Context, Result};

use sea_orm::{ConnectOptions, Database, DatabaseConnection};

pub async fn init_db(path: impl ToString) -> Result<DatabaseConnection> {
let options = ConnectOptions::new(format!("sqlite:{}", path.to_string()));

let db = Database::connect(options)
.await
.context("Failed to connect to database")?;

Ok(db)
}
13 changes: 13 additions & 0 deletions packages/database/src/providers/sql/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#[cfg(feature = "cloudflare")]
pub mod cloudflare;
#[cfg(feature = "native")]
pub mod libsqlite;
#[cfg(feature = "wasi")]
pub mod wasmtime_wasi;

#[cfg(feature = "cloudflare")]
pub use cloudflare::*;
#[cfg(feature = "native")]
pub use libsqlite::*;
#[cfg(feature = "wasi")]
pub use wasmtime_wasi::*;
Loading

0 comments on commit 7501b59

Please sign in to comment.