zenstackhq / zenstack

Fullstack TypeScript toolkit that enhances Prisma ORM with flexible Authorization layer for RBAC/ABAC/PBAC/ReBAC, offering auto-generated type-safe APIs and frontend hooks.
https://zenstack.dev
MIT License
2.07k stars 88 forks source link

Error when generating with React Plugin #279

Closed jonathangerbaud closed 1 year ago

jonathangerbaud commented 1 year ago

Hello, I got this error when I'm using the React lplugin:

npx zenstack generate
⌛️ ZenStack CLI v1.0.0-alpha.79, running plugins
⠋ Running plugin PrismaEnvironment variables loaded from .env
Prisma schema loaded from prisma\schema.prisma

✔ Generated Prisma Client (4.11.0 | library) to .\node_modules\@prisma\client in 281ms
You can now start using Prisma Client in your code. Reference: https://pris.ly/d/client

import { PrismaClient } from '@prisma/client'
const prisma = new PrismaClient()

✔ Running plugin Prisma
✔ Running plugin Model Metadata
✔ Running plugin Access Policy
✖ Running plugin React

An unexpected error occurred:
 TypeError: Cannot read properties of undefined (reading 'updateOne')
    at generateModelHooks (D:\Coding\Web\DigitalNomading\node_modules\@zenstackhq\react\react-hooks-generator.js:232:87)
    at D:\Coding\Web\DigitalNomading\node_modules\@zenstackhq\react\react-hooks-generator.js:57:13
    at Array.forEach (<anonymous>)
    at D:\Coding\Web\DigitalNomading\node_modules\@zenstackhq\react\react-hooks-generator.js:55:16
    at Generator.next (<anonymous>)
    at D:\Coding\Web\DigitalNomading\node_modules\@zenstackhq\react\react-hooks-generator.js:31:71
    at new Promise (<anonymous>)
    at __awaiter (D:\Coding\Web\DigitalNomading\node_modules\@zenstackhq\react\react-hooks-generator.js:27:12)
    at generate (D:\Coding\Web\DigitalNomading\node_modules\@zenstackhq\react\react-hooks-generator.js:42:12)
    at D:\Coding\Web\DigitalNomading\node_modules\@zenstackhq\react\index.js:17:53

I just checked the code and I think you just need to add a "?" in https://github.com/zenstackhq/zenstack/blob/main/packages/plugins/react/src/react-hooks-generator.ts

Change if (mapping?.update || (mapping as any).updateOne) { To if (mapping?.update || (mapping as any)?.updateOne) {

(Sorry, too lazy do to a PR...)

ymc9 commented 1 year ago

Thanks again for filing this! I've made a fix to consistently deal with models marked @@ignore. Please update "zenstack" and "@zenstackhq/*" packages to "@latest" (version 1.0.0-alpha.81) and it should work now.

283

jonathangerbaud commented 1 year ago

Okay, it's working now with the last version. Thanks!