React Native is the best available framework for building a cross-platform mobile app for iOS and Android. The demand for React Native development is increasing, and many organizations and developers rely on the framework and its ecosystem to ship mobile apps.
One common challenge during React Native development is choosing a database for your app. There are a variety of database options available. In this post, we'll find out what options are available and whether they might fit your requirements, such as supporting offline development capabilities or scaling easily with your user base.
A list of database options that are included in this post:
- Firebase and Cloud Firestore
- Realm Database
Firebase and Cloud Firestore
Firebase is a Backend as a Service (BaaS) that helps to write well-designed APIs in an opinionated way by keeping the tech stack minimal. Applications are often complex in terms of architecture. Usually, they are implemented as full-stack, or the combination of a frontend and a backend application with a database.
Using Firebase allows you to develop your mobile app without spending too much time to set up a custom server that can communicate securely with a database. It offers a NoSQL database that is commonly known as Firestore.
An important aspect of using Firebase is that the data is synchronized with overall clients in real-time. Firebase offers you methods that you can implement to listen to real-time subscriptions.
It's also easy to get started when using Firebase. It takes a minimal amount of effort, and Firebase provides a nice dashboard to control the aspects of the database and set your own security rules. Since the Firestore database is based on NoSQL terminology, there is no big learning curve, and the basic concepts of this terminology, such as storing data in the form of collections and documents, apply here as well.
Firebase also gives an option to track a user's offline and online presence with persistent capabilities. It stores a timestamp when a user gets disconnected from their network.
It is easy to scale databases when using Firebase and Firestore as the backend. It offers different pricing plans to store data, such as the Blaze plan where you can pay as you go.
Using SQLite is a common approach for storing and persisting data locally in React Native apps. It was originally designed to provide local storage for mobile apps. It offers a lightweight library based on relational databases and can be integrated easily.
Since it follows SQL-based standards, it can be said that SQLite is ACID compliant. ACID (which stands for atomicity, consistency, isolation, durability) is a set of properties of database transactions intended to guarantee validity even in the event of errors, power failures, and so on.
To persist data for offline usage, there is a package that acts as a native plugin for React Native apps for both iOS and Android platforms. This plugin is called
Security wise, SQLite doesn't support encryption out of the box. However, there is an extension named SQLite Encryption Extension (or SEE) that is provided on the official website. This extension allows SQLite to read and write data in an encrypted format using algorithms such as AES-128, AES-256, RC4, and so on.
Apart from SEE, there are other extensions available such as SQLCipher, SQLiteCrypt, and so on, that can be used to encrypt data when using the SQLite database in React Native apps.
Realm database or MongoDB Realm (since recently acquired by MongoDB) is an engine that is good at handling a large amount of data for React Native applications. During its development, it was build from scratch to let developers create mobile apps with offline capabilities. Apart from supporting mobile applications, it also supports tablets and wearables.
Realm provides a complete API to store, interact, create, and fetch data. It allows you to create relations between different rows of data using primary and foreign keys. It is based on object-oriented concepts rather than a traditional relational database, and this is one of the major differences between a Realm Database and an SQLite database. Data is directly exposed as objects and can be queried using code, thus removing the need for ORMs riddled with performance and maintenance issues. It also supports generics and vectorization.
Realm uses different encryption standards for each mobile platform for security purposes. For Android, it uses
AES-256 level of encryption and decryption of all the data stored locally. For iOS apps, the encryption is based on the iOS
CommonCrypto library that protects the app data and passwords stored in the keychain.
Realm database is open source and free to use. It comes with a Realm Studio which is a developer tool to manage database fields inside a Realm database. With Realm Studio, you can open and edit local and synced Realms, and administer any Realm Object Server instance. You can perform basic tasks such as connecting to your instance, browsing data, and viewing logs. This development tool is available for all major platforms.
Realm also offers a paid option to store data in the cloud and get real-time synchronization for your mobile app called Realm platform.
This database is built using CouchDB protocols and supports both online and offline capabilities. CouchDB is a NoSQL database created in 2005 by Damien Katz and now maintained by the Apache Software Foundation. It enables applications to store data locally while offline, then synchronize it with CouchDB and compatible servers when the application is back online, keeping the user's data in sync no matter where they next login.
PouchDB is lightweight as a dependency – just
46kB when gzipped. Securing data is easy when using PouchDB as the local database for a React Native app. It has an inbuilt authentication framework for React Native, and it offers features such as storing the password in hash form using the PBKDF2 crypto algorithm (a standard key derivation function) to protect encrypted keys from brute force attacks. It also supports SSL, which means that the data can be easily encrypted using AES256.
WatermelonDB is a high-performance and reactive open source database. It is built on top of SQLite. It is primarily made for the React Native framework and has the potential of scaling millions of records without any loss in speed or accuracy.
It has capabilities to handle data persistence on low-end devices by being lazy. Nothing is loaded unless requested, and since all querying is performed directly on the database on a separate native thread, most queries resolve in an instant.
It is fully observable. This means that whenever there is a change in one of the records, all UI that depends on it will automatically re-render. For example, completing a task in a to-do app will re-render the task component, the list (to reorder), and all relevant task counters. This makes a WatermelonDB highly reactive and gives an advantage over the other databases listed in this post. Another reason why this database is popular.
It also supports static typing. If you use Flow or TypeScript in your React Native apps, you won't have a problem working on a team. It is multi-platform and supports iOS, Android, and web. The reactive API to query and mutate data is similar to that of RxJS. Offline synchronization capabilities are there and you can hook your own backend to do so.
Vasern is an open-source database suitable for React Native apps. It is a data storage tool for React Native that includes linked-consistent key-value stores. Its data engine is built natively to achieve native performance. The API of this database is not just made for local data storage but also for synchronizing the clients of a particular app.
It supports cloud storage and provides end-to-end database synchronization. The database mainly focuses on the ease of development, performance, and dependency size. All basic data types are supported by this database. It has its own id implementation and requires a schema to be set to help Documents define its properties data type and validate records.
With so many suitable database options for your next React Native app, there is always a database that will perfectly suit your requirements. I hope this post gives you an overview in that area.
For more posts on React Native, please check out my blog at amanhimself.dev.