学习利用人工智能构建可扩展的后台
可扩展 “的后端是指可以处理增加的负载而不会导致性能大幅下降的后端。
构建可扩展的后端涉及多个流程和考虑因素,如数据库设计和部署策略。
在本文中,您将了解如何借助人工智能(AI)构建可扩展的后台。
Contents
是什么让后台具有可扩展性?
在软件开发生命周期中实施的各种因素共同作用,使后端具有可扩展性。
影响后台可扩展性的一个因素是数据库查询的速度。
查询速度慢会增加服务器的响应时间,并影响其同时处理多个请求的能力。
通过实施适当的模式设计和编写高效的查询,可以加快查询速度。
异步处理是后端可扩展性的另一个因素。资源密集型任务会阻塞执行线程,限制服务器处理并发请求的能力。
您可以将大型计算和文件上传等资源密集型任务委托给后台作业来解决这个问题。
另一个因素是您的部署策略。您必须通过 Back4app 等服务使用可扩展的部署策略来部署您的后端。
例如,当流量激增时,新容器会自动启动,无需人工干预即可在多个实例之间分配负载。
同样,当需求下降时,系统也会缩小规模,释放未使用的资源。
在本教程中,您将创建一个狗舍管理应用程序,该程序将实现上述所有因素,使其具有可扩展性。
利用人工智能设计可扩展的后台
如上所述,您将构建一个狗舍管理应用程序。以下是应用程序的要求:
- 您的申请将允许狗主人为他们的狗办理入住手续。
- 当狗主人将其爱犬登记到您的狗舍时,您的应用程序将存储爱犬的详细信息,如名称、品种、年龄、主人姓名和图像。然后,它会为主人生成一个唯一的代码。
- 狗主人收到的唯一代码是访问结束后从狗舍取回爱犬的唯一途径。
- 在访问狗舍期间,您要记录狗狗参与的所有活动。本教程只包括进食、用药和梳理。
- 参观结束时,主人将提供办理入住手续时获得的唯一代码,并取回自己的爱犬。
实现上述要求的第一步是设计一个几乎没有数据冗余的规范化数据库。
您将在 Back4app AI 代理的帮助下设计和创建该数据库模式,该代理将充当 AI 后台生成器。
Back4app 人工智能代理
Back4app AI Agent是一个 LLM,可与 Back4app 产品、后端平台和网络部署平台连接。
它可以让您使用提示与这些产品进行交互,还能帮助您利用人工智能创建可扩展的后台。
要访问人工智能代理,您需要一个 Back4app 账户。如果您没有账户,可以免费注册。
登录您的 Back4app 账户,点击仪表板导航栏上的 “人工智能代理 “链接。
如下图所示,点击该链接将进入人工智能代理页面。
您可以在此页面输入各种提示,例如在您的 Back4app 账户上创建一个新的 BaaS 应用程序。
利用人工智能构建后台
要在 Back4app 上创建新的后端应用程序,可以在人工智能代理中输入以下提示或类似内容。
- Create a new backend application called "Kennel Management Backend"
您应该会收到应用程序已成功创建的回复。
您可以在 Back4app 面板上查看创建的应用程序,如下图所示。
如上图所示,除了默认的_User
和_Role
类,新后台应用程序的数据库是空的。
接下来,您将设计数据库模式并将其添加到后台应用程序中。
设计数据库
要根据上述应用要求设计规范化数据库模式,请在人工智能代理中输入以下提示或类似内容。
- Design a normalized database schema for a dog kennel management application.
- The app stores dog details: name, breed, age, a unique owner username, owner contact details, and an image;
- When an owner checks in and generates a unique code for the owner to retrieve the dog.
- It tracks activities during the visit (eating, medication, grooming).
The owner provides the unique code to retrieve their dog at the end of the visit.
- Ensure relationships between entities like dogs, owners, visits, activities, and the retrieval code are properly structured.
上述提示应返回与下图类似的数据库模式。
上述数据库模式有四个表:所有者、狗、访问和活动。一个所有者可以拥有多只狗(一对多),但每只狗只属于一个所有者,通过狗表中
的外键相连。
同样,一只狗在一段时间内可以有多次访问(一对多),每次访问
都是针对该狗的,并通过外键在访问
表中进行跟踪。
此外,每次探视可能涉及多项活动(一对多),如喂食、梳理或用药,每项活动
都通过活动
表中的visit_id
外键与特定探视相关联。
现在您已经设计好了数据库,可以使用类似下面的提示在后台提示人工智能创建数据库。
- Create the designed database in the backend app "Kennel Management Backend".
您应该会收到数据库已成功创建的回复。如下图所示,您可以在应用程序仪表板上查看新创建的表,以确认这一点。
现在,您已经创建了后台并添加了应用程序的数据库表,您将执行应用程序逻辑。
用人工智能实现后台逻辑
从应用程序的要求来看,您的应用程序应能让狗主人将狗送入狗舍,存储狗的姓名、品种、年龄、主人姓名和图像等详细信息,并为狗主人生成唯一的检索代码。
为了实现这些要求,您需要一个实用功能来生成唯一的检索代码,并在后台上传狗的图像,以确保上传任务不会拖慢应用程序的响应时间。
实现实用功能
您可以创建一个实用程序,通过向人工智能代理输入下面的提示或类似内容来生成唯一的代码:
- Create a utility cloud code function in the "Kennel Management Backend" app that generates a unique retrieval code for the dogs checked into the kennel.
-Store the code in a `utils.js` file and make the file accessible to the entire application.
您应该会收到云代码功能已创建的响应,与下图中的响应类似。
接下来,通过向人工智能代理输入以下提示或类似内容,生成上传狗狗图像并将其与相应狗狗记录关联的函数:
- Create an asynchronous utility Cloud Code function named `uploadDogImage` that accepts three parameters: `dogName`, `dogImage`, and `dogId`.
- The function should upload the `dogImage`, using `dogName` as the image name, and ensure that the image is properly associated with the corresponding dog record identified by `dogId`.
- Implement error handling to manage any issues during the upload process and confirm the association with the dog record.
- Add the `utils.js` file and make it accessible to the entire application.
上述提示可确保正确上传犬只图像,并与正确的犬只记录关联,同时处理可能出现的错误。
您应该会收到函数已成功创建的回复。
这两个实用功能是在utils.js
文件中实现的,以保持代码的模块化。现在,您已经拥有了为应用程序实现签到功能所需的两个实用程序函数。
实施签到功能
要在应用程序中实现签到功能,您需要狗狗的姓名、年龄、品种、图像和主人的用户名。
然后,您需要为狗主人生成一个检索代码,记录访问情况,上传狗的图像(在后台),并将代码返回给狗主人。
您可以通过向人工智能代理输入下面的提示或类似内容,将上述逻辑添加到您的后台应用程序中:
- Create an asynchronous Cloud Code function named `checkIn` in a `main.js` file that accepts five parameters from a request body: `dogName` (String), `dogAge` (Number), `dogBreed` (String), `ownerUsername`(String), and `dogImage` (File).
- The function should first validate and sanitize each input to ensure they conform to expected types and formats.
- It should then search for an owner in the `owner` table using `ownerUsername` and create a new owner record if none exists. Next, the function should create a new record in the `dogs` table with the provided `dogName`, `dogBreed`, `dogAge`, and the corresponding `ownerId`.
- After that, it should call the `generateRetrievalCode` utility function to create a unique retrieval code.
- The function must also use the `uploadImage` utility to upload `dogImage`, running this process in the background without awaiting the return value.
- Finally, it should create a record in the `visits` table that includes relevant details such as the `ownerId`, `dogId`, and the generated retrieval code, and return a success message along with the retrieval code to the user.
- Ensure error handling is implemented for database operations and input validation, returning appropriate error messages where necessary.
您应该会收到函数已成功创建的回复。
您可以在应用程序控制面板→云代码上查看人工智能代理创建的代码,并在必要时进行修改,如下图所示。
现在,您可以为新狗办理入住狗舍手续了。接下来,您将使用记录活动功能。
实施记录活动功能
根据申请要求,您的申请应记录狗狗在探访期间参与的所有活动。
要实现记录活动功能,需要获取访问记录并创建包含所需信息的新活动。
您可以通过向人工智能代理输入下面的提示或类似内容,将上述逻辑添加到您的后台应用程序中:
- Create an asynchronous function called `recordActivity` that accepts three parameters from a request body: `visitId`, `type`, and `description`.
- It should first check if a visit with the specified `visitId` exists in the database.
- If the visit is not found, return an appropriate error message.
- If the visit exists, the function should create a new activity record with the visitId, type, and description, set the time to the current date,
and return a success message, after a successful creation.
- Ensure error handling is implemented for database operations and input validation, returning appropriate error messages where necessary.
您可以检查仪表板,确保该功能已正确执行,并在必要时进行更改。
实施结账功能
要实现结账功能,需要使用retrieval_code
获取访问记录,包括相关的狗和狗主人详细信息,更新check_out_time
,并返回包含狗和狗主人信息以及访问的入住和结账时间
的响应。
您可以通过向人工智能代理输入下面的提示或类似内容,将上述逻辑添加到您的后台应用程序中:
- Create an asynchronous cloud code function called `checkOut` that retrieves dog and visit details based on a `retrieval_code` extracted from the request body.
- The function should fetch the corresponding visit record in the `Visit` table and include related dog details from the `Dog` table and the `Owner` table.
- The function should select specific dog attributes such as `name`, `breed`, `age` and image.
- If no visit is found, return a message indicating that the `retrieval_code` is invalid.
- Upon successfully finding the visit, the function should update the `check_out_time` to the current date and save the updated visit record.
- Finally, structure a response that includes the dog's information
along with its owner details, as well as the visit's `check_in_time` and `check_out_time`.
- Implement error handling to catch and log any issues during the process,
returning a relevant error message in case of failure.
您可以在应用程序控制面板→云代码→main.js
上查看人工智能代理创建的代码。
随着结账功能的全面实施,您已经完成了一个简单的狗舍管理应用程序的应用要求。
结论
在本文中,您将构建一个可扩展的狗舍管理应用程序,它支持签到、跟踪狗在访问期间的活动,并允许主人在访问结束时使用人工智能找回他们的狗。
为了提高应用程序的扩展能力,您设计了一个规范化数据库,以确保查询速度不会太慢。
您还将狗图片上传等长期运行的任务委托给一个后台运行的函数,并将代码分割成多个文件,使其更易于维护。
后端完成后,您可以将其连接到使用React、Svelte 或Vue等前端库构建的用户界面,并在 Back4app Web 部署平台等平台上使用 Docker 进行部署。