React Node.js 和 Prisma 构建全栈框架

发布于:2023-12-07 ⋅ 阅读:(73) ⋅ 点赞:(0)

特点

  • 快速入门:由于其表现力强,您可以使用很少的简洁、一致的声明性代码从头开始创建和部署生产就绪的 Web 应用程序。
  • 无样板代码:通过抽象出复杂的全栈功能,可以减少样板代码。这意味着需要维护和理解的代码更少!这也意味着更容易升级。
  • 无锁定:您可以将此框架上的应用程序部署在任何您喜欢的地方。不存在特定提供商的锁定,您可以完全控制代码。
  • 全栈身份验证
  • RPC(客户端<->服务器)
  • 简单部署
  • 作业
  • 电子邮件发送
  • 全栈类型安全

代码片段

app todoApp {
  title: "ToDo App",  // visible in the browser tab
  auth: { // full-stack auth out-of-the-box
    userEntity: User, methods: { email: {...} }
  }
}

route RootRoute { path: "/", to: MainPage }
page MainPage {
  authRequired: true, // Limit access to logged-in users.
  component: import Main from "@client/Main.tsx" // Your React code.
}

query getTasks {
  fn: import { getTasks } from "@server/tasks.js", // Your Node.js code.
  entities: [Task] // Automatic cache invalidation.
}

entity Task {=psl  // Your Prisma data model.
    id          Int     @id @default(autoincrement())
    description String
    isDone      Boolean @default(false)
psl=}

在配置中定义您的应用程序并获取:

  • 使用 Auth UI 组件登录和注册,
  • 全栈式安全
  • 电子邮件发送
  • 异步处理作业
  • React Query 支持数据获取
  • 安全最佳实践

假设您想要构建一个 Web 应用程序,允许用户创建和分享他们最喜欢的食谱。

让我们从此件开始:它是应用程序的中心文件,您可以在其中从高层描述应用程序。

让我们给我们的应用程序一个标题,然后立即通过用户名和密码打开全栈身份验证:

app RecipeApp {
  title: "My Recipes",
  auth: {
    methods: { usernameAndPassword: {} },
    onAuthFailedRedirectTo: "/login",
    userEntity: User
  }
}

然后让我们为您的食谱添加数据模型。我们希望用户和用户可以拥有食谱:

...

entity User {=psl  // Data models are defined using Prisma Schema Language.
  id          Int @id @default(autoincrement())
  username    String @unique
  password    String
  recipes     Recipe[]
psl=}

entity Recipe {=psl
  id          Int @id @default(autoincrement())
  title       String
  description String?
  userId      Int
  user        User @relation(fields: [userId], references: [id])
psl=}

框架链接

参阅 - 亚图跨际
本文含有隐藏内容,请 开通VIP 后查看

网站公告

今日签到

点亮在社区的每一天
去签到