react-native-async-storage / async-storage

An asynchronous, persistent, key-value storage system for React Native.
https://react-native-async-storage.github.io/async-storage/
MIT License
4.61k stars 459 forks source link

feat(core): Introduce new API #1010

Closed krizzu closed 8 months ago

krizzu commented 9 months ago

Summary

This is an introduction to AsyncStorage v2. This is by no means the final shape, just something to spark a discussion.

Glossary

Main changes

API separation

To expand AsyncStorage into different use cases, the storage has been separated from the core API. This means, that while the main API stays the same, the underlying storage engine will provide different capabilities, depending only on the implementation. From a user perspective, one API can be used to use different kinds of storage, such as file-on-disk, in-memory, SQLite, Encrypted, etc.

Getting AsyncStorage

Because of the separation, the main instance, AsyncStorage needs to be connected to StorageBackend. This happens via call to AsyncStorageFactory.create method, where user needs to provide an instance of StorageBackend.

krizzu commented 9 months ago

After giving it some more thought, I became more sceptic about this approach. There is a shortcomings that I think blocks AsyncStorage from moving into the "next" stage.

AsyncStorage is working on serialised data, only. That means, it accepts and returns strings and leave serialization to the user. In this PR, you can see added "Adapter" acting as a middleware responsible for just that , an attempt to improve the developer experience. At that point, I think it's a workaround, rather than a solution, because we force this limitation on StorageBackends (force them to work on strings), rather giving them a freedom to choose.

With TurboModules, we have access to bridging functionality, which could omit serialization (to some extent) on JS thread, improving performance. Good example of this is MMKV storage.

I think it's worth re-thinking a reason to have StorageBackend all together. I think exposing an AsyncStorage interface and let storages implement it in their way (with flexible Storage Model) would be better approach. WDYT?

krizzu commented 8 months ago

See #1016