Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions packages/pglite/examples/worker-process.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,20 @@ import { vector } from '../dist/vector/index.js'
worker({
async init() {
const pg = new PGlite({
dataDir: 'idb://my-pgdata',
extensions: {
vector,
},
})
// If you want run any specific setup code for the worker process, you can do it here.
console.log('Creating table...')
await pg.exec(`
CREATE EXTENSION IF NOT EXISTS vector;
CREATE TABLE IF NOT EXISTS test (
id SERIAL PRIMARY KEY,
data vector(3)
);
`)
return pg
},
})
Expand Down
33 changes: 17 additions & 16 deletions packages/pglite/examples/worker.html
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ <h2>Main Thread</h2>
<script type="module">
import { PGliteWorker } from '@electric-sql/pglite/worker'
import { live } from '@electric-sql/pglite/live'

console.log('Creating worker...')
const pg = new PGliteWorker(
new Worker(new URL('./worker-process.js', import.meta.url), {
Expand All @@ -64,19 +63,19 @@ <h2>Main Thread</h2>
const leader = document.getElementById('leader')
leader.textContent = 'true'
leader.style.color = 'green'

}
console.log('Leader has changed, clearing output ...')
output.textContent = ''
setTimeout(async () => {
console.log('requesting incrementalQuery')
let incResult = await pg.live.incrementalQuery(`SELECT * FROM test`, [], 'id', (data) => {
const output = document.getElementById('output')
output.textContent = JSON.stringify(data.rows, null, 2)
})
}, 200)
})

console.log('Creating table...')

await pg.exec(`
CREATE EXTENSION IF NOT EXISTS vector;
CREATE TABLE IF NOT EXISTS test (
id SERIAL PRIMARY KEY,
data vector(3)
);
`)

console.log('Ready!')

async function insertData() {
Expand All @@ -96,11 +95,13 @@ <h2>Main Thread</h2>
const btnClear = document.querySelector('#clear')
btnClear.addEventListener('click', clearData)

// pg.live.query(
pg.live.incrementalQuery(`SELECT * FROM test`, [], 'id', (data) => {
const output = document.getElementById('output')
output.textContent = JSON.stringify(data.rows, null, 2)
})
setTimeout(async () => {
let incResult = await pg.live.incrementalQuery(`SELECT * FROM test`, [], 'id', (data) => {
console.log('requesting incrementalQuery')
const output = document.getElementById('output')
output.textContent = JSON.stringify(data.rows, null, 2)
})
}, 200)
</script>
</div>
<div id="log"></div>
Expand Down