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

[Feature Request] Compatibility with Edge Runtime #785

Closed ymc9 closed 6 months ago

ymc9 commented 11 months ago

Background

Prisma didn't support running in the edge environment (Vercel, CloudFlare, etc.) in older versions, so it wasn't a design goal for ZenStack either. However, things have been changing fast in the past few months:

Proposed Solution

The main obstacle to running ZenStack on the edge today is the environment doesn't allow dynamic require calls. ZenStack uses it to dynamically load generated code (for evaluating access policies) and determine Prisma version (for compatibility with both v4 and v5), and a few other less important occasions.

However, if we go ahead and make the enhance API generated as well, the dynamicity mentioned above can be determined at zenstack generate time instead of runtime. I believe it's doable but requires careful backward compatibility testing and verification in different edge environments.

Question

What are the main edge runtime providers that people care about besides Vercel and CloudFlare?

ErikDakoda commented 10 months ago

I want to note that although I requested this feature, I have since moved away from using Prisma and, therefore, ZenStack in my edge runtime routes altogether. Prisma gets bloated very quickly, with all models included, so it is really easy to exceed the maximum bundle size (Hobby: 1 MB, Pro: 2 MB, Ent: 4 MB).

ymc9 commented 10 months ago

I want to note that although I requested this feature, I have since moved away from using Prisma and, therefore, ZenStack in my edge runtime routes altogether. Prisma gets bloated very quickly, with all models included, so it is really easy to exceed the maximum bundle size (Hobby: 1 MB, Pro: 2 MB, Ent: 4 MB).

Got it. May I know what your deployment target is? So it's still bloated even using the Edge version of PrismaClient, right? That's surprising ...

ErikDakoda commented 10 months ago

Yes, @prisma/client/runtime/edge.js and .prisma/client/edge.js are the two largest dependencies, and without them the bundle size goes from 2.84 MB down to 613 KB. I should also be able to remove the third largest dependency, which is zod/lib/types.js.

ymc9 commented 10 months ago

Yes, @prisma/client/runtime/edge.js and .prisma/client/edge.js are the two largest dependencies, and without them the bundle size goes from 2.84 MB down to 613 KB. I should also be able to remove the third largest dependency, which is zod/lib/types.js.

OK, that's a surprise. Prisma team should look into it 🤷🏻. What's your alternative now? Drizzle?

ErikDakoda commented 10 months ago

I am saving the data from the client side and I am avoiding database access in the edge function. If I didn't have that option, I would try the new @prisma/adapter-planetscale or access the database through my REST or rRPC API.

ymc9 commented 6 months ago

Fixed in 2.0.0-beta.8 (for Vercel edge and Cloudflare worker)

crowd4gpt commented 5 months ago

was excited to hear this available for cloudflare

but disappointed D1 isn't supported (if it did zenstack would see massive adoption from our side)

what are my options here? put off by vercel as it is 10x more expensive than cloudflare. i like having database close to the users without having to setup read replicas :/

zenstack would be perfect if it could support D1 somehow

ymc9 commented 5 months ago

was excited to hear this available for cloudflare

but disappointed D1 isn't supported (if it did zenstack would see massive adoption from our side)

what are my options here? put off by vercel as it is 10x more expensive than cloudflare. i like having database close to the users without having to setup read replicas :/

zenstack would be perfect if it could support D1 somehow

Hi @crowd4gpt , the main obstacle for D1 support is its lack of interactive transactions. ZenStack uses ITX in certain cases to enforce access policies. I think it's possible to have partial support for D1 with some usage patterns of policy rules disallowed. A thorough review is needed to determine if it's viable.

Do you mind creating a separate FR for it?