prisma / react-native-prisma

Apache License 2.0
122 stars 5 forks source link

Hooks extension improvement proposal #2

Open MaxBolotnyi opened 2 months ago

MaxBolotnyi commented 2 months ago

Hi, First of all, thank you for all the effort with that project. The idea of bringing prisma to react-native and expo is breathtaking and promising.

The current implementation of the react hooks extension makes a query to a database on a per-hook basis, which could lead to a redundant query waterfall in a scenario where the same hook/query is utilized in multiple components at the same time. Would you consider implementing the shared cache for the same queries 'batching' the same calls into a single one sharing a common RAM cache over all the hooks that read that same query?
The TanStack Query uses a similar approach for batching and sharing the cache over its query hooks.

Also, would you be open to accepting the PR for that implementation to help you?

sorenbs commented 2 months ago

Hey Max - thank you for your interest!

What you describe is exactly what we have planned. Our goal for the first release is to get an API that we feel good about in the hands of developers, such that we can gather feedback and lock-down a final API. For example, it is convention that hooks be free functions ala useQuery() rather than properties on an object ala prisma.user.findFirst() - is that a real concern that we should address by changing the API, or is the commonality with the normal Prisma API enough of a benefit to break this convention? That's the kinds of questions we seek to get answered by the end of Q2.

Following that, we want to focus on optimizations. a shared cache as you propose is an important optimization. Another one is reducing the size of the React Native library. It's currently 25 mb, which is fine for gathering initial feedback, but not really acceptable for broader use in production apps.

We generally welcome contributions, and would love to see a pr for the shared cache implementation. We would need extensive test coverage before merging it, and we can collaborate on that aspect if you like. The current code base is not well tested, so we'll need to improve here before we introduce additional complexity.

MaxBolotnyi commented 1 month ago

Hi Soren, thank for the valuable reply!

I would be glad to collaborate and roll out the PR fot the proposed cache implementation. Could you please point me out to any documentation regarding the flow that would fit you best, or would it be best just to roll out the draft version of it and start from there?

P.S. Regarding the question you raised in the first paragraph, I would personally put my vote on the object property implementation and keep it aligned with the usual Prisma API, but that's just my humble opinion, though

sorenbs commented 1 month ago

P.S. Regarding the question you raised in the first paragraph, I would personally put my vote on the object property implementation and keep it aligned with the usual Prisma API, but that's just my humble opinion, though

Thank you for that feedback!

If you simply fork the repo and prepare a pull request, then we can take it from there. Let me know if you need any help. You are welcome to ping me on Twitter if I am slow to reply here :-)

Also, I just announced this at app.js in Krakow. You can find the blogpost here: https://www.prisma.io/blog/bringing-prisma-orm-to-react-native-and-expo