Skip to content

Commit

Permalink
Unify idb-sys and idb into one crate which are enabled with featu…
Browse files Browse the repository at this point in the history
…res (#19)

* Unify idb-sys and idb into one crate which are enabled with features

* Remove lock file copy
  • Loading branch information
devashishdxt committed Jan 25, 2024
1 parent b4c6e00 commit 8d20e87
Show file tree
Hide file tree
Showing 87 changed files with 2,111 additions and 2,079 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/audit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ jobs:
audit:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions-rs/audit-check@v1
- uses: actions/checkout@v4
- uses: rustsec/audit-check@v1.4.1
with:
token: ${{ secrets.GITHUB_TOKEN }}
28 changes: 9 additions & 19 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,37 +12,27 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout sources
uses: actions/checkout@v3
uses: actions/checkout@v4
- name: Install stable rust toolchain
uses: actions-rs/toolchain@v1
uses: dtolnay/rust-toolchain@master
with:
profile: minimal
toolchain: stable
target: wasm32-unknown-unknown
targets: wasm32-unknown-unknown
- name: Run cargo build
uses: actions-rs/cargo@v1
with:
command: build
run: cargo build --target wasm32-unknown-unknown
lint:
name: Lint
runs-on: ubuntu-latest
steps:
- name: Checkout sources
uses: actions/checkout@v3
uses: actions/checkout@v4
- name: Install stable rust toolchain
uses: actions-rs/toolchain@v1
uses: dtolnay/rust-toolchain@master
with:
profile: minimal
toolchain: stable
targets: wasm32-unknown-unknown
components: rustfmt, clippy
target: wasm32-unknown-unknown
- name: Run cargo fmt
uses: actions-rs/cargo@v1
with:
command: fmt
args: --all -- --check
run: cargo fmt --all -- --check
- name: Run cargo clippy
uses: actions-rs/cargo@v1
with:
command: clippy
args: -- -D warnings
run: cargo clippy -- -D warnings
14 changes: 6 additions & 8 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,12 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout sources
uses: actions/checkout@v3
uses: actions/checkout@v4
- name: Install stable rust toolchain
uses: actions-rs/toolchain@v1
uses: dtolnay/rust-toolchain@master
with:
profile: minimal
toolchain: stable
target: wasm32-unknown-unknown
targets: wasm32-unknown-unknown
- name: Install wasm-pack
uses: jetli/wasm-pack-action@v0.4.0
- name: Install just
Expand All @@ -30,13 +29,12 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout sources
uses: actions/checkout@v3
uses: actions/checkout@v4
- name: Install stable rust toolchain
uses: actions-rs/toolchain@v1
uses: dtolnay/rust-toolchain@master
with:
profile: minimal
toolchain: stable
target: wasm32-unknown-unknown
targets: wasm32-unknown-unknown
- name: Install wasm-pack
uses: jetli/wasm-pack-action@v0.4.0
- name: Install just
Expand Down
64 changes: 61 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,61 @@
[workspace]
members = ["idb", "idb-sys"]
resolver = "2"
[package]
name = "idb"
version = "0.6.0"
authors = ["Devashish Dixit <devashishdxt@gmail.com>"]
license = "MIT/Apache-2.0"
description = "A futures based crate for interacting with IndexedDB on browsers using webassembly"
homepage = "https://github.com/devashishdxt/idb"
repository = "https://github.com/devashishdxt/idb"
categories = ["asynchronous", "database", "wasm", "web-programming"]
keywords = ["wasm", "indexeddb", "future", "webassembly", "idb"]
readme = "README.md"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[features]
default = ["futures"]
doc = []
futures = ["tokio"]

[dependencies]
js-sys = "0.3.67"
num-traits = "0.2.17"
thiserror = "1.0.56"
tokio = { version = "1.35.1", features = ["sync"], optional = true }
wasm-bindgen = "0.2.90"
web-sys = { version = "0.3.67", features = [
"DomException",
"DomStringList",
"Event",
"IdbCursor",
"IdbCursorDirection",
"IdbCursorWithValue",
"IdbDatabase",
"IdbFactory",
"IdbIndex",
"IdbIndexParameters",
"IdbKeyRange",
"IdbObjectStore",
"IdbObjectStoreParameters",
"IdbOpenDbOptions",
"IdbOpenDbRequest",
"IdbRequestReadyState",
"IdbTransaction",
"IdbTransactionMode",
"IdbVersionChangeEvent",
"StorageType",
] }

[dev-dependencies]
gloo = { version = "0.11.0", default-features = false, features = [
"timers",
"futures",
] }
serde = "1.0.195"
serde-wasm-bindgen = "0.6.3"
serde_json = "1.0.111"
wasm-bindgen-test = "0.3.40"

[package.metadata.docs.rs]
all-features = true
8 changes: 4 additions & 4 deletions Justfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,19 @@ alias test := test-chrome
# Runs browser tests for `idb` using chrome
test-chrome:
@echo 'Testing...'
cd idb && wasm-pack test --chrome
wasm-pack test --chrome

# Runs browser tests for `idb` using chrome (intended for use in CI)
test-chrome-headless:
@echo 'Testing...'
cd idb && wasm-pack test --headless --chrome
wasm-pack test --headless --chrome

# Runs browser tests for `idb` using firefox (intended for use in CI)
test-firefox-headless:
@echo 'Testing...'
cd idb && wasm-pack test --headless --firefox
wasm-pack test --headless --firefox

# Generate readme from doc comments
readme:
@echo 'Generating README...'
cd idb && cargo readme > ../README.md
cargo readme > ../README.md
37 changes: 25 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,27 @@ A futures based crate for interacting with IndexedDB on browsers using webassemb

## Usage

To use `idb`, you need to add the following to your `Cargo.toml`:
To use `idb`, you need to run following command from your project root:

```toml
[dependencies]
idb = "0.5"
```sh
cargo add idb
```

If you don't want to use `async`/`await` syntax, you can disable the `futures` feature using:

```sh
cargo add idb --no-default-features
```

After disabling the `futures` feature, you can use `on_success` and `on_error` methods on requests to attach
callbacks.

### Example

To create a new database, you can use [`Factory::open`]:

```rust
use idb::{Database, Error, Factory};
use idb::{Database, DatabaseEvent, Error, Factory, IndexParams, KeyPath, ObjectStoreParams};

async fn create_database() -> Result<Database, Error> {
// Get a factory instance from global scope
Expand Down Expand Up @@ -58,9 +66,10 @@ async fn create_database() -> Result<Database, Error> {
To add data to an object store, you can use [`ObjectStore::add`]:

```rust
use idb::{Database, Error};
use idb::{Database, Error, TransactionMode};
use serde::Serialize;
use serde_wasm_bindgen::Serializer;
use wasm_bindgen::JsValue;

async fn add_data(database: &Database) -> Result<JsValue, Error> {
// Create a read-write transaction
Expand All @@ -85,7 +94,7 @@ async fn add_data(database: &Database) -> Result<JsValue, Error> {
.await?;

// Commit the transaction
transaction.commit().await?;
transaction.commit()?.await?;

Ok(id)
}
Expand All @@ -94,7 +103,11 @@ async fn add_data(database: &Database) -> Result<JsValue, Error> {
To get data from an object store, you can use [`ObjectStore::get`]:

```rust
async fn get_data(database: &Database, id: JsValue) -> Result<Option<serde_json::Value>, Error> {
use idb::{Database, Error, TransactionMode};
use serde_json::Value;
use wasm_bindgen::JsValue;

async fn get_data(database: &Database, id: JsValue) -> Result<Option<Value>, Error> {
// Create a read-only transaction
let transaction = database
.transaction(&["employees"], TransactionMode::ReadOnly)
Expand All @@ -104,14 +117,14 @@ async fn get_data(database: &Database, id: JsValue) -> Result<Option<serde_json:
let store = transaction.object_store("employees").unwrap();

// Get the stored data
let stored_employee: Option<JsValue> = store.get(id).await?;
let stored_employee: Option<JsValue> = store.get(id)?.await?;

// Deserialize the stored data
let stored_employee: Option<serde_json::Value> = stored_employee
let stored_employee: Option<Value> = stored_employee
.map(|stored_employee| serde_wasm_bindgen::from_value(stored_employee).unwrap());

// Wait for the transaction to complete
transaction.done().await?;
// Wait for the transaction to complete (alternatively, you can also commit the transaction)
transaction.await?;

Ok(stored_employee)
}
Expand Down
File renamed without changes.
42 changes: 0 additions & 42 deletions idb-sys/Cargo.toml

This file was deleted.

1 change: 0 additions & 1 deletion idb-sys/README.md

This file was deleted.

5 changes: 0 additions & 5 deletions idb-sys/src/cursor/mod.rs

This file was deleted.

28 changes: 0 additions & 28 deletions idb-sys/src/lib.rs

This file was deleted.

Loading

0 comments on commit 8d20e87

Please sign in to comment.