Upgrade to Pro — share decks privately, control downloads, hide ads and more …

NoSQL database for Mobile Apps

NoSQL database for Mobile Apps

Mohit Sharma

May 12, 2023
Tweet

More Decks by Mohit Sharma

Other Decks in Programming

Transcript

  1. Evolution Evilution Repo Cache Database Network API 3.1 API 3.2

    API 2.1 API 2.2 API 1.1 API 1.2 API xx API xx API xx API xx API xx API xx API xx API xx
  2. Realm Reactive Notifications Built-in Encryption Offline-First, Background Sync Object Schema

    & Relationships Simple to Learn & Master Idiomatic Query Language
  3. Realm History • Free and Open Source Database • Founded

    2011. First release in 2014 • C++ with Language SDK’s on top • Part of MongoDB since 2019 By MongoDB
  4. Realm Object Classes are the Schema class Dog : RealmObject

    { var name: String = "" var age: Int = 0 } class Person : RealmObject { var name: String = "" var dog: Dog? = null var puppies: RealmList<Expert> = RealmList() } @codeWithMohit
  5. Relationships class Person : RealmObject { var name: String =

    "" var dog: Dog? = null var puppies: RealmList<Expert> = RealmList() var tags: RealmMap<String, Object> = RealmMap() var activities: RealmSet<String> = RealmSet() } @codeWithMohit
  6. Realm // Minimal configuration val config = RealmConfiguration.with(schema = setOf(Person::class,

    Dog::class)) // Extended Configuration val config2 = RealmConfiguration.Builder(schema = setOf(Person::class, Dog::class)) .name("my-realm.realm") .deleteRealmIfMigrationNeeded() .build() // Opening a Realm val realm = Realm.open(config) // Closing a Realm (not strictly needed) realm.close() @codeWithMohit
  7. Realm // Minimal configuration val config = RealmConfiguration.with(schema = setOf(Person::class,

    Dog::class)) // Extended Configuration val config2 = RealmConfiguration.Builder(schema = setOf(Person::class, Dog::class)) .name("my-realm.realm") .deleteRealmIfMigrationNeeded() .build() // Opening a Realm val realm = Realm.open(config) // Closing a Realm (not strictly needed) realm.close() @codeWithMohit
  8. Encryption & Schema Migration • Automatic Migration for basic use-case

    like Addition or deletion. • Single call API for encrypting complete database. @codeWithMohit
  9. Realm // Minimal configuration val config = RealmConfiguration.with(schema = setOf(Person::class,

    Dog::class)) // Extended Configuration val config2 = RealmConfiguration.Builder(schema = setOf(Person::class, Dog::class)) .name("my-realm.realm") .deleteRealmIfMigrationNeeded() .build() // Opening a Realm val realm = Realm.open(config) // Closing a Realm (not strictly needed) realm.close() @codeWithMohit
  10. Dependency Injection val koinModule = module { single { RealmConfiguration.with(schema

    = setOf(Person::class)) } single { Realm.open(get()) } } class MyRepository : KoinComponent { val realm: Realm by inject() } @codeWithMohit
  11. Queries val id = "my-id" val realm: Realm by inject()

    val results: RealmResults<Person> = realm.query<Counter>("id = ${id}") @codeWithMohit
  12. Realm Query Language • alwaysFalse • alwaysTrue • and •

    beginGroup • beginsWith • between • contains • distinct • endsWith • endGroup • equalTo • greaterThan • greaterThanOrEqual • in • isEmpty • isNotEmpty • isNotNull • isNull • lessThan • lessThanOrEqual • like • limit • maximumDate • minimumDate • not • notEqualTo • or • sort • average • count • sum • max • min @codeWithMohit
  13. Listen for & React to changes in data val myPuppy

    = realm.query<Dog>("age”, “1").first().find() // Observe Realm Notifications myDog.asFlow().map{ when(it){ is InitialResults -> TODO() is UpdatedResults -> TODO() } } // Update the puppy’s age to see the effect realm.write { myPuppy.age = 3 } @codeWithMohit
  14. Atlas Device Sync Out of the box device-cloud synchronization Atlas

    Database Atlas Device Sync Mobile App @codeWithMohit
  15. Sync Conflict Resolution Offline client deletes User1 Driver adds User3

    and updates User2’s number t0 t1 Sync Changes t2 Sync Changes t2 Send Merged Changes t4 Send Merged Changes t4 Apply Conflict Resolution Rules t3 Delete Always Wins MongoDB Device Sync Last Update Wins Inserts in lists are ordered by time MongoDB Atlas Realm Sync Client Final State User List |- user0 : 000 |- user2 : 999 |- user3 : 003 Initial State User List |- user0 : 000 |- user1 : 001 |- user2 : 002
  16. Thatʼs not all ! Out-of-the-box device-cloud synchronization Mobile-optimized database Ultra-lightweight

    and fast Object-oriented No ORM or DAO required Advanced security and permissions Built-in conflict resolution The leading developer data platform on the cloud Functions Triggers GraphQL DataAPI Data Access Control Atlas Realm Kotlin SDK Atlas Device Sync Search Charts Time Series @codewithMohit