You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Ziyang Hu f2cc74d284 Use github links for tutorial 2 years ago
..
src bump version and update API 2 years ago
tests compile for WASM 2 years ago
wasm-react-demo Use github links for tutorial 2 years ago
.appveyor.yml compile for WASM 2 years ago
.gitignore compile for WASM 2 years ago
.travis.yml compile for WASM 2 years ago
Cargo.toml bump version and update API 2 years ago
README.md Use github links for tutorial 2 years ago
build.sh finish web demo 2 years ago

README.md

Cozo in web assembly

This crate provides Cozo web assembly modules for browsers. If you are targeting NodeJS, use this instead: native code is still much faster than WASM.

This document describes how to set up the Cozo WASM module for use. To learn how to use CozoDB (CozoScript), follow the tutorial first and then read the manual. You can run all the queries described in the tutorial with an in-browser DB here.

Installation

npm install cozo-lib-wasm

Alternatively, you can download cozo_wasm-<VERSION>-wasm32-unknown-unknown.zip from the release page and include the JS and WASM files directly in your project: see the index.html example here for what is required in your code.

Usage

See the code here. Basically, you write

import init, {CozoDb} from "cozo-lib-wasm";

and call

let db;
init().then(() => {
    db = CozoDb.new();
    // db can only be used after the promise resolves 
})

API

export class CozoDb {
    free(): void;

    static new(): CozoDb;

    run(script: string, params: string): string;

    export_relations(data: string): string;

    import_relations(data: string): string;
}

Note that this API is synchronous. If your computation runs for a long time, it will block the main thread. If you know that some of your queries are going to be heavy, you should consider running Cozo in a web worker. However, the published module may not work across browsers in web workers (look for the row "Support for ECMAScript modules" here).

The next section contains some pointers for how to alleviate this, but expect a lot of work.

Compiling

You will need to install Rust, NodeJS with npm, and wasm-pack first.

The published module was built with

wasm-pack build --target web --release

and the environment variable CARGO_PROFILE_RELEASE_LTO=fat.

The important option is --target web: the above usage instructions only work for this target. See the documentation here.

if you are interested in running Cozo in a web worker and expect it to run across browsers, you will need to use the --target no-modules option, and write a lot of gluing code. See here for tips.