-
Notifications
You must be signed in to change notification settings - Fork 217
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Refactor PGlite Worker to use an explicit worker process created by the user * Update README * Working multi tab with live.query() * Multi tab live.changes and live.incrementalQuery * Remove withExtensions static method * Addtional options to worker * Test for multi tab worker live queries * address feedback
- Loading branch information
Showing
16 changed files
with
1,309 additions
and
321 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,84 @@ | ||
<script type="module"> | ||
import { PGliteWorker } from "../dist/worker/index.js"; | ||
<html> | ||
<head> | ||
<title>PGlite Worker Example</title> | ||
<script type="module"> | ||
import { PGliteWorker } from "../dist/worker/index.js"; | ||
import { live } from "../dist/live/index.js"; | ||
|
||
console.log("Creating worker..."); | ||
const pg = new PGliteWorker( | ||
new Worker(new URL("./worker-process.js", import.meta.url), { | ||
type: "module", | ||
}), | ||
{ | ||
extensions: { | ||
live, | ||
}, | ||
} | ||
); | ||
|
||
console.log("Starting..."); | ||
// In-memory database: | ||
const pg = new PGliteWorker(); | ||
// Or with IndexedDB: | ||
// const pg = new PGliteWorker('idb://pgdata'); | ||
pg.onLeaderChange(() => { | ||
if (pg.isLeader) { | ||
document.title = "[leader] PGlite Worker Example"; | ||
const leader = document.getElementById("leader"); | ||
leader.textContent = "true"; | ||
leader.style.color = "green"; | ||
} | ||
}); | ||
|
||
await pg.exec(` | ||
CREATE EXTENSION IF NOT EXISTS vector; | ||
CREATE TABLE IF NOT EXISTS test ( | ||
id SERIAL PRIMARY KEY, | ||
data vector(3) | ||
); | ||
`); | ||
|
||
console.log("Waiting for ready..."); | ||
await pg.waitReady; | ||
async function insertData() { | ||
const data = [ | ||
Math.random(), | ||
Math.random(), | ||
Math.random(), | ||
]; | ||
await pg.query( | ||
`INSERT INTO test (data) VALUES ($1)`, | ||
[JSON.stringify(data)] | ||
); | ||
} | ||
|
||
console.log("Ready!"); | ||
async function clearData() { | ||
await pg.query(`DELETE FROM test`); | ||
} | ||
|
||
const btnInsert = document.querySelector("#insert"); | ||
btnInsert.addEventListener("click", insertData); | ||
|
||
console.log("Creating table..."); | ||
await pg.exec(` | ||
CREATE TABLE IF NOT EXISTS test ( | ||
id SERIAL PRIMARY KEY, | ||
name TEXT | ||
); | ||
`); | ||
const btnClear = document.querySelector("#clear"); | ||
btnClear.addEventListener("click", clearData); | ||
|
||
console.log("Inserting data..."); | ||
await pg.exec("INSERT INTO test (name) VALUES ('test');"); | ||
|
||
console.log("Selecting data..."); | ||
const res = await pg.exec(` | ||
SELECT * FROM test; | ||
`); | ||
|
||
console.log(res); | ||
|
||
// Transaction example: | ||
console.log("Transaction example..."); | ||
await pg.transaction(async (tx) => { | ||
await tx.exec("INSERT INTO test (name) VALUES ('test2');"); | ||
await tx.exec("INSERT INTO test (name) VALUES ('test3');"); | ||
}); | ||
|
||
console.log("Selecting data..."); | ||
const res2 = await pg.exec(` | ||
SELECT * FROM test; | ||
`); | ||
|
||
console.log(res2); | ||
|
||
</script> | ||
// pg.live.query( | ||
pg.live.incrementalQuery( | ||
`SELECT * FROM test`, | ||
[], | ||
'id', | ||
(data) => { | ||
const output = document.getElementById("output"); | ||
output.textContent = JSON.stringify(data.rows, null, 2); | ||
} | ||
); | ||
</script> | ||
</head> | ||
<body> | ||
<h1>PGlite Worker Example</h1> | ||
<p>Leader: <span id="leader" style="color: red;">false</span></p> | ||
<p> | ||
<button id="insert"> | ||
Insert Data | ||
</button> | ||
<button id="clear"> | ||
Clear Data | ||
</button> | ||
</p> | ||
<pre id="output"></pre> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
de4b87a
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🚀 Deployed on https://66aa5ddf9340c400828060e1--pglite-dev-demos.netlify.app