# surrealdb.worker

> Use surrealdb.js in workers!

Latest version **0.2.0** (published 2022-09-28) · MIT license · 0 weekly downloads

## Install

```sh
npm install surrealdb.worker
pnpm add surrealdb.worker
yarn add surrealdb.worker
bun add surrealdb.worker
```

## Health

**Score 35/100 (D)** — status: abandoned.

Positive: esm support; no vulnerabilities; high maintenance score.

Warnings: low downloads; no types; pre 1.0.

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 0.2.0 |
| Published | 2022-09-28 |
| First published | 2022-09-28 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | ESM |
| Dependencies | 0 |
| Unpacked size | 108.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 8 |
| Author | Sebastian Krüger |
| Maintainers | mathe42 |

## Links

- npm: https://www.npmjs.com/package/surrealdb.worker
- Repository: https://github.com/mathe42/surrealdb.worker
- Homepage: https://github.com/mathe42/surrealdb.worker#readme
- Issues: https://github.com/mathe42/surrealdb.worker/issues
- npm.io page: https://npm.io/package/surrealdb.worker

## Recent versions

- 0.2.0 (latest) — 2022-09-28
- 0.1.0 — 2022-09-28

## README

# SurrealDB in worker!

## WHY?

The main thread is full of stuff that don't need to be there. Also when you have
an Application where each client connects via WebSocket to SurrealDB that will
be a lot of open connections.

This package enables you to use 1 Connection per browser (NOT per tab). All
Connection related stuff (basicly most querys) has to be moved into a single
config object that lives in the Worker.

> NOTE: If the browser of the user supports SharedWorker it is a good idea to
> use it!

## Live Querys

This is build with live querys in mind so as soon as they are available they are
added here.

## Browser support

This needs the Lock API! See https://caniuse.com/mdn-api_lock for support.

## Size

Uncompressed (but minified) you add arround 5KB to your page. With gzip or
brotli you get that to 1-2KB!

## In worker

```ts
import { setupWorker } from "surrealdb.worker/dist/worker.js";

export const config = {
  /** */
};

setupWorker(config).then((init) => {
  // When this runs the Worker only exists once!
  // This only runs again (in a different worker) if
  // The worker is killed (because tab closed + SharedWorker not supported)
  init('http://localhost:8000/rpc'); 
});
```

## In client to start worker

```ts
import { strartWorker } from "surrealdb.worker/dist/worker.setup.js";

strartWorker(new URL("./path/to/worker/file", import.meta.url));
```

## in client to setup cliet

```ts
import { config } from "./path/to/worker/file";
import { setupClient } from "surrealdb.worker/dist/client.js";

const client = setupClient<typeof config>();

// For vue
import { vue } from "surrealdb.worker/dist/framework/vue.js";
const vueClient = vue<typeof config>(client);
```

## Frameworks

As I will use this with Vue I added a wrapper for that. I also added a react
wrapper but I don't know if that works / what the best practices for that are.

You want to add other frameworks? Feel free to create a PR for this!

## Multiple Worker
If you have multiple surrealdb instances you want to connect to or have different workers you can add a name argument to the `setupWorker`, `startWorker` and `setupClient` calls.

## Vite support
You can import a small vite plugin:

```ts
import surrealdbWorker from "surrealdb.worker/vite/plugin.js"

export default {
  /*... */
  plugins: [
    /*... */
    surrealdbWorker()
    /*... */
  ],
  /*... */
}
```

This allowes imports like this:

```ts
import setup from 'sdb.w:default:./path/to/worker/file'


const promiseThatNeverResolves = setup()
```

## Why imports like `surrealdb.worker/dist/...`
This makes it a lot more easy to have multiple entrypoints for this package!

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