Monday, January 28, 2019

Introduction to Realm Mobile Database In React Native

Nowadays we have a lot of options when deciding to choose a database solution. We have different attractive options each of which has it's strengths and weaknesses. One I have found to be outstanding is Realm database. Realm database can be used by Android, IOS, React and even Xamarin developers.

When Realm was launched in 2014, the goal was to help mobile developers build better apps faster by giving them a robust alternative to SQLite and Core Data. The Realm Mobile Database is a cross-platform database solution that can be used as an alternative to SQLite and Core Data. Compared to these two options, Realm is easier to set up and use. To perform the same operation in Realm, you usually end up writing fewer lines of code than you would with SQLite or Core Data. On performance, Realm is said to be faster and it also offers other modern features such as encryption, JSON support and data change notifications.

Advantages of Realm over SQLite:
  1. easy to use
  2. faster than SQLite (up to 10x speed up over raw SQLite for normal operations)
  3. object conversion handled for you
  4. very responsive team
  5. Cross-platform support.
  6. convenient for creating and storing data on the fly.

Also there are some disadvantages which might be taken into consideration:
  1. no importing
  2. not a lot of content online
  3. still under active development
  4. can’t access objects across threads
Introduction to Realm Mobile Database In React Native

What is Realm ?

Realm is not an ORM, and is not built on top of SQLite. Instead we’ve built a full database for mobile app developers, one that uses native JavaScript objects that are dynamically mapped to a full, custom database engine (not just a key-value store). This allows us to provide a simple API while preserving performance. With Realm, you can model complex data, link objects in a graph, and compose advanced queries.

Models & Writes :

You can create table structure by following below model in react native application :

class Employee {}
Employee.schema = {
    name: 'Employee',
    properties: {
        name: 'string',
        salary: 'int',
    }
};

let realm = new Realm({schema: [Employee]});

realm.write(() => {
    realm.create('Employee', { name: 'skptricks', salary: 3000 });
});

Queries :

You can perform query operation in realm database by following below lines of code.

// Basic query
let r = realm.objects('Employee').filtered('salary < 200');

// Queries are chainable
let r2 = r.filtered('name contains "s"');
r2.length // => 1

realm.write(() => {
    realm.create('Employee', { name: 'sumit', salary: 4000 });
});

// Queries are updated in real-time
r2.length // => 2

Relationships :

you can create one to one, one to many relationship using realm database. For example check out the below scenario.

class Department {}
Department.schema = {
    name: 'string',
    emp: {type: 'list', objectType: 'Employee'},
};

let realm = new Realm({schema: [Employee, Department]});

realm.write(() => {
    let department = realm.create('Department ', {
        name: 'IT Sector',
        emp: [{name: 'amit', salary: 3989}],
    });
});


Realm also boasts of speed, according to the documentation, Realm is faster than even raw SQLite on common operations, while maintaining an extremely rich feature set. Realm is a lightweight database in Android, but it doesn't use SQLite. If we use ORMLite or ActiveAndroid or any other similar library, our data is stored in SQLite database, because these libraries give us only an overlay on SQLite.Realm occupies very less memory space compared to SQLite. So ultimately it reduces the overall size of our application. Realm is very fast in reading & writing data compared to SQLite.

Lets see the below performance statistics of realm lightweight database in android devices :

Insertion :

Introduction to Realm Mobile Database In React Native

Counts :

Introduction to Realm Mobile Database In React Native
Queries :

Introduction to Realm Mobile Database In React Native

Listed above are some of the benefits you should know about Realm as the local database for your React Native app.

Though, SQLite alone is a powerful local database for your React Native app. However, it is not useful when your application needs to scale with the increasing number of users. Realm, as a local database is the best alternative to be used in such type of case.

So, are you ready to consider realm in your React Native app? Let us know in comments below.


No comments:

Post a Comment