fireproof-storage / fireproof

Realtime database, runs anywhere. Install Fireproof in your front-end app or edge function, and sync data via any backend.
https://fireproof.storage
Other
219 stars 16 forks source link

React Native - attempt #2 #107

Open boorad opened 2 months ago

boorad commented 2 months ago

This PR adds the required bits to have a fully-functional Fireproof database in a React Native mobile application. The biggest difference is polyfilling certain global objects that are expected to be present in Node or a browser with alternatives that work in React Native.

We also add an example RN app showcasing Fireproof as the live database.


Note: Until react-native-quick-crypto gets support for subtle.encrypt() and subtle.decrypt(), you need to use {public: true} database option:

const { database, useDocument } = useFireproof('TodoDB', {public: true});
boorad commented 1 month ago

Simulator Screenshot - iPhone 15 Pro - 2024-05-13 at 19 06 32

jchris commented 1 month ago

The (there's never any changes in db.subscribe()) is something that happens to a lot of people. I think I should update the API so that it doesn't pass any data, ever. Then people would know it is just an event they can use to know when to query the database. That's how I use it 90% of the time. Hence, it has an optional second argument when you pass true that returns the data items. The reason why is in conditions of post merge (remote events), fetching the data items can be somewhat expensive, which makes no sense when they are discarded 90% of the time. Curious for your thoughts on the API.

It would be interesting to see what issues show up when trying useLiveQuery as that should be the main API in react. It's probably worth bridging the gap unless there is something weird there.

boorad commented 1 month ago

db.subscribe()

Aha, so all your docs have to do is compensate for me being an idiot and not reading / understanding. I do think you should add a bit about that boolean flag in the docs, like:

Note: Use returnChangeData: boolean argument to return the changed data

The latest commit has this working and refreshing the Todo List after any change, but it is kind of slow. Like a second or two of logging, and then the mobile UI updates. That could be me not exactly getting the call semantics correct.

boorad commented 1 month ago

useLiveQuery()

Can you talk to how the index is stored (prolly trees)? From the docs:

By default, Fireproof stores data in the browser's local storage

RN doesn't have local storage, so wondering how I can override and store in MMKV or similar.

I see the proper number of documents coming from useLiveQuery but they're all undefined, and have that `{_key: ..., _value: ...} format.