Skip to content
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

feat: wasm bindings #249

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
40 changes: 40 additions & 0 deletions bindings/js/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
[package]
name = "tonbo-js"
version = "0.1.0"
edition = "2021"

[lib]
crate-type = ["cdylib", "rlib"]

[workspace]

[dependencies]

fusio = { git = "https://github.com/tonbo-io/fusio.git", rev = "216eb446fb0a0c6e5e85bfac51a6f6ed8e5ed606", package = "fusio", version = "0.3.3", features = [
"aws",
"opfs",
] }
fusio-dispatch = { git = "https://github.com/tonbo-io/fusio.git", rev = "216eb446fb0a0c6e5e85bfac51a6f6ed8e5ed606", package = "fusio-dispatch", version = "0.2.1", features = [
"aws",
"opfs",
] }
futures = { version = "0.3" }
js-sys = { version = "0.3.72" }
tonbo = { version = "0.2.0", path = "../../", default-features = false, features = [
"bytes",
"wasm",
] }

wasm-bindgen = "0.2.95"
wasm-bindgen-futures = { version = "0.4.45" }
wasm-streams = "0.4.2"

[dev-dependencies]
wasm-bindgen = "0.2.95"
wasm-bindgen-futures = { version = "0.4.45" }
wasm-bindgen-test = "0.3.9"
web-sys = { version = "0.3", features = ["console"] }
wasm-streams = "0.4.2"

[package.metadata.wasm-pack.profile.release]
wasm-opt = false
27 changes: 27 additions & 0 deletions bindings/js/examples/db/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { userSchema } from "./schema";
import { TonboDB, DbOption, Bound } from "./pkg/tonbo_js";


const option = new DbOption("store_dir");
const db = await new TonboDB(option, userSchema);

await db.insert({ id: 0, name: "Alice" });

const id = await db.get(0, (val) => val.id);
console.log(id);

await db.transaction(async (txn) => {
txn.insert({ id: 1, name: "Bob" });
const record1 = await txn.get(1, ["id"]);
const record2 = await txn.get(0, ["id"]);
console.log(record1);
console.log(record2);
// can not read uncommitted change
const uncommitted_name = await db.get(1, (val) => val.name);
console.log("read uncommitted name: ", uncommitted_name);
await txn.commit();
const name = await db.get(1, (val) => val.name);
console.log("read committed name: ", name);
});


13 changes: 13 additions & 0 deletions bindings/js/examples/db/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"scripts": {
"build": "wasm-pack build",
"serve": "cp -r ../../pkg ./ && webpack serve"
},
"devDependencies": {
"@wasm-tool/wasm-pack-plugin": "1.5.0",
"html-webpack-plugin": "^5.6.0",
"webpack": "^5.93.0",
"webpack-cli": "^5.1.4",
"webpack-dev-server": "^5.0.4"
}
}
11 changes: 11 additions & 0 deletions bindings/js/examples/db/schema.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export const userSchema = {
id: {
primary: true,
type: "UInt8",
nullable: false,
},
name: {
type: "String",
nullable: true,
},
};
22 changes: 22 additions & 0 deletions bindings/js/examples/db/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const path = require("path");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const webpack = require("webpack");
const WasmPackPlugin = require("@wasm-tool/wasm-pack-plugin");

module.exports = {
entry: "./index.js",
output: {
path: path.resolve(__dirname, "dist"),
filename: "index.js",
},
mode: "development",
plugins: [
new HtmlWebpackPlugin(),
new WasmPackPlugin({
crateDirectory: path.resolve(__dirname, "."),
}),
],
experiments: {
asyncWebAssembly: true,
},
};
53 changes: 53 additions & 0 deletions bindings/js/src/datatype.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
use tonbo::record::Datatype;
use wasm_bindgen::prelude::wasm_bindgen;

#[wasm_bindgen]
#[repr(u8)]
#[derive(Copy, Clone, Debug)]
pub enum DataType {
UInt8 = 0,
UInt16 = 1,
UInt32 = 2,
UInt64 = 3,
Int8 = 4,
Int16 = 5,
Int32 = 6,
Int64 = 7,
String = 8,
Boolean = 9,
Bytes = 10,
}

impl From<DataType> for Datatype {
fn from(datatype: DataType) -> Self {
match datatype {
DataType::UInt8 => Datatype::UInt8,
DataType::UInt16 => Datatype::UInt16,
DataType::UInt32 => Datatype::UInt32,
DataType::UInt64 => Datatype::UInt64,
DataType::Int8 => Datatype::Int8,
DataType::Int16 => Datatype::Int16,
DataType::Int32 => Datatype::Int32,
DataType::Int64 => Datatype::Int64,
DataType::String => Datatype::String,
DataType::Boolean => Datatype::Boolean,
_ => todo!(),
}
}
}

pub(crate) fn to_datatype(datatype: &str) -> Datatype {
match datatype {
"UInt8" => Datatype::UInt8,
"UInt16" => Datatype::UInt16,
"UInt32" => Datatype::UInt32,
"UInt64" => Datatype::UInt64,
"Int8" => Datatype::Int8,
"Int16" => Datatype::Int16,
"Int32" => Datatype::Int32,
"Int64" => Datatype::Int64,
"String" => Datatype::String,
"Boolean" => Datatype::Boolean,
_ => todo!(),
}
}
Loading
Loading