Modern data layer for TypeScript apps - type-safe ORM, built-in access control, automatic query services
Find a file
2023-05-12 10:48:56 -07:00
.changeset merge canary to dev (#181) 2023-01-27 22:31:44 +08:00
.github fix: deprecated cuid dependency & clean up CI file (#359) 2023-04-18 22:01:20 -07:00
.vscode feat: Add multi-schema file support (#368) 2023-04-27 21:19:34 +01:00
packages feat: restful style openapi spec generation (#410) 2023-05-12 10:48:56 -07:00
tests/integration chore: remove dep to chevrotain and upgrade langium to 1.2.0 (#406) 2023-05-08 12:01:35 -07:00
.gitignore fix: issue with dealing with result read-back for nextjs and react hooks 2023-03-02 20:17:48 +08:00
.npmrc dev2main (#30) 2022-10-28 20:22:29 +08:00
.prettierignore merge canary to dev (#181) 2023-01-27 22:31:44 +08:00
.prettierrc merge canary to dev (#181) 2023-01-27 22:31:44 +08:00
CHANGELOG.md chore: bump version and fix test (#144) 2022-12-16 16:32:03 +08:00
CODE_OF_CONDUCT.md merge canary to dev (#181) 2023-01-27 22:31:44 +08:00
LICENSE dev2main: release v0.2.1 (#34) 2022-10-29 09:23:46 +08:00
package.json chore: remove dep to chevrotain and upgrade langium to 1.2.0 (#406) 2023-05-08 12:01:35 -07:00
pnpm-lock.yaml feat: restful style openapi spec generation (#410) 2023-05-12 10:48:56 -07:00
pnpm-workspace.yaml merge canary to dev (#181) 2023-01-27 22:31:44 +08:00
README.md chore: update README links (#383) 2023-05-03 21:11:34 -07:00
SECURITY.md merge canary to dev (#181) 2023-01-27 22:31:44 +08:00

What it is

ZenStack is a toolkit that simplifies the development of a web app's backend. It supercharges Prisma ORM with a powerful access control layer and unleashes its full potential for web development.

Our goal is to let you save time writing boilerplate code and focus on building real features!

How it works

ZenStack extended Prisma schema language for supporting custom attributes and functions and, based on that, implemented a flexible access control layer around Prisma.

// schema.zmodel

model Post {
    id String @id
    title String
    published Boolean @default(false)
    author User @relation(fields: [authorId], references: [id])
    authorId String

    // 🔐 allow logged-in users to read published posts
    @@allow('read', auth() != null && published)

    // 🔐 allow full CRUD by author
    @@allow('all', author == auth())
}

At runtime, transparent proxies are created around Prisma clients for intercepting queries and mutations to enforce access policies. Moreover, framework integration packages help you wrap an access-control-enabled Prisma client into backend APIs that can be safely called from the frontend.

// Next.js example: pages/api/model/[...path].ts

import { requestHandler } from '@zenstackhq/next';
import { withPolicy } from '@zenstackhq/runtime';
import { getSessionUser } from '@lib/auth';
import { prisma } from '@lib/db';

export default requestHandler({
    getPrisma: (req, res) => withPolicy(prisma, { user: getSessionUser(req, res) }),
});

Plugins can generate strong-typed client libraries that talk to the APIs:

// React example: components/MyPosts.tsx

import { usePost } from '@lib/hooks';

const MyPosts = () => {
    // Post CRUD hooks
    const { findMany } = usePost();

    // list all posts that're visible to the current user, together with their authors
    const { data: posts } = findMany({
        include: { author: true },
        orderBy: { createdAt: 'desc' },
    });

    return (
        <ul>
            {posts?.map((post) => (
                <li key={post.id}>
                    {post.title} by {post.author.name}
                </li>
            ))}
        </ul>
    );
};

The following diagram gives a high-level overview of how it works.

Architecture

Features

  • Access control and data validation rules right inside your Prisma schema
  • Auto-generated OpenAPI (RESTful) specifications, services, and client libraries
  • End-to-end type safety
  • Extensible: custom attributes, functions, and a plugin system
  • A framework-agnostic core with framework-specific adapters
  • Uncompromised performance

Plugins

  • Prisma schema generator
  • Zod schema generator
  • React hooks generator
  • OpenAPI specification generator
  • tRPC router generator
  • 🙋🏻 Request for a plugin

Framework adapters

Prisma schema extensions

Examples

Check out the Collaborative Todo App for a running example. You can find the source code below:

Community

Join our discord server for chat and updates!

License

MIT