解锁后端简单性:使用 Convex 构建可扩展应用程序
**构建可扩展、高效的应用程序可能很有挑战性,对吧?特别是当你时间不多或参加黑客马拉松时。如果我告诉你有一个后端解决方案可以简化这个过程,你会怎么想?**
最近我正在做一个项目,第一次使用了 Convex 后端,你猜怎么着,感觉棒极了。
Convex 不仅仅是一个数据库,它还是为现代开发人员量身定制的全面后端解决方案。它提供从 TypeScript 中的云函数到实时数据同步的所有内容,让您可以完全专注于前端代码。这促使它越来越受欢迎。
是什么让它与众不同🤔🤔
这些是我个人使用的功能,还有更多功能,例如**ACID 事务**、**TypeScript 支持**、**安全和访问控制**、**自动缓存和优化**,您绝对可以尝试一下。
现在让我们通过一个简单的“getGroupMembers”函数看看在普通后端和凸后端中的方法。
让我们使用 MongoDB 和 Node.js 构建一个后端函数
const identity = await verifyToken(req.headers.authorization);
if (!identity) {
res.status(401).send("Unauthorized");
return;
}const conversation = await db.collection("conversations").findOne({ _id: conversationId });
if (!conversation) {
res.status(404).send("Conversation not found");
return;
}const users = await db.collection("users").find().toArray();
const groupMembers = users.filter(user => conversation.participants.includes(user._id));res.status(200).send(groupMembers);
**以下是上述代码片段的表示图**

让我们使用 Convex 构建一个后端函数
const identity = await ctx.auth.getUserIdentity();
if (!identity) {
throw new ConvexError("Unauthorized");
}const conversation = await ctx.db.query("conversations")
.filter((q) => q.eq(q.field("_id"), args.conversationId))
.first();
if (!conversation) {
throw new ConvexError("Conversation not found");
}const users = await ctx.db.query("users").collect();
const groupMembers = users.filter((user) => conversation.participants.includes(user._id));return groupMembers;
**下面是凸处理后端的总体简化解释图 -**

我如何在我的项目中使用 Convex
我刚刚使用 Next.js、TypeScript 以及最重要的 Convex 后端重新创建了 freeCodeCamp MERN 堆栈书店项目。
因此,如果您想了解如何使用 Convex 后端,那么您可以关注我的 github 项目,我已将我的技术堆栈从 MERN 堆栈转移到 NEXT.js + TS + Convex。