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

How do I load the module in my application? #15

Closed
rhashimoto opened this issue May 16, 2021 · 0 comments
Closed

How do I load the module in my application? #15

rhashimoto opened this issue May 16, 2021 · 0 comments
Labels
faq Frequently asked question

Comments

@rhashimoto
Copy link
Owner

Applications should import the Emscripten module factory and the SQLite API:

// Emscripten module factory (most applications use only one)
import SQLiteESMFactory from "wa-sqlite/dist/wa-sqlite.mjs";
import SQLiteAsyncESMFactory from "wa-sqlite/dist/wa-sqlite-async.mjs";

// SQLite API constants and functions
import * as SQLite from 'wa-sqlite';

Most applications will only use one of the Emscripten builds, either the synchronous build or the asynchronous build. See this question for an explanation of the difference.

The Emscripten module factory is a function that asynchronously loads, compiles, and initializes the corresponding WebAssembly file. Both the module (.mjs) and WebAssembly (.wasm) files are located in the dist/ subdirectory of the wa-sqlite package. If the Emscripten module factory is called with no arguments, then the .wasm file will be loaded from the same directory path as the .mjs file, e.g.:

const module = await SQLiteESMFactory();

That works fine if you're serving the Emscripten module as-is to the browser but bundling can complicate matters (especially when targeting a Worker). If you need the ability to explicitly specify the .wasm URL, do that by passing a configuration argument to the Emscripten module factory:

const module = await SQLiteESMFactory({
  // When provided a filename, return the URL for that filename.
  // This example uses the same directory as the document.
  locateFile(file) {
    return `./${file}`;
  }
});

Once you have an Emscripten module instance, you can create an API instance:

const sqlite3 = SQLite.Factory(module);

// And you're off and running...
const db = await sqlite3.open_v2('myDB');
...

See also the reference docs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
faq Frequently asked question
Projects
None yet
Development

No branches or pull requests

1 participant