Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions packages/misc/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2022 ZenStack

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
1 change: 1 addition & 0 deletions packages/misc/redwood/LICENSE
79 changes: 79 additions & 0 deletions packages/misc/redwood/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
# ZenStack RedwoodJS Integration

This package provides the CLI and runtime APIs for integrating [ZenStack](https://zenstack.dev) into a [RedwoodJS](https://redwoodjs.com/) 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

Visit [homepage](https://zenstack.dev) for more details.

## Setting up

This package leverages RedwoodJS's experimental feature to register a custom CLI command to `yarn redwood`. To enable it, add the following to `redwood.toml`:

```toml
[experimental.cli]
autoInstall = true
[[experimental.cli.plugins]]
package = "@zenstackhq/redwood"
```

Then you can run `yarn rw @zenstackhq setup` to install ZenStack into your Redwood project. The setup command will:

1. Install ZenStack dependencies.
2. Copy your Prisma schema file "api/db/schema.prisma" to "api/db/schema.zmodel".
3. Add a "zenstack" section into "api/package.json" to specify the location of both the "schema.prisma" and "schema.zmodel" files.
4. Install a GraphQLYoga plugin in "api/src/functions/graphql.[ts|js]".
5. 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 regular Prisma schema file will be regenerated from the ZModel file when you run

```bash
yarn rw @zenstackhq generate
```

[The Complete Guide](https://zenstack.dev/docs/the-complete-guide/part1/) of ZenStack is the best way to learn how to author ZModel schemas. You can also use the

```bash
yarn rw @zenstackhq sample
```

command to browse a list of sample schemas and create from them.

## 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:

```bash
yarn rw @zenstackhq generate && yarn rw deploy vercel
```

## Sample application

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

## Getting help

The best way to getting help and updates about ZenStack is by joining our [Discord server](https://discord.gg/Ykhr738dUe).
54 changes: 54 additions & 0 deletions packages/misc/redwood/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
{
"name": "@zenstackhq/redwood",
"displayName": "ZenStack RedwoodJS Integration",
"version": "1.6.0",
"description": "CLI and runtime for integrating ZenStack with RedwoodJS projects.",
"repository": {
"type": "git",
"url": "https://github.com/zenstackhq/zenstack"
},
"scripts": {
"clean": "rimraf dist",
"build": "pnpm lint --max-warnings=0 && pnpm clean && tsc",
"watch": "tsc --watch",
"lint": "eslint src --ext ts",
"prepublishOnly": "pnpm build"
},
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
"exports": {
".": {
"default": "./dist/index.js",
"types": "./dist/index.d.ts"
},
"./graphql": {
"default": "./dist/graphql.js",
"types": "./dist/graphql.d.ts"
},
"./package.json": {
"default": "./package.json"
}
},
"author": {
"name": "ZenStack Team"
},
"homepage": "https://zenstack.dev",
"license": "MIT",
"dependencies": {
"@zenstackhq/runtime": "workspace:*",
"colors": "1.4.0",
"ts-morph": "^16.0.0"
},
"peerDependencies": {
"execa": "^5.0.0",
"listr2": "^6.0.0",
"terminal-link": "^2.0.0",
"yargs": "^17.7.2"
},
"devDependencies": {
"@redwoodjs/graphql-server": "^6.6.0",
"@redwoodjs/cli-helpers": "^6.6.0",
"@types/yargs": "^17.0.32",
"graphql-yoga": "^5.0.2"
}
}
51 changes: 51 additions & 0 deletions packages/misc/redwood/src/cli-passthrough.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { getPaths } from '@redwoodjs/cli-helpers';
import colors from 'colors';
import execa from 'execa';
import { CommandModule } from 'yargs';

// eslint-disable-next-line @typescript-eslint/no-explicit-any
async function runCommand(command: string, options: any) {
const args = ['zenstack', command];
for (const [name, value] of Object.entries(options)) {
args.push(name.length > 1 ? `--${name}` : `-${name}`);
if (typeof value === 'string') {
// Make sure options that take multiple quoted words
// are passed to zenstack with quotes.
value.split(' ').length > 1 ? args.push(`"${value}"`) : args.push(value);
}
}

console.log();
console.log(colors.green('Running ZenStack CLI...'));
console.log(colors.underline('$ npx ' + args.join(' ')));
console.log();

try {
await execa('npx', args, { cwd: getPaths().api.base, shell: true, stdio: 'inherit', cleanup: true });
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} catch (e: any) {
process.exit(e?.exitCode || 1);
}
}

/**
* Creates a yargs command that passes all options to the ZenStack CLI command.
*/
export function makePassthroughCommand(command: string): CommandModule<unknown> {
return {
command,
describe: `Run \`zenstack ${command} ...\``,
builder: (yargs) => {
return yargs
.strictOptions(false)
.strictCommands(false)
.strict(false)
.parserConfiguration({ 'camel-case-expansion': false, 'boolean-negation': false })
.help(false)
.version(false);
},
handler: async ({ _, $0: _$0, ...options }) => {
await runCommand(command, options);
},
};
}
Loading