ZenStack是一個節點/typescript工具包,可簡化Web應用程序後端的開發。它通過靈活的授權層和自動生成的類型安全的API/鉤子增強了Prisma Orm,從而解開了其全堆棧開發的全部潛力。
我們的目標是讓您節省時間編寫樣板代碼,並專注於構建真實功能!
閱讀完整的文檔? Zenstack.dev。加入Discord以獲取反饋和問題。
Zenstack通過以下四層逐步擴展Prisma的力量:
Zenstack引入了一種名為“ Zmodel”的數據建模語言 - Prisma Schema語言的超集。它通過自定義屬性和功能擴展了Prisma模式,並基於此實現了Prisma周圍的靈活訪問控制層。
// base.zmodel
abstract model Base {
id String @ id
author User @ relation ( fields : [ authorId ] , references : [ id ] )
authorId String
// ? allow full CRUD by author
@@ allow ( 'all' , author == auth ( ) )
} // schema.zmodel
import "base"
model Post extends Base {
title String
published Boolean @ default ( false )
// ? allow logged-in users to read published posts
@@ allow ( 'read' , auth ( ) != null && published )
} zenstack CLI將ZMODEL轉移到標準的Prisma架構中,您可以將其與常規的Prisma工作流程一起使用。
在運行時,圍繞Prisma客戶端創建了透明代理,以攔截查詢和突變以執行訪問策略。
import { enhance } from '@zenstackhq/runtime' ;
// a regular Prisma client
const prisma = new PrismaClient ( ) ;
async function getPosts ( userId : string ) {
// create an enhanced Prisma client that has access control enabled
const enhanced = enhance ( prisma , { user : userId } ) ;
// only posts that're visible to the user will be returned
return enhanced . post . findMany ( ) ;
}服務器適配器軟件包可幫助您將啟用訪問控制的PRISMA客戶端包裝到可從前端安全調用的後端CRUD API。這是Next.js的一個示例:
// pages/api/model/[...path].ts
import { requestHandler } from '@zenstackhq/next' ;
import { enhance } from '@zenstackhq/runtime' ;
import { getSessionUser } from '@lib/auth' ;
import { prisma } from '@lib/db' ;
// Mount Prisma-style APIs: "/api/model/post/findMany", "/api/model/post/create", etc.
// Can be configured to provide standard RESTful APIs (using JSON:API) instead.
export default requestHandler ( {
getPrisma : ( req , res ) => enhance ( prisma , { user : getSessionUser ( req , res ) } ) ,
} ) ;插件可以生成與上述API交談的強型客戶庫。這是一個反應的示例:
// components/MyPosts.tsx
import { useFindManyPost } from '@lib/hooks' ;
const MyPosts = ( ) => {
// list all posts that're visible to the current user, together with their authors
const { data : posts } = useFindManyPost ( {
include : { author : true } ,
orderBy : { createdAt : 'desc' } ,
} ) ;
return (
< ul >
{ posts ?. map ( ( post ) => (
< li key = { post . id } >
{ post . title } by { post . author . name }
</ li >
) ) }
</ ul >
) ;
} ; 下圖給出了Zenstack的高級體系結構概述。
樣本回購包括以下模式:
您可以將此博客文章用作簡介。
查看多租戶應用程序的運行示例。您可以在下面找到不同的實現:
加入我們的Discord服務器進行聊天和更新!
如果您喜歡Zenstack,請加入我們,使其成為更好的工具!請使用貢獻指南以獲取有關如何入門的詳細信息,並毫不猶豫地加入Discord以分享您的想法。文檔位於單獨的存儲庫中:zenstack-docs。
還請考慮贊助我們的工作以加快發展的速度。您的貢獻將100%用作賞金獎勵,以鼓勵社區成員幫助修復錯誤,添加功能並改善文檔。
感謝您的慷慨支持!
大塊主義 | 美人魚圖表 | coderabbit | 約翰·羅恩(Johann Rohn) |
本傑明·澤西羅維奇 | Ulric | Fabian Jocks |
感謝所有幫助Zenstack變得更好的貢獻者!
麻省理工學院