diff --git a/.appveyor.yml b/.appveyor.yml deleted file mode 100644 index 50910bd..0000000 --- a/.appveyor.yml +++ /dev/null @@ -1,11 +0,0 @@ -install: - - appveyor-retry appveyor DownloadFile https://win.rustup.rs/ -FileName rustup-init.exe - - if not defined RUSTFLAGS rustup-init.exe -y --default-host x86_64-pc-windows-msvc --default-toolchain nightly - - set PATH=%PATH%;C:\Users\appveyor\.cargo\bin - - rustc -V - - cargo -V - -build: false - -test_script: - - cargo test --locked diff --git a/.cargo-ok b/.cargo-ok deleted file mode 100644 index e69de29..0000000 diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index 7a91325..0000000 --- a/.travis.yml +++ /dev/null @@ -1,69 +0,0 @@ -language: rust -sudo: false - -cache: cargo - -matrix: - include: - - # Builds with wasm-pack. - - rust: beta - env: RUST_BACKTRACE=1 - addons: - firefox: latest - chrome: stable - before_script: - - (test -x $HOME/.cargo/bin/cargo-install-update || cargo install cargo-update) - - (test -x $HOME/.cargo/bin/cargo-generate || cargo install --vers "^0.2" cargo-generate) - - cargo install-update -a - - curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh -s -- -f - script: - - cargo generate --git . --name testing - # Having a broken Cargo.toml (in that it has curlies in fields) anywhere - # in any of our parent dirs is problematic. - - mv Cargo.toml Cargo.toml.tmpl - - cd testing - - wasm-pack build - - wasm-pack test --chrome --firefox --headless - - # Builds on nightly. - - rust: nightly - env: RUST_BACKTRACE=1 - before_script: - - (test -x $HOME/.cargo/bin/cargo-install-update || cargo install cargo-update) - - (test -x $HOME/.cargo/bin/cargo-generate || cargo install --vers "^0.2" cargo-generate) - - cargo install-update -a - - rustup target add wasm32-unknown-unknown - script: - - cargo generate --git . --name testing - - mv Cargo.toml Cargo.toml.tmpl - - cd testing - - cargo check - - cargo check --target wasm32-unknown-unknown - - cargo check --no-default-features - - cargo check --target wasm32-unknown-unknown --no-default-features - - cargo check --no-default-features --features console_error_panic_hook - - cargo check --target wasm32-unknown-unknown --no-default-features --features console_error_panic_hook - - cargo check --no-default-features --features "console_error_panic_hook wee_alloc" - - cargo check --target wasm32-unknown-unknown --no-default-features --features "console_error_panic_hook wee_alloc" - - # Builds on beta. - - rust: beta - env: RUST_BACKTRACE=1 - before_script: - - (test -x $HOME/.cargo/bin/cargo-install-update || cargo install cargo-update) - - (test -x $HOME/.cargo/bin/cargo-generate || cargo install --vers "^0.2" cargo-generate) - - cargo install-update -a - - rustup target add wasm32-unknown-unknown - script: - - cargo generate --git . --name testing - - mv Cargo.toml Cargo.toml.tmpl - - cd testing - - cargo check - - cargo check --target wasm32-unknown-unknown - - cargo check --no-default-features - - cargo check --target wasm32-unknown-unknown --no-default-features - - cargo check --no-default-features --features console_error_panic_hook - - cargo check --target wasm32-unknown-unknown --no-default-features --features console_error_panic_hook - # Note: no enabling the `wee_alloc` feature here because it requires - # nightly for now. diff --git a/Cargo.toml b/Cargo.toml index 1bd5bb0..236437a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,9 +2,21 @@ name = "rexie" version = "0.1.0" authors = ["Devashish Dixit "] +license = "MIT/Apache-2.0" +description = "Rexie is an easy-to-use, futures based wrapper around IndexedDB that compiles to webassembly" +homepage = "https://github.com/devashishdxt/rexie" +repository = "https://github.com/devashishdxt/rexie" +categories = ["asynchronous", "database", "wasm", "web-programming"] +keywords = ["wasm", "indexeddb", "futures", "webassembly", "idb"] +readme = "README.md" +include = ["Cargo.toml", "src/**/*.rs", "tests/**/*.rs", "README.md"] edition = "2021" +[package.metadata.wasm-pack.profile.release] +wasm-opt = false + [lib] +path = "src/lib.rs" crate-type = ["cdylib", "rlib"] [features] @@ -46,6 +58,7 @@ wee_alloc = { version = "0.4.5", optional = true } [dev-dependencies] serde = { version = "1.0.133", features = ["derive"] } +serde_json = "1.0.74" serde-wasm-bindgen = "0.4.1" wasm-bindgen-test = "0.3.28" diff --git a/README.md b/README.md index 1e4617a..32dd7cc 100644 --- a/README.md +++ b/README.md @@ -1,69 +1,101 @@ -
+# rexie -

