2.1.1 • Published 2 days ago

@zenstackhq/redwood v2.1.1

Weekly downloads
-
License
MIT
Repository
github
Last release
2 days ago

ZenStack RedwoodJS Integration

This package provides the CLI and runtime APIs for integrating ZenStack into a RedwoodJS project. You can use ZenStack as a drop-in replacement to Prisma and define flexible access control policies declaratively inside the database schema. It's especially useful for building multi-tenant applications which tend to have complex authorization requirements beyond RBAC.

ZenStack is a full-stack toolkit built above Prisma ORM. It extends Prisma at the schema and the runtime level for adding the following capabilities:

  • Flexible access control
  • Data validation rules
  • Multi-file schemas
  • Custom attributes and functions in schemas

You can find a more detailed integration guide here.

Setting up

Run the following package setup command:

yarn rw setup package @zenstackhq/redwood

The setup command will:

  1. Update "redwood.toml" to allow ZenStack CLI plugin.
  2. Install ZenStack dependencies.
  3. Copy your Prisma schema file "api/db/schema.prisma" to "api/db/schema.zmodel".
  4. Add a "zenstack" section into "api/package.json" to specify the location 1f both the "schema.prisma" and "schema.zmodel" files.
  5. Install a GraphQLYoga plugin in "api/src/functions/graphql.ts|js".
  6. Eject service templates and modify the templates to use context.db (ZenStack-enhanced PrismaClient) instead of db for data access.

Modeling data and access policies

ZenStack's ZModel language is a superset of Prisma schema language. You should use it to define both the data schema and access policies. The Complete Guide of ZenStack is the best way to learn how to author ZModel schemas.

You should run the following command after updating "schema.zmodel":

yarn rw @zenstackhq generate

The command does the following things:

  1. Regenerate "schema.prisma"
  2. Run prisma generate to regenerate PrismaClient
  3. Generates supporting JS modules for enforcing access policies at runtime

Development workflow

The workflow of using ZenStack is very similar to using Prisma in RedwoodJS projects. The two main differences are:

  1. Generation

    You should run yarn rw @zenstackhq generate in place of yarn rw prisma generate. The ZenStack's generate command internally regenerates the Prisma schema from the ZModel schema, runs prisma generate automatically, and also generates other modules for supporting access policy enforcement at the runtime.

  2. Database access in services

    In your service code, you should use context.db instead of db for accessing the database. The context.db is an enhanced Prisma client that enforces access policies.

    The "setup" command prepared a customized service code template. When you run yarn rw g service, the generated code will already use context.db.

Other Prisma-related workflows like generation migration or pushing schema to the database stay unchanged.

Deployment

You should run the "generate" command in your deployment script before yarn rw deploy. For example, to deploy to Vercel, the command can be:

yarn rw @zenstackhq generate && yarn rw deploy vercel

Using the @zenstackhq CLI plugin

The @zenstackhq/redwood package registers a set of custom commands to the RedwoodJS CLI under the @zenstackhq namespace. You can run it with:

yarn rw @zenstackhq <cmd> [options]

The plugin is a simple wrapper of the standard zenstack CLI, similar to how RedwoodJS wraps the standard prisma CLI. It's equivalent to running npx zenstack ... inside the "api" directory.

See the CLI references for the full list of commands.

Sample application

You can find a complete multi-tenant Todo application built with RedwoodJS and ZenStack at: https://github.com/zenstackhq/sample-todo-redwood.

Getting help

The best way to get help and updates about ZenStack is by joining our Discord server.

2.1.1

2 days ago

2.1.0

4 days ago

2.0.3

12 days ago

2.0.2

16 days ago

2.0.1

21 days ago

1.12.5

22 days ago

2.0.0

23 days ago

1.12.4

25 days ago

2.0.0-beta.14

1 month ago

2.0.0-beta.13

1 month ago

2.0.0-beta.11

1 month ago

2.0.0-beta.12

1 month ago

1.12.3

1 month ago

2.0.0-beta.9

1 month ago

1.12.2

1 month ago

2.0.0-beta.10

1 month ago

2.0.0-beta.8

1 month ago

1.12.1

1 month ago

2.0.0-beta.7

1 month ago

2.0.0-beta.6

1 month ago

1.12.0

2 months ago

2.0.0-beta.5

2 months ago

2.0.0-beta.4

2 months ago

2.0.0-beta.3

2 months ago

1.11.1

2 months ago

2.0.0-beta.2

2 months ago

2.0.0-beta.1

2 months ago

1.11.0

2 months ago

2.0.0-alpha.7

2 months ago

2.0.0-alpha.6

2 months ago

1.10.3

2 months ago

1.10.2

2 months ago

1.10.1

2 months ago

2.0.0-alpha.3

2 months ago

2.0.0-alpha.4

2 months ago

2.0.0-alpha.5

2 months ago

2.0.0-alpha.2

2 months ago

1.10.0

3 months ago

1.9.0

3 months ago

2.0.0-alpha.1

3 months ago

1.8.2

3 months ago

1.8.1

4 months ago

1.8.0

4 months ago

1.7.1

4 months ago

1.7.0

4 months ago

1.6.2

4 months ago

1.6.1

4 months ago

1.6.0

5 months ago

0.5.0

5 months ago