Client-side Support
Opened this issue · 1 comments
rob-gordon commented
Is this package intended to work in the browser? I'm currently trying to initialize an IndexedDB-backed PGlite database on the client-side, but create-migrator
seems to rely on packages which expect to be run in node.
My code:
import { createMigrator, KyselyPGlite } from "kysely-pglite";
import { Dialect, Kysely } from "kysely";
import { openDB } from "idb";
import type { DB } from "./schema";
const DB_NAME = "my-db";
const DB_VERSION = 1;
async function initializeDatabase() {
// Open or create the IndexedDB database
await openDB(DB_NAME, DB_VERSION);
// Create a KyselyPGlite instance backed by IndexedDB
const dialect = new KyselyPGlite(`idb://${DB_NAME}`);
// Create the Kysely instance
const db = new Kysely<DB>({
dialect: dialect as unknown as Dialect,
log(event) {
console.log(event.query.sql);
console.log(event.query.parameters);
},
});
// Create the migrator
const migrator = createMigrator(db, "src/db/migrations");
// Run migrations
console.log("Migrating to latest");
const { error, results } = await migrator.migrateToLatest();
if (error) {
console.error("Migration failed:", error);
} else if (results) {
console.log("Migration results:", results);
}
return db;
}
// Export a promise that resolves to the db instance
export const dbPromise = initializeDatabase();
Next is unable to build this because the migrator has packages in it's dependency tree which expect to have fs
present.
Module not found: Can't resolve 'fs'
Did you mean './fs'?
Requests that should resolve in the current directory need to start with './'.
Requests that start with a name are treated as module requests and resolve within module directories (node_modules).
If changing the source code is not an option there is also a resolve options called 'preferRelative' which tries to resolve these kind of requests in the current directory too.
https://nextjs.org/docs/messages/module-not-found
Import trace for requested module:
./node_modules/@nodelib/fs.scandir/out/settings.js
./node_modules/@nodelib/fs.scandir/out/index.js
./node_modules/@nodelib/fs.walk/out/settings.js
./node_modules/@nodelib/fs.walk/out/index.js
./node_modules/fast-glob/out/readers/async.js
./node_modules/fast-glob/out/providers/async.js
./node_modules/fast-glob/out/index.js
./node_modules/globby/index.js
./node_modules/kysely-pglite/dist/utils/create-migrator.js
./node_modules/kysely-pglite/dist/index.js
./src/db/index.ts
./src/components/HomePage.tsx
./src/app/page.tsx
⨯ ./node_modules/@nodelib/fs.scandir/out/adapters/fs.js:4:1
czeidler commented
You could try https://www.npmjs.com/package/kysely-pglite-dialect which is a minimal Kysely driver for pglite in the meantime (it also serialized concurrent connections #2).
@dnlsandiego sorry for publishing an alternative package but I haven't seen any updates recently...