pglite icon indicating copy to clipboard operation
pglite copied to clipboard

Drizzle's relational query builder not working with worker

Open bastiankistner opened this issue 4 months ago • 2 comments

I've setup pglite with idb and alternatively with opfs-ahp/worker and drizzle. Usually there should be a .query.* available for the drizzle client when you pass in the schema as secondary options argument, which is the case for indexeddb. But it's empty, when I initialize it with the worker.

This is my worker

import { PGlite } from '@electric-sql/pglite';
import { live } from '@electric-sql/pglite/live';
import { OpfsAhpFS } from '@electric-sql/pglite/opfs-ahp';
import { worker } from '@electric-sql/pglite/worker';

await worker({
	async init() {
		return await PGlite.create({
			extensions: { live },
			fs: new OpfsAhpFS('/dummy/abc'),
		});
	},
});

and this is the code for the client

import * as schema from '@/drizzle/pglite';
import { PGliteWorker } from '@electric-sql/pglite/worker';
import { drizzle } from 'drizzle-orm/pglite';
import { live } from '@electric-sql/pglite/live';

const pg = await PGliteWorker.create(
	new Worker(new URL('./worker/index.ts', import.meta.url), {
		type: 'module',
	}),
	{
		extensions: {
			live,
		},
	},
);

// @ts-expect-error should work
const db = drizzle(pg, { schema });

Then, later on

console.log(db.query) // empty object

however, when I use the indexeddb version

import { IdbFs, PGlite } from '@electric-sql/pglite';
import { live } from '@electric-sql/pglite/live';

const pgIdb = await PGlite.create({
	extensions: { live },
	fs: new IdbFs('pglite-db'),
});

const dbIdb = drizzle(pgIdb, { schema: dt });

// and later

console.log(dbIdb.query) // this contains my schema tables

query is properly set to reflect my schema.

Am I missing something or is this not supposed to work?

below is a screenshot of a console log where it's also obvious that the schema was not set

image

bastiankistner avatar Oct 17 '24 19:10 bastiankistner