部署到 Azure Functions
本指南介绍了使用 Azure 函数 将基于 Node.js 的函数应用部署到 Azure 时如何避免常见问题。
¥This guide explains how to avoid common issues when deploying a Node.js-based function app to Azure using Azure Functions.
Azure Functions 是一个无服务器部署平台。你不需要维护基础设施来部署代码。对于 Azure Functions,基本构建块是 功能应用。函数应用在 Azure 中提供运行函数的执行上下文。它由 Azure 一起管理、部署和扩展的一项或多项单独功能组成。你可以将多个功能作为单个逻辑单元进行组织和集中管理。
¥Azure Functions is a serverless deployment platform. You do not need to maintain infrastructure to deploy your code. With Azure Functions, the fundamental building block is the function app. A function app provides an execution context in Azure in which your functions run. It is comprised of one or more individual functions that Azure manages, deploys, and scales together. You can organize and collectively manage multiple functions as a single logical unit.
如果 Prisma ORM 的 Rust 引擎二进制文件导致包体积过大、构建速度缓慢或部署问题(例如,在无服务器或边缘环境中),你可以使用 generator
块的以下配置来使用它,而无需使用它们:
¥If Prisma ORM's Rust engine binaries cause large bundle sizes, slow builds, or deployment issues (for example, in serverless or edge environments), you can use it without them using this configuration of your generator
block:
generator client {
provider = "prisma-client-js" // or "prisma-client"
engineType = "client"
}
自 v6.16.0 以来,不带 Rust 二进制文件的 Prisma ORM 一直是 一般可用。
¥Prisma ORM without Rust binaries has been Generally Available since v6.16.0.
请注意,在这种情况下,你需要使用 驱动适配器。
¥Note that you need to use a driver adapter in this case.
使用此架构时:
¥When using this architecture:
-
无需下载或安装 Rust 查询引擎二进制文件。
¥No Rust query engine binary is downloaded or shipped.
-
数据库连接池由你安装的原生 JS 数据库驱动程序(例如,PostgreSQL 的
@prisma/adapter-pg
)维护。¥The database connection pool is maintained by the native JS database driver you install (e.g.,
@prisma/adapter-pg
for PostgreSQL).
此设置可以简化无服务器或边缘运行时中的部署。在 文档在这里 中了解更多信息。
¥This setup can simplify deployments in serverless or edge runtimes. Learn more in the docs here.
好奇我们为什么放弃 Rust 引擎?了解为什么我们在 博客文章 中从 Rust 二进制引擎过渡到全 TypeScript 方法,以实现更快、更轻量的 Prisma ORM。
¥Curious why we moved away from the Rust engine? Take a look at why we transitioned from Rust binary engines to an all-TypeScript approach for a faster, lighter Prisma ORM in this blog post.
先决条件
¥Prerequisites
-
使用 Prisma ORM 的现有函数应用项目
¥An existing function app project with Prisma ORM
要知道的事情
¥Things to know
虽然 Prisma ORM 可以很好地与 Azure 函数配合使用,但在部署应用之前需要注意一些事项。
¥While Prisma ORM works well with Azure functions, there are a few things to take note of before deploying your application.
定义多个二进制目标
¥Define multiple binary targets
部署函数应用时,Azure Functions 运行远程构建的操作系统与用于托管函数的操作系统不同。因此,我们建议在你的 Prisma 架构中指定以下 binaryTargets
选项:
¥When deploying a function app, the operating system that Azure functions runs a remote build is different from the one used to host your functions. Therefore, we recommend specifying the following binaryTargets
options in your Prisma schema:
generator client {
provider = "prisma-client-js"
binaryTargets = ["native", "debian-openssl-1.1.x"]
}
连接池
¥Connection pooling
通常,当你使用 FaaS(函数即服务)环境与数据库交互时,每次函数调用都会导致与数据库的新连接。对于持续运行的 Node.js 服务器来说,这不是问题。因此,池化数据库连接有利于获得更好的性能。要解决此问题,你可以使用 Prisma 加速。其他解决方案请参见 无服务器环境的连接管理指南。
¥Generally, when you use a FaaS (Function as a Service) environment to interact with a database, every function invocation can result in a new connection to the database. This is not a problem with a constantly running Node.js server. Therefore, it is beneficial to pool DB connections to get better performance. To solve this issue, you can use the Prisma Accelerate. For other solutions, see the connection management guide for serverless environments.
概括
¥Summary
要更深入地了解 Prisma Client 的 API,请探索函数处理程序并查看 Prisma 客户端 API 参考
¥For more insight into Prisma Client's API, explore the function handlers and check out the Prisma Client API Reference