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.16k stars 88 forks source link

[Feature Request] Allow specifying a whitelist of models in the configuration so that zod and trpc can be skipped on others #728

Open levipro opened 1 year ago

levipro commented 1 year ago

Is your feature request related to a problem? Please describe. When using zenstack on a large pre-existing database I have run in to an issue where I cannot run the server because of the number of files that get created by zod when using the trpc integration.

I have used the /// @@Gen.model(hide: true) comment to disable the trpc generation for certain models but zod still runs on all of them.

In my case this generated > 65 MB of files and the server could not start.

Describe the solution you'd like Allow specifying a whitelist of models that zod+trpc generation should happen on

Describe alternatives you've considered NA

Additional context NA

ymc9 commented 1 year ago

Hi @levipro , I've implemented it and would like to see how well it'll solve your problem. The tricky part is, given a whitelist of models to generate Zod schema for, we need to build a transitive closure and include all model types that have a relation with the whitelisted ones. The reason is Prisma allows relation filtering in queries and allows you to traverse related models in query conditions.

This can be problematic when, say, you have a "hub" model like User, which transitively reaches all other models. Do you mind sharing some basic structure of your zmodel so I can check it? Thank you!

levipro commented 1 year ago

When I do run prismas introspection on my database it creates quite a large schema file. I certainly have a few hub models throughout as well.

I can see how this is a complex issue to solve for. Is there a way to automatically exclude a relation if the model for that relation is not in the whitelist as well?

Do you have a branch I can try running this from to help test things? That would be the easiest way for me to try and help. Cleaning up my schema file and removing sensitive structures would take a lot of time.

ymc9 commented 1 year ago

When I do run prismas introspection on my database it creates quite a large schema file. I certainly have a few hub models throughout as well.

I can see how this is a complex issue to solve for. Is there a way to automatically exclude a relation if the model for that relation is not in the whitelist as well?

Do you have a branch I can try running this from to help test things? That would be the easiest way for me to try and help. Cleaning up my schema file and removing sensitive structures would take a lot of time.

That's a good idea. You can install the v1.1 package from the preview registry:

npm i -D --registry https://preview.registry.zenstack.dev zenstack @zenstackhq/trpc

The updated trpc plugin has a "generateModels" option, you can pass a comma-separated list of model names to whitelist. It'll pass it down to the zod plugin that it implicitly activates. If you've also manually declared a zod plugin in your zmodel, you need to put the same option in it.

plugin trpc {
    ...
    generateModels = 'User,Post'
}
levipro commented 1 year ago

Adding the above trpc setting did work to only generate the User/Post routers, but all the zod files were still generated. I also tried adding this but the same files were generated:

plugin zod {
  provider = '@core/zod'
  generateModels = 'User,Post'
}

I also had to add the node options -stack-size=2048 --max-old-space-size=12288 for this to complete

ymc9 commented 1 year ago

Got it. I believe it's due to the transitive closure, as we discussed previously ... I think it's possible to exclude relation - a bit more work to do. I'm trying to wrap up v1.1 release and probably have to postpone it.

I'll keep thinking about this and hopefully get a complete resolution in the next one.

ymc9 commented 8 months ago

Probably a real solution is to infer zod schemas instead of generating them.