K
Kysely•9mo ago
Saif

How to use kysely in edge runtime (like NextJS 14 middleware)?

Error: The edge runtime does not support Node.js 'crypto' module. Learn More: https://nextjs.org/docs/messages/node-module-in-edge-runtime Full log: https://pastebin.com/JSXBNRxv I need to verify if the user has access to the resources they are trying to visit, the middleware seemed to be the best place for this. However, I am not able to query the database as it is throwing the above error.
Pastebin
web:dev: âš  ../../node_modules/.pnpm/[email protected]/node_modules/pg/lib/...
Pastebin.com is the number one paste tool since 2002. Pastebin is a website where you can store text online for a set period of time.
4 Replies
koskimas
koskimas•9mo ago
Kysely doesn't import the crypto module. That error comes from somewhere else.
Saif
SaifOP•9mo ago
It's from the pg package:
import { Database } from "@/lib/kysely/types";
import { Pool } from "pg";
import { Kysely, PostgresDialect } from "kysely";

const dbClientSingleton = () => {
const dialect = new PostgresDialect({
pool: new Pool({
max: 10,
connectionString: process.env.DATABASE_URL!,
}),
});

return new Kysely<Database>({
log(event) {
if (event.level === "query") {
console.log(event.query.sql);
console.log(event.query.parameters);
}
},
dialect,
});
};

declare global {
var db: undefined | ReturnType<typeof dbClientSingleton>;
}

const db = globalThis.db ?? dbClientSingleton();

export default db;

if (process.env.NODE_ENV !== "production") globalThis.db = db;
import { Database } from "@/lib/kysely/types";
import { Pool } from "pg";
import { Kysely, PostgresDialect } from "kysely";

const dbClientSingleton = () => {
const dialect = new PostgresDialect({
pool: new Pool({
max: 10,
connectionString: process.env.DATABASE_URL!,
}),
});

return new Kysely<Database>({
log(event) {
if (event.level === "query") {
console.log(event.query.sql);
console.log(event.query.parameters);
}
},
dialect,
});
};

declare global {
var db: undefined | ReturnType<typeof dbClientSingleton>;
}

const db = globalThis.db ?? dbClientSingleton();

export default db;

if (process.env.NODE_ENV !== "production") globalThis.db = db;
Igal (mobile)
Igal (mobile)•9mo ago
Hey 👋 Try using kysely-postgres-js instead of the core dialect.
Saif
SaifOP•9mo ago
Sure, let me try
Want results from more Discord servers?
Add your server