Reputation: 33725
I'm new to react native and I need a list of local database tools that will meet the minimum criteria in the description to follow.
If this were a PHP/MySQL application, I build 3 database tables:
t_food (500 rows)
- food_id (primary_key)
- food_name (string)
t_meal (100 000 rows)
- meal_id (primary_key)
- meal_name (string)
t_meal_item (1 000 000 rows)
- meal_id (foreign_key)
- food_id (foreign_key)
These three tables combined in MySQL takes up about 10MB to 15MB. Then I would use PHP to create an interface that will present the results of this query:
SELECT meal_id FROM t_meal_item WHERE food_id = @food1 AND meal_id IN (SELECT meal_id FROM t_meal_item WHERE food_id = @food2 AND meal_id IN (SELECT meal_id FROM t_meal_item WHERE food_id = @food3))
In a MySQL/PHP application, the results of the SQL query will return in less than 2 seconds.
In the react-native world, I need a local database that achieves similar results to above but must also comply with the following requirements:
must support 15 MB of persistent storage (so that you can use it without internet access)
querying for a meal filtered by 3 food items must complete in less than 3 seconds on modern mainstream mobile devices. Eg. the equivalent of the SQL query above must complete in less than 3 seconds on an iphone 6 and Huawei Nova Plus.
I have already tried the following:
A. Realm is failing on criteria 2 as indicated in this question here:
Improve the speed of a realm query in react-native?
A solution to the Realm question will be an acceptable answer to this question.
B. Six years ago, I tried this with core data on an iOS device. The iOS device kept crashing due to insufficient memory.
C. I'm trying SQLite Storage but already running into this problem here:
basic React Native SQLite script returning undefined which causes error
What tools can solve this problem?
Upvotes: 18
Views: 28183
Reputation: 259
As per my experience, it totally depends upon the application requirements. If your application is not large scale you can use React Native's default Async Storage, if you are using redux, you can use redux-persist and provide it async storage plugin so it will persist your reducers automatically.
In other case, if you have a large scale application and you are much concerned about the database, you should use WatermelonDB, as it is the best available database for React Native. It also provide synchronising functionality through which you can create an online/offline app with database automatically being synced to the backend. WatermelonDB is also using SQlite on its backend to store data but its more powerful and useful
Upvotes: 0
Reputation: 585
As of now, WatermelonDB seems to fulfil both size limit and performance requirements.
Realm is now commercial and tightly coupled to MongoDB and their cloud services (or at least it seems like that from the documentation).
I've also had a quick look at PouchDB, but one of it's react-native integrations stockulus/pouchdb-react-native is based on AsyncStorage (which from my experience is terribly slow), and other one craftzdog/pouchdb-react-native is based on sqlite, but has no activity in last few years.
Upvotes: 1
Reputation: 334
I have not used those other databases that people listed, but I would recommend Realm. Very useful, fast, fully integrated with react-native and very well documented.
Upvotes: 6
Reputation: 3136
consider react native as front end library thats need a restful api to connect so you can make backend with php and react native will receive data from it
if you need local database you can use react-native-local-mongodb
or react-native-sqlite-storage
also if you use redux you can also use redux-persist
Upvotes: 10
Reputation: 33
You can use SQLite, it has a native plugin for React Native : https://github.com/andpor/react-native-sqlite-storage
Upvotes: 1