Skip to content

Add storage crate #59

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ readme = "README.md"
version = "0.1.0"

[dependencies]
gloo-timers = { version = "0.1.0", path = "crates/timers" }
gloo-console-timer = { version = "0.1.0", path = "crates/console-timer" }
gloo-storage = { version = "0.1.0", path = "crates/storage" }
gloo-timers = { version = "0.1.0", path = "crates/timers" }

[features]
default = []
Expand Down
32 changes: 32 additions & 0 deletions crates/storage/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
[package]
name = "gloo-storage"
version = "0.1.0"
authors = ["Rust and WebAssembly Working Group"]
edition = "2018"

[dependencies]
futures = "0.1.25"
js-sys = "0.3.17"
serde_json = { optional = true, version = "1.0" }
wasm-bindgen = "0.2.40"
wasm-bindgen-futures = "0.3.17"
web-sys = "0.3.17"

[dev-dependencies]
wasm-bindgen-test = "0.2.37"

[features]
default = ["indexed-db"]
indexed-db = [
"web-sys/IdbDatabase",
"web-sys/IdbFactory",
"web-sys/IdbIndex",
"web-sys/IdbIndexParameters",
"web-sys/IdbObjectStore",
"web-sys/IdbOpenDbOptions",
"web-sys/IdbOpenDbRequest",
"web-sys/IdbRequest",
"web-sys/IdbTransaction",
"web-sys/Window"
]
json-storage = ["serde_json", "web-sys/Storage", "web-sys/Window"]
162 changes: 162 additions & 0 deletions crates/storage/src/indexed_db.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
use crate::{Index, Storage, TableDefinition, TableManager, Version};
use wasm_bindgen::{prelude::*, JsCast};
use web_sys::{IdbDatabase, IdbFactory, IdbIndexParameters, IdbObjectStore, IdbOpenDbOptions};

/// IndexedDB
#[derive(Debug)]
pub struct IndexedDb {
database_name: String,
factory: IdbFactory,
}

impl IndexedDb {
/// Creates a new instance for database `name`.
pub fn new<I>(name: I) -> Self
where
I: Into<String>,
{
Self {
database_name: name.into(),
factory: web_sys::window()
.unwrap_throw()
.indexed_db()
.unwrap_throw()
.unwrap_throw(),
}
}
}

impl Storage for IndexedDb {
type TableManager = IndexedDbTableManager;
type Version = IndexedDbVersion;

fn add_version<F, I>(&mut self, version: I, cb: F)
where
F: FnOnce(Self::Version) -> Self::Version + 'static,
I: Into<f64>,
{
let request = self
.factory
.open_with_idb_open_db_options(
&self.database_name,
IdbOpenDbOptions::new().version(version.into()),
)
.unwrap_throw();
let result = request.result();
let closure = Closure::once(move || {
let database = result.unwrap_throw().unchecked_into::<IdbDatabase>();
cb(IndexedDbVersion { database })
});
let func = closure.as_ref().unchecked_ref::<js_sys::Function>();
request.set_onupgradeneeded(Some(func));
}

fn delete(&mut self) {
self.factory
.delete_database(&self.database_name)
.unwrap_throw();
}

fn name(&self) -> &str {
&self.database_name
}

fn table_manager(&mut self, _: &str) -> Self::TableManager {
unimplemented!();
}

fn transaction(&mut self) {
unimplemented!();
}
}

/// IndexedDB table definition
#[derive(Debug)]
pub struct IndexedDbTableDefinition {
object_store: IdbObjectStore,
}

impl TableDefinition for IndexedDbTableDefinition {
fn add_row_with_index(self, name: &str, index: Index) -> Self {
let mut params = IdbIndexParameters::new();
match index {
Index::MultiEntry => {
params.multi_entry(true);
}
Index::Unique => {
params.unique(true);
}
_ => {}
};
self.object_store
.create_index_with_str_and_optional_parameters(name, name, &params)
.unwrap_throw();
self
}

fn remove_old_row(self, name: &str) -> Self {
self.object_store.delete(&name.into()).unwrap_throw();;
self
}
}

/// IndexedDB table manager
#[derive(Debug)]
pub struct IndexedDbTableManager();

impl TableManager for IndexedDbTableManager {
fn get_all() {
unimplemented!();
}

fn push() {
unimplemented!();
}
}

/// IndexedDB version
#[wasm_bindgen]
#[derive(Debug)]
pub struct IndexedDbVersion {
database: web_sys::IdbDatabase,
}

impl Version for IndexedDbVersion {
type TableDefinition = IndexedDbTableDefinition;

fn add_table(self, name: &str) -> Self {
self.database.create_object_store(name).unwrap_throw();
self
}

fn add_and_update_table<F>(self, name: &str, cb: F) -> Self
where
F: FnMut(Self::TableDefinition) -> Self::TableDefinition,
{
self.add_table(name).update_table(name, cb)
}

fn remove_table(self, name: &str) -> Self {
self.database.delete_object_store(name).unwrap_throw();
self
}

fn update_table<F>(self, name: &str, mut cb: F) -> Self
where
F: FnMut(Self::TableDefinition) -> Self::TableDefinition,
{
cb(IndexedDbTableDefinition {
object_store: self
.database
.transaction_with_str(name)
.unwrap_throw()
.object_store(name)
.unwrap(),
});
self
}

fn update_version(self) -> Self {
unimplemented!();
}
}
Loading