wasm-pack-template

+Rexie is an easy-to-use, futures based wrapper around IndexedDB that compiles to webassembly. - A template for kick starting a Rust and WebAssembly project using wasm-pack. +## Usage -

- Build Status -

+To use Rexie, you need to add the following to your `Cargo.toml`: -

- Tutorial - | - Chat -

+```toml +[dependencies] +rexie = "0.1" +``` - Built with 🦀🕸 by The Rust and WebAssembly Working Group -
+### Example + +To create a new database, you can use the [`Rexie::builder`] method: + +```rust +use rexie::*; + +async fn build_database() -> Result { + // Create a new database + let rexie = Rexie::builder("test") + // Set the version of the database to 1.0 + .version(1) + // Add an object store named `employees` + .add_object_store( + ObjectStore::new("employees") + // Set the key path to `id` + .key_path("id") + // Enable auto increment + .auto_increment(true) + // Add an index named `email` with the key path `email` with unique enabled + .add_index(Index::new("email", "email").unique(true)), + ) + // Build the database + .build() + .await?; + + // Check basic details of the database + assert_eq!(rexie.name(), "test"); + assert_eq!(rexie.version(), 1.0); + assert_eq!(rexie.store_names(), vec!["employees"]); + + Ok(rexie) +} +``` -## About +To add an employee, you can use the [`Store::add`] method after creating a [`Transaction`]: -[**📚 Read this template tutorial! 📚**][template-docs] +```rust +use rexie::*; -This template is designed for compiling Rust libraries into WebAssembly and -publishing the resulting package to NPM. +async fn add_employee(rexie: &Rexie, name: &str, email: &str) -> Result { + let transaction = rexie.transaction(&["employees"], TransactionMode::ReadWrite)?; -Be sure to check out [other `wasm-pack` tutorials online][tutorials] for other -templates and usages of `wasm-pack`. + let employees = transaction.store("employees")?; -[tutorials]: https://rustwasm.github.io/docs/wasm-pack/tutorials/index.html -[template-docs]: https://rustwasm.github.io/docs/wasm-pack/tutorials/npm-browser-packages/index.html + let employee = serde_json::json!({ + "name": name, + "email": email, + }); + let employee = serde_wasm_bindgen::to_value(&employee).unwrap(); + let employee_id = employees.add(&employee, None).await?; -## 🚴 Usage + transaction.commit().await?; + Ok(num_traits::cast(employee_id.as_f64().unwrap()).unwrap()) +} +``` -### 🐑 Use `cargo generate` to Clone this Template +To get an employee, you can use the [`Store::get`] method after creating a [`Transaction`]: -[Learn more about `cargo generate` here.](https://github.com/ashleygwilliams/cargo-generate) +```rust +use rexie::*; -``` -cargo generate --git https://github.com/rustwasm/wasm-pack-template.git --name my-project -cd my-project -``` +async fn get_employee(rexie: &Rexie, id: u32) -> Result> { + let transaction = rexie.transaction(&["employees"], TransactionMode::ReadOnly)?; -### 🛠️ Build with `wasm-pack build` + let employees = transaction.store("employees")?; -``` -wasm-pack build + let employee = employees.get(&id.into()).await?; + let employee: Option = serde_wasm_bindgen::from_value(employee).unwrap(); + + transaction.commit().await?; + Ok(employee) +} ``` -### 🔬 Test in Headless Browsers with `wasm-pack test` +## License -``` -wasm-pack test --headless --firefox -``` +Licensed under either of -### 🎁 Publish to NPM with `wasm-pack publish` +- Apache License, Version 2.0 ([LICENSE-APACHE](LICENSE-APACHE)) +- MIT license ([LICENSE-MIT](LICENSE-MIT)) -``` -wasm-pack publish -``` +at your option. -## 🔋 Batteries Included +## Contribution -* [`wasm-bindgen`](https://github.com/rustwasm/wasm-bindgen) for communicating - between WebAssembly and JavaScript. -* [`console_error_panic_hook`](https://github.com/rustwasm/console_error_panic_hook) - for logging panic messages to the developer console. -* [`wee_alloc`](https://github.com/rustwasm/wee_alloc), an allocator optimized - for small code size. +Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as +defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions. diff --git a/README.tpl b/README.tpl new file mode 100644 index 0000000..7b1e6b3 --- /dev/null +++ b/README.tpl @@ -0,0 +1,17 @@ +# {{crate}} + +{{readme}} + +## License + +Licensed under either of + +- Apache License, Version 2.0 ([LICENSE-APACHE](LICENSE-APACHE)) +- MIT license ([LICENSE-MIT](LICENSE-MIT)) + +at your option. + +## Contribution + +Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as +defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions. \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs index a5f2fde..5372f24 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,17 +1,87 @@ -//! ## Usage -//! -//! ```rust,ignore -//! let rexie = Rexie::builder("backend") -//! .version(1) -//! .add_object_store( -//! ObjectStore::new("employees") -//! .key_path("id") -//! .add_index(Index::new("email", "email").unique(true)), -//! ) -//! .build() -//! .await?; -//! -//! web_sys::console::log_1(&rexie.name().into()); +//! Rexie is an easy-to-use, futures based wrapper around IndexedDB that compiles to webassembly. +//! +//! # Usage +//! +//! To use Rexie, you need to add the following to your `Cargo.toml`: +//! +//! ```toml +//! [dependencies] +//! rexie = "0.1" +//! ``` +//! +//! ## Example +//! +//! To create a new database, you can use the [`Rexie::builder`] method: +//! +//! ```rust +//! use rexie::*; +//! +//! async fn build_database() -> Result { +//! // Create a new database +//! let rexie = Rexie::builder("test") +//! // Set the version of the database to 1.0 +//! .version(1) +//! // Add an object store named `employees` +//! .add_object_store( +//! ObjectStore::new("employees") +//! // Set the key path to `id` +//! .key_path("id") +//! // Enable auto increment +//! .auto_increment(true) +//! // Add an index named `email` with the key path `email` with unique enabled +//! .add_index(Index::new("email", "email").unique(true)), +//! ) +//! // Build the database +//! .build() +//! .await?; +//! +//! // Check basic details of the database +//! assert_eq!(rexie.name(), "test"); +//! assert_eq!(rexie.version(), 1.0); +//! assert_eq!(rexie.store_names(), vec!["employees"]); +//! +//! Ok(rexie) +//! } +//! ``` +//! +//! To add an employee, you can use the [`Store::add`] method after creating a [`Transaction`]: +//! +//! ```rust +//! use rexie::*; +//! +//! async fn add_employee(rexie: &Rexie, name: &str, email: &str) -> Result { +//! let transaction = rexie.transaction(&["employees"], TransactionMode::ReadWrite)?; +//! +//! let employees = transaction.store("employees")?; +//! +//! let employee = serde_json::json!({ +//! "name": name, +//! "email": email, +//! }); +//! let employee = serde_wasm_bindgen::to_value(&employee).unwrap(); +//! let employee_id = employees.add(&employee, None).await?; +//! +//! transaction.commit().await?; +//! Ok(num_traits::cast(employee_id.as_f64().unwrap()).unwrap()) +//! } +//! ``` +//! +//! To get an employee, you can use the [`Store::get`] method after creating a [`Transaction`]: +//! +//! ```rust +//! use rexie::*; +//! +//! async fn get_employee(rexie: &Rexie, id: u32) -> Result> { +//! let transaction = rexie.transaction(&["employees"], TransactionMode::ReadOnly)?; +//! +//! let employees = transaction.store("employees")?; +//! +//! let employee = employees.get(&id.into()).await?; +//! let employee: Option = serde_wasm_bindgen::from_value(employee).unwrap(); +//! +//! transaction.commit().await?; +//! Ok(employee) +//! } //! ``` mod error; mod index;