今日已更新 111 条资讯 | 累计 32441 条内容
关于我们

Build One Guarded Prisma Endpoint, Then Break It Five Ways

hellowwworld 2026年08月17日 05:21 0 次阅读 来源:Dev.to

A generated route can remove repetitive Express handlers without removing the API contract. That distinction becomes concrete when one endpoint is deliberately broken in five small ways. Each break below changes either shape construction, request validation, emitted Prisma arguments, or execution-time projection. The status code alone is not enough to identify which layer moved. The examples use prisma-guard 1.33.0, Prisma 6.19.3, and Zod 4.4.3. Those versions are pinned because several observations concern exact runtime behavior. The goal is a test you can rerun during upgrades, not a rule inferred from one successful response. Start with a small tenant model. Nursery is the scope root, and Plant carries the foreign key that the guard extension can constrain. /// @scope-root model Nursery { id String @id @default(cuid()) name String plants Plant[] } model Plant { id String @id @default(cuid()) name String priceCents Int isPublished Boolean @default(false) nurseryId String nursery Nursery @relation(fields: [nurseryId], references: [id]) } The generated router still needs an extended Prisma client and trusted request context. Authentication remains application code. The important detail is that the tenant ID comes from the authenticated session, not from the query string or body. import { AsyncLocalStorage } from ' node:async_hooks ' import { PrismaClient } from ' @prisma/client ' import { guard } from ' ./generated/guard/client ' type RequestContext = { nurseryId : string ; audience : ' public ' | ' seller ' } const requestStore = new AsyncLocalStorage < RequestContext > () const prisma = new PrismaClient (). $extends ( guard . extension (() => { const context = requestStore . getStore () return { Nursery : context ?. nurseryId , caller : context ?. audience } }), ) Now define one public read contract. In a guard shape, true means the client may choose a value. A literal means the server chose it. force(true) is required to pin a Boolean to true because bare true is

本文内容来源于互联网,版权归原作者所有
查看原文