Realm React Native SDK
The Realm React Native SDK allows you to use Realm Database and backend Apps from React Native applications for iOS and Android written in JavaScript or TypeScript.
The React Native SDK does not support JavaScript or TypeScript applications written for web browsers. For that use case, you should consider the Web SDK. For development on Node.js without React, refer to the Node.js SDK documentation.
Local Realm Database
With the Realm React Native SDK, you can access objects stored in a local instance of Realm Database. With Realm Database, you can:
Define an Object Schema
Define your object schema by creating an object or a class.
import Realm from "realm"; const Cat = { name: "Cat", properties: { _id: "objectId", name: "string", age: "int", type: "string", }, }; // open a local realm with the 'Cat' schema const realm = await Realm.open({ schema: [Cat], });
Query Realm Database
Query for stored objects:
// retrieve the set of Cat objects const cats = realm.objects("Cat"); console.log(`There are ${cats.length} cats`); // filter for cats that are older than 7 const catsOlderThan7 = cats.filtered("age > 7"); console.log( `There are ${catsOlderThan7.length} cats older than 7 years old` ); // filter for calico cats const calicoCats = cats.filtered("type == 'Calico'"); console.log(`There are ${calicoCats.length} Calico cats`);
Update Live Objects
Update objects in Realm Database by updating field values on an instance of the object within a transaction:
// find a cat named "Clover" const cloverCat = cats.filtered("name == 'Clover'"); // update the Cat in a write transaction realm.write(() => { // update clover's age to 5 years old cloverCat.age = 5; }); // when the transaction completes, the cat's age is updated in the database console.log(`Clover the cat is ${cloverCat.age} years old`);
Watch for Object Updates
Receive object updates and notifications automatically when objects stored in Realm Database change:
// insert a cat into the database let aliceCat; realm.write(() => { aliceCat = realm.create("Cat", { _id: new BSON.ObjectID(), name: "Alice", age: 14, type: "Calico", }); }); // create a listener that logs new changes to the cat aliceCat.addListener((obj, changes) => { changes.changedProperties.forEach((changedProperty) => { console.log( `${obj.name}'s ${changedProperty} was altered to be ${obj[changedProperty]}` ); // This should log "Alice's age was altered to be 15" }); }); // update the cat realm.write(() => { aliceCat.age = 15; });
Always Access the Latest Data
Live objects keep all instances of an object up to date at all times:
// always access the latest data const realmInstanceA = await Realm.open({ schema: [Cat], }); const realmInstanceB = await Realm.open({ schema: [Cat], }); // get a reference to a single cat object // stored in the database from each realm instance const calicoCatFromRealmInstanceA = realm .objects("Cat") .filtered("type = 'Calico'")[0]; const calicoCatFromRealmInstanceB = realm .objects("Cat") .filtered("type = 'Calico'")[0]; // update cat A's name realm.write(() => { calicoCatFromRealmInstanceA.name = "Hestu"; }); // cat B instance automatically updates with the new name expect(calicoCatFromRealmInstanceB.name).toBe("Hestu");
To get started with Realm Database, try our Quick Start.
Realm Apps
Apps are backends for client applications hosted by MongoDB in the cloud. They provide the ability to synchronize data stored in Realm Database, called Atlas Device Sync, as well as a layer of backend functionality collectively called App Services. The Realm React Native SDK optionally contains the ability to access these Apps running in the cloud. In addition to local Realm Database in the SDK, Apps provide the following functionality:
Atlas Device Sync
Automatically sync data between realms on client devices and your backend MongoDB Atlas data store with Atlas Device Sync:
var config = { schema: [Cat], // predefined schema sync: { user: app.currentUser, partitionValue: "MyPartitionValue", }, }; // Open a realm with a configuration object that has a SyncConfiguration // A SyncConfiguration requires both a logged-in user and a partition value let realm = await Realm.open(config); // start a write transaction let darukCat; realm.write(() => { // get a cat from the database to update darukCat = realm.objects("Cat").filtered("name = 'Daruk")[0]; // change the cat's name darukCat.name = "Daruk Goron"; }); // when the transaction completes, the cat's name is updated in the database // and synced to the connected Realm App
App Services
- Use Realm's built-in user management to enable user account creation and user authentication across devices.
- Store data persistently with permissions in your backend App using a MongoDB data source.
- Execute logic in your backend App from a client application using Functions.
- React to events in your backend App using Triggers.
To get started with Realm, try the Quick Start with Sync.
Get Started
To start using the Realm React Native SDK in your React Native application, see Install Realm for React Native to add the Realm JS SDK dependency and then check out the Quick Start. To quickly build your App using Expo, check out the Quick Start with Expo.
To learn more about the concepts that underly Realm Database, such as write transactions, the Query Engine, and more, check out the Fundamentals.
For practical code samples of common tasks in Realm Database and Apps, take a look at the Examples.
Get Started | Fundamentals | Examples | Realm Apps Examples |
---|---|---|---|
Advanced Guides
For information about advanced concepts, such as encryption and the Query Engine, see the Advanced Guides section.