在serverless中使用prisma

262 阅读1分钟

在serverless中使用prisma

众所周知为了冷启动速度一般要将代码打包到一个js文件。

当在项目中使用了 prisma 打包完后上传到serverless平台,一般会遇到第一个问题:

 Error: 
 Invalid `prisma.companie.findMany()` invocation:


Query engine binary for current platform "debian-openssl-1.1.x" could not be found.
This probably happens, because you built Prisma Client on a different platform.
(Prisma Client looked in "/home/ubuntu/nacho/server/node_modules/@prisma/client/runtime/query-engine-debian-openssl-1.1.x")




To solve this problem, add the platform "debian-openssl-1.1.x" to the "generator" block in the "schema.prisma" file:
generator client {
provider      = "prisma-client-js"
binaryTargets = ["native"]
}




Then run "prisma generate" for your changes to take effect.
Read more about deploying Prisma Client: https://pris.ly/d/client-generator
at PrismaClientFetcher.request (/home/ubuntu/nacho/server/node_modules/@prisma/client/runtime/index.js:79361:15)
at process._tickCallback (internal/process/next_tick.js:68:7) clientVersion: '2.12.1' }

Then run "prisma generate" for your changes to take effect. Read more about deploying Prisma Client: pris.ly/d/client-ge… at PrismaClientFetcher.request (/home/ubuntu/nacho/server/node_modules/@prisma/client/runtime/index.js:79361:15) at process._tickCallback (internal/process/next_tick.js:68:7) clientVersion: '2.12.1' }

经过搜索后得知 prisma/issues/4769 ,prisma 在不同平台是依赖不同二进制文件的。

那么现在修改 prisma 配置重新生成代码再 编译打包上传运行 依旧会出现找不到该文件的问题。

例如:

PrismaClientInitializationError: Prisma Client could not locate the Query Engine for runtime "debian-openssl-1.1.x".

This is likely caused by a bundler that has not copied "libquery_engine-debian-openssl-1.1.x.so.node" next to the resulting bundle. Ensure that "libquery_engine-debian-openssl-1.1.x.so.node" has been copied next to the bundle or in "node_modules/.pnpm/@prisma+client@5.7.1_prisma@5.7.1/node_modules/.prisma/client".

We would appreciate if you could take the time to share some information with us. Please help us by answering a few questions: pris.ly/engine-not-…

The following locations have been searched: /code/node_modules/.pnpm/@prisma+client@5.7.1_prisma@5.7.1/node_modules/.prisma/client / D:\code\node_modules.pnpm@prisma+client@5.7.1_prisma@5.7.1\node_modules@prisma\client /.prisma/client /tmp/prisma-engines at dl (/code/fc.js:170:805) at async Object.loadLibrary (/code/fc.js:217:9816) at async zt.loadEngine (/code/fc.js:219:448) at async zt.instantiateLibrary (/code/fc.js:218:10356) { clientVersion: '5.7.1', errorCode: undefined }

现在需要从本地找到该文件 libquery_engine-debian-openssl-1.1.x.so.node

然后和代码一起上传,注意路径要和你编译后的代码中所依赖的一致

image

📝完结撒花🎉