Skip to content

Commit 48d125c

Browse files
committed
feat: pglite web workers with extension support in v0.2.0
1 parent 97d4f37 commit 48d125c

File tree

5 files changed

+53
-20
lines changed

5 files changed

+53
-20
lines changed

apps/db-service/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"psql": "psql 'host=localhost port=5432 user=postgres sslmode=verify-ca sslrootcert=ca-cert.pem'"
1010
},
1111
"dependencies": {
12-
"@electric-sql/pglite": "0.2.0-alpha.3",
12+
"@electric-sql/pglite": "0.2.0-alpha.6",
1313
"pg-gateway": "^0.2.5-alpha.2"
1414
},
1515
"devDependencies": {

apps/postgres-new/lib/db.ts renamed to apps/postgres-new/lib/db/index.ts

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
import { PGlite, PGliteInterface, Transaction } from '@electric-sql/pglite'
2-
import { vector } from '@electric-sql/pglite/vector'
1+
import type { PGliteInterface, PGliteOptions, Transaction } from '@electric-sql/pglite'
2+
import { PGliteWorker } from '@electric-sql/pglite/worker'
3+
34
import { codeBlock } from 'common-tags'
5+
import { nanoid } from 'nanoid'
46

57
export type Database = {
68
id: string
@@ -17,6 +19,28 @@ const databaseConnections = new Map<string, Promise<PGliteInterface> | undefined
1719

1820
getRuntimePgVersion()
1921

22+
/**
23+
* Creates a PGlite instance that runs in a web worker
24+
*/
25+
async function createPGlite(dataDir?: string, options?: PGliteOptions) {
26+
if (typeof window === 'undefined') {
27+
throw new Error('PGlite worker instances are only available in the browser')
28+
}
29+
30+
return PGliteWorker.create(
31+
// Note the below syntax is required by webpack in order to
32+
// identify the worker properly during static analysis
33+
// see: https://webpack.js.org/guides/web-workers/
34+
new Worker(new URL('./worker.ts', import.meta.url), { type: 'module' }),
35+
{
36+
// If no data dir passed (in-memory), just create a unique ID (leader election purposes)
37+
id: dataDir ?? nanoid(),
38+
dataDir,
39+
...options,
40+
}
41+
)
42+
}
43+
2044
export async function getMetaDb() {
2145
if (metaDbPromise) {
2246
return await metaDbPromise
@@ -25,11 +49,7 @@ export async function getMetaDb() {
2549
async function run() {
2650
await handleUnsupportedPGVersion('meta')
2751

28-
const metaDb = new PGlite(`idb://meta`, {
29-
extensions: {
30-
vector,
31-
},
32-
})
52+
const metaDb = await createPGlite('idb://meta')
3353
await metaDb.waitReady
3454
await runMigrations(metaDb, metaMigrations)
3555
return metaDb
@@ -65,11 +85,7 @@ export async function getDb(id: string) {
6585

6686
await handleUnsupportedPGVersion(dbPath)
6787

68-
const db = new PGlite(`idb://${dbPath}`, {
69-
extensions: {
70-
vector,
71-
},
72-
})
88+
const db = await createPGlite(`idb://${dbPath}`)
7389
await db.waitReady
7490
await runMigrations(db, migrations)
7591

@@ -231,7 +247,7 @@ export async function getRuntimePgVersion() {
231247
}
232248

233249
// Create a temp DB
234-
const db = new PGlite()
250+
const db = await createPGlite()
235251

236252
const {
237253
rows: [{ version }],

apps/postgres-new/lib/db/worker.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { PGlite } from '@electric-sql/pglite'
2+
import { vector } from '@electric-sql/pglite/vector'
3+
import { PGliteWorkerOptions, worker } from '@electric-sql/pglite/worker'
4+
5+
worker({
6+
async init(options: PGliteWorkerOptions) {
7+
return new PGlite({
8+
...options,
9+
extensions: {
10+
...options.extensions,
11+
12+
// vector extension needs to be passed directly in the worker vs. main thread
13+
vector,
14+
},
15+
})
16+
},
17+
})

apps/postgres-new/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"dependencies": {
1212
"@ai-sdk/openai": "^0.0.21",
1313
"@dagrejs/dagre": "^1.1.2",
14-
"@electric-sql/pglite": "0.2.0-alpha.3",
14+
"@electric-sql/pglite": "0.2.0-alpha.6",
1515
"@gregnr/postgres-meta": "^0.82.0-dev.2",
1616
"@monaco-editor/react": "^4.6.0",
1717
"@radix-ui/react-accordion": "^1.2.0",

package-lock.json

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)