# cloudflare-worker-sqlite-wasm

> fork of sql.js for cloudflare workers

Latest version **0.0.1-alpha.5** (published 2023-09-16) · MIT license · 0 weekly downloads

## Install

```sh
npm install cloudflare-worker-sqlite-wasm
pnpm add cloudflare-worker-sqlite-wasm
yarn add cloudflare-worker-sqlite-wasm
bun add cloudflare-worker-sqlite-wasm
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support; large bundle; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.1-alpha.5 |
| Published | 2023-09-16 |
| First published | 2023-09-10 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 32.2 MB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Maintainers | adrianlyjak |
| Keywords | sql, sqlite, stand-alone, relational, database, RDBMS, data, query, statement, emscripten, asm, asm.js |

## Links

- npm: https://www.npmjs.com/package/cloudflare-worker-sqlite-wasm
- Repository: https://github.com/adrianlyjak/cloudflare-worker-sqlite-wasm
- Homepage: http://github.com/adrianlyjak/cloudflare-worker-sqlite-wasm
- Issues: https://github.com/adrianlyjak/cloudflare-worker-sqlite-wasm/issues
- npm.io page: https://npm.io/package/cloudflare-worker-sqlite-wasm

## Alternatives

- [gamedig](https://npm.io/package/gamedig.md) — 29.3K weekly downloads
- [join-monster](https://npm.io/package/join-monster.md) — 12.8K weekly downloads
- [masked](https://npm.io/package/masked.md) — 5.5K weekly downloads
- [@comunica/actor-query-process-explain-logical](https://npm.io/package/@comunica/actor-query-process-explain-logical.md) — 4.7K weekly downloads
- [@veracity/vui](https://npm.io/package/@veracity/vui.md) — 4.6K weekly downloads

## Recent versions

- 0.0.1-alpha.5 (latest) — 2023-09-16
- 0.0.1-alpha.4 — 2023-09-16
- 0.0.1-alpha.3-debug — 2023-09-16
- 0.0.1-alpha.3 — 2023-09-16
- 0.0.1-alpha.2 — 2023-09-15
- 0.0.1-alpha.1 — 2023-09-10
- 0.0.2 — 2023-09-10
- 0.0.1 — 2023-09-10

## README

A fork of [sql.js](https://github.com/sql-js/sql.js) with some minor modifications to support running in memory
sqlite databases in cloudflare workers.

1. The standard builds of sql.js that are generated try to infer "web worker" vs "web" environment via emscripten. 
   Unfortunately, cloudflare workers are detected as a web worker, and then start throwing errors from other 
   global fields missing when trying to initialize within cloudflare.
2. Standard emscripten tries to bootstrap the worker from a dynamically loaded file. Cloudflare workers don't 
   support initializing web workers from binary, and rather need to directly `import` the web assembly module.
   This can be supported with an `instantiateWasm` config on the emscripten generated layer so long as the




```js
// functions/helloworld.js
import wasm from "../sqljs/sql-wasm.wasm";
import initSqljs from "../sqljs/sql-wasm";

export async function onRequest(context) {
  try {
    const SQL = await initSqljs({
      instantiateWasm(info, receive) {
        let instance = new WebAssembly.Instance(wasm, info);
        receive(instance);
        return instance.exports;
      },
      log: console.log,
      error: console.error,
    });

    const db = new SQL.Database();
    let sqlstr =
      "CREATE TABLE hello (a int, b char); \
INSERT INTO hello VALUES (0, 'hello'); \
INSERT INTO hello VALUES (1, 'world');";
    db.run(sqlstr); // Run the query without returning anything

    // Prepare an sql statement
    const stmt = db.prepare("SELECT * FROM hello WHERE a=:aval AND b=:bval");

    // Bind values to the parameters and fetch the results of the query
    const result = stmt.getAsObject({ ":aval": 1, ":bval": "world" });
    console.log(result); // Will print {a:1, b:'world'}
    return new Response(JSON.stringify(result), { status: 200 });
  } catch (ex) {
    console.error("failed to start", ex);
    return new Response("ouch, ", { status: 500 });
  }
}


```

---
_Source: https://npm.io/package/cloudflare-worker-sqlite-wasm · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
