Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Client-side Support #3

Open
rob-gordon opened this issue Oct 2, 2024 · 0 comments
Open

Client-side Support #3

rob-gordon opened this issue Oct 2, 2024 · 0 comments

Comments

@rob-gordon
Copy link

rob-gordon commented Oct 2, 2024

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant