-
Notifications
You must be signed in to change notification settings - Fork 45
[BUG] Can not import .wasm
(by @prisma/client/wasm, and other wasm-pack generated)
#139
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
Comments
.wasm
(by @prisma/client).wasm
(by @prisma/client/wasm)
.wasm
(by @prisma/client/wasm).wasm
(by @prisma/client/wasm, and other wasm-pack generated)
Hi @mizchi, it looks like your issue is caused by Others have solved this problem by adding configuration to |
Good point, can you try adding |
I used those links to verify this by rewriting First I needed to examine the prisma code generation and runtime steps.
I found some problems here.
This is my trial and error. It hasn't completely worked yet, but it worked on hand to the point where it bypasses the initialization of wasm. First, in config.plugins.push(
new webpack.IgnorePlugin({
contextRegExp: /\\.wasm$/,
resourceRegExp: /\\.wasm$/, }
})
); At prisma/client/wasm runtime, modify the code generator to resolve from global variables. diff --git a/generator-build/index.js b/generator-build/index.js
index f0adad1914bb059450d36d8df69acd57f9c95750..056bf25d709c0ca12f0b748d58f2156f54fadec9 100644
--- a/generator-build/index.js
+++ b/generator-build/index.js
@@ -7753,10 +7753,7 @@ function buildQueryEngineWasmModule(wasm, copyEngine, runtimeNameJs) {
return `config.engineWasm = {
getRuntime: () => require('. /query_engine_bg.js'),.
getQueryEngineWasmModule: async () => {
- const queryEngineWasmFilePath = require('path').join(config.dirname, 'query_engine_bg.wasm')
- const queryEngineWasmFileBytes = require('fs').readFileSync(queryEngineWasmFilePath)
-fs'.
- return new WebAssembly.Module(queryEngineWasmFileBytes)
+ return __PRISMA_BINARY;
}
}`;
}
@@ -7764,9 +7761,7 @@ function buildQueryEngineWasmModule(wasm, copyEngine, runtimeNameJs) {
return `config.engineWasm = {
getRuntime: () => require('. /query_engine_bg.js'),.
getQueryEngineWasmModule: async () => {
- const loader = (await import('#wasm-engine-loader')).default
- const engine = (await loader).default
- return engine
+ return __PRISMA_BINARY;
}
}`;
} With this in place, copy import wasm from ". /query_engine_bg.wasm";
globalThis.__PRISMA_BINARY = await WebAssembly.instantiate(wasm);
// ...original This should now work, at least on the Workers runtime. However, we could not verify that this actually works. The reason is that it fails in the prerender phase of the Next App Router. The prerender executes There may be a better way. I will experiment some more. |
Thanks for the detailed analysis! |
I managed to use and deploy my app using import wasm from "./query_engine_bg.wasm";
globalThis.__PRISMA_BINARY = wasm // <-- Prisma would initialize it with correct imports And |
I deal with the issue on a regular basis. Prisma itself is currently being rewritten from Rust to TypeScript. https://www.prisma.io/blog/rust-to-typescript-update-boosting-prisma-orm-performance This will likely circumvent this issue. We're enabling an experimental client based on benchmarks. https://github.com/prisma/query-compiler-benchmarks/tree/main/benches/prisma-pg-qc SetupDATABASE_URL="..."
PRISMA_UNSTABLE_CLIENT_ENGINE_TYPE=true schema.prisma datasource db {
provider = "postgres"
url = env("DATABASE_URL")
}
generator client {
provider = "prisma-client-js"
previewFeatures = ["driverAdapters"]
engineType = "client"
}
Run with postgres on node.js import { PrismaClient } from "@prisma/client";
import { PrismaPg } from "@prisma/adapter-pg";
import pg from "pg";
const connectionString = process.env.DATABASE_URL ?? "";
const pool = new pg.Pool({ connectionString });
const adapter = new PrismaPg(pool);
const prisma = new PrismaClient({ adapter });
await prisma.$connect();
try {
const users = await prisma.user.findMany();
console.log("users", users);
} catch (error) {
console.error("error", error);
} finally {
await prisma.$disconnect().catch(console.error);
process.exit(0);
} This runtime allowed me to avoid using wasm, at least for postgres on localhost docker. However, it didn't work with anything other than postgres. I haven't tried it yet, but it's likely to work on opennext if you use prisma/adapte-pg-worker. |
Prisma (no-rust, 6.7.0 preview) works with OpenNext! https://github.com/prisma/prisma/releases/tag/6.7.0 It uses with generator client {
provider = "prisma-client-js"
previewFeatures = ["queryCompiler", "driverAdapters"]
output = "../src/generated/prisma"
}
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}
// sample model
model User {
id String @id @default(cuid())
name String?
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
} Generate prisma/client without runtime engines. $ npx prisma generate --no-engine
$ npx prisma migrate dev (I tried with https://www.prisma.io/postgres) // src/app/page.tsx
import { PrismaClient } from "../generated/prisma";
const prisma = new PrismaClient();
export default async function Page() {
const user = await prisma.user.findFirst({});
return (
<div>
<pre>{JSON.stringify(user, null, 2)}</pre>
</div>
);
} This doesn't solve the issue of wasm-pack generating code that can't be loaded, but it seems to solve the problem with prisma. |
I reported prisma/client/runtime/library does not work on workerd but it relates opennext bundle process about
|
@mizchi This should be fixed in 1.0.0 Also see the docs for prisma here https://opennext.js.org/cloudflare/howtos/db#prisma-orm |
I overlooked that prisma documentation. Thanks! I tried v1.0.0 and the Unfortunately, it seems that I can't use that method when I enable
$ npx prisma generate --no-engine
Warning: You did not specify an output path for your `generator` in schema.prisma. This behavior is deprecated and will no longer be supported in Prisma 7.0.0. To learn more visit https://pris.ly/cli/output-path I failed to call It seems that specifying output will be required in 7.0.0, but the experimental queryCompiler may already require it. |
@mizchi |
Describe the bug
Opennext and
@prisma/client/wasm
do not work by import*.wasm
.Prisma does not work on opennext.
Vite and webpack can handle it but wrangler's esbuild & workerd can not
Steps to reproduce
@prisma/client/wasm
npx wrangler deploy
Expected behavior
@prisma/client
works on opennext.@opennextjs/cloudflare version
0.2.1
Node.js version
22.x
Wrangler version
3.88.0
next info output
Operating System: Platform: linux Arch: x64 Version: #1 SMP Tue Nov 5 00:21:55 UTC 2024 Available memory (MB): 64163 Available CPU cores: 16 Binaries: Node: 22.9.0 npm: 10.8.3 Yarn: N/A pnpm: 9.9.0 Relevant Packages: next: 15.0.3 // Latest available version is detected (15.0.3). eslint-config-next: 15.0.3 react: 18.3.1 react-dom: 18.3.1 typescript: 5.6.3 Next.js Config: output: N/A
The text was updated successfully, but these errors were encountered: