()` method can take a TypeScript type describing the expected shap
### Web Workers:
-It's likely that you will want to run PGlite in a Web Worker so that it doesn't block the main thread. To aid in this we provide a `PGliteWorker` with the same API as the core `PGlite` but it runs Postgres in a dedicated Web Worker. To use, import from the `/worker` export:
+It's likely that you will want to run PGlite in a Web Worker so that it doesn't block the main thread. To aid in this we provide a `PGliteWorker` with the same API as the core `PGlite` but it runs Postgres in a dedicated Web Worker.
+
+First you need to create a js file for your worker instance, initiate PGlite with the worker extension, and start it:
+
+```js
+// my-pglite-worker.js
+import { PGlite } from "@electric-sql/pglite";
+import { worker } from "@electric-sql/pglite/worker";
+
+worker({
+ async init() {
+ return new PGlite();
+ },
+});
+```
+
+Then connect the `PGliteWorker` to your new worker process:
```js
import { PGliteWorker } from "@electric-sql/pglite/worker";
-const pg = new PGliteWorker('idb://my-database');
+const pg = new PGliteWorker(
+ new Worker(new URL("./my-pglite-worker.js", import.meta.url), {
+ type: "module",
+ })
+);
+
await pg.exec(`
CREATE TABLE IF NOT EXISTS test (
id SERIAL PRIMARY KEY,
diff --git a/packages/pglite/examples/opfs-worker.js b/packages/pglite/examples/opfs-worker.js
new file mode 100644
index 00000000..9d03d3d3
--- /dev/null
+++ b/packages/pglite/examples/opfs-worker.js
@@ -0,0 +1,12 @@
+import { PGlite } from "../dist/index.js";
+import { worker } from "../dist/worker/index.js";
+
+worker({
+ async init() {
+ const pg = new PGlite("opfs-ahp://my-test-db2");
+ // If you want run any specific setup code for the worker process, you can do it here.
+ return pg;
+ },
+});
+
+console.log("Worker process started");
diff --git a/packages/pglite/examples/opfs.html b/packages/pglite/examples/opfs.html
new file mode 100644
index 00000000..cf3cb6a1
--- /dev/null
+++ b/packages/pglite/examples/opfs.html
@@ -0,0 +1,49 @@
+
diff --git a/packages/pglite/examples/worker-process.js b/packages/pglite/examples/worker-process.js
new file mode 100644
index 00000000..ac51d9ab
--- /dev/null
+++ b/packages/pglite/examples/worker-process.js
@@ -0,0 +1,17 @@
+import { PGlite } from "../dist/index.js";
+import { worker } from "../dist/worker/index.js";
+import { vector } from "../dist/vector/index.js";
+
+worker({
+ async init() {
+ const pg = new PGlite({
+ extensions: {
+ vector,
+ },
+ });
+ // If you want run any specific setup code for the worker process, you can do it here.
+ return pg;
+ },
+});
+
+console.log("Worker process started");
diff --git a/packages/pglite/examples/worker.html b/packages/pglite/examples/worker.html
index 3769bfa1..f258c3b6 100644
--- a/packages/pglite/examples/worker.html
+++ b/packages/pglite/examples/worker.html
@@ -1,47 +1,84 @@
-
+ // pg.live.query(
+ pg.live.incrementalQuery(
+ `SELECT * FROM test`,
+ [],
+ 'id',
+ (data) => {
+ const output = document.getElementById("output");
+ output.textContent = JSON.stringify(data.rows, null, 2);
+ }
+ );
+
+
+
+ PGlite Worker Example
+ Leader: false
+
+
+
+
+
+
+