Skip to content

Commit

Permalink
Initial draft
Browse files Browse the repository at this point in the history
  • Loading branch information
cbullinger committed Oct 31, 2023
1 parent 4b0f02f commit 678ad4f
Show file tree
Hide file tree
Showing 5 changed files with 541 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
package com.mongodb.realm.realmkmmapp

import io.realm.kotlin.ext.backlinks
import io.realm.kotlin.ext.realmDictionaryOf
import io.realm.kotlin.ext.realmListOf
import io.realm.kotlin.types.RealmInstant
import io.realm.kotlin.types.RealmList
import io.realm.kotlin.types.RealmObject
import io.realm.kotlin.ext.realmSetOf
import io.realm.kotlin.query.RealmResults
import io.realm.kotlin.types.*
import io.realm.kotlin.types.annotations.PrimaryKey
import org.mongodb.kbson.ObjectId

// :replace-start: {
// "terms": {
// "ExampleSyncObject_": "",
// "ExampleSyncRelationship_": "",
// "SyncTask": "Task"
// }
// }
Expand Down Expand Up @@ -44,6 +47,86 @@ class Team : RealmObject {
}
// :snippet-end:

/*
** Used on Model Data with Device Sync page **
*/

// :snippet-start: sync-define-realm-object
// Implements the `RealmObject` interface
class ExampleSyncObject_Frog : RealmObject {
@PrimaryKey
var _id: ObjectId = ObjectId()
var name: String = ""
var weight: Double = 0.0
var hasSiblings: Boolean = false
var birthdate: RealmInstant? = null
var fliesEaten: MutableRealmInt? = null
var favoriteThings: RealmList<RealmAny?> = realmListOf()
}
// :snippet-end:

class ExampleSyncRelationship_Frog : RealmObject {
@PrimaryKey
var _id: ObjectId = ObjectId()
var name: String = ""
var age: Int = 0
var favoritePond: ExampleSyncRelationship_Pond? = null
var bestFriend: ExampleSyncRelationship_Frog? = null
}
class ExampleSyncRelationship_Pond : RealmObject {
@PrimaryKey
var _id: ObjectId = ObjectId()
var name: String = ""
}

class ExampleSyncRelationship_Forest : RealmObject {
@PrimaryKey
var _id: ObjectId = ObjectId()
var name: String = ""
// Set of RealmObject type (CANNOT be null)
var frogsThatLiveHere: RealmSet<ExampleSyncRelationship_Frog> = realmSetOf()
// List of RealmObject type (CANNOT be null)
var nearbyPonds: RealmList<ExampleSyncRelationship_Pond> = realmListOf()
}

class ExampleSyncRelationship_User : RealmObject {
@PrimaryKey
var _id: ObjectId = ObjectId()
var name: String = ""
// List of child RealmObject type (CANNOT be nullable)
var posts: RealmList<ExampleSyncRelationship_Post> = realmListOf()
// Set of child RealmObject type (CANNOT be nullable)
var favoritePosts: RealmSet<ExampleSyncRelationship_Post> = realmSetOf()
// Dictionary of child RealmObject type (value MUST be nullable)
var postByYear: RealmDictionary<ExampleSyncRelationship_Post?> = realmDictionaryOf()
}

// Backlink of RealmObject must be RealmResults<E> of parent object type
class ExampleSyncRelationship_Post : RealmObject {
var title: String = ""
var date: RealmInstant = RealmInstant.now()
// Backlink to parent RealmObject type (CANNOT be null & MUST be val)
val user: RealmResults<ExampleSyncRelationship_User> by backlinks(ExampleSyncRelationship_User::posts)
}
class ExampleSyncRelationship_Contact : RealmObject {
@PrimaryKey
var _id: ObjectId = ObjectId()
var name: String = ""
// Property of EmbeddedRealmObject type (MUST be null)
var address: ExampleSyncRelationship_EmbeddedAddress? = null
}

class ExampleSyncRelationship_EmbeddedAddress : EmbeddedRealmObject {
var propertyOwner: ExampleSyncRelationship_Contact? = null
var street: String? = ""
// Embed another EmbeddedRealmObject type
var country: ExampleSyncRelationship_EmbeddedCountry? = null
}

class ExampleSyncRelationship_EmbeddedCountry : EmbeddedRealmObject {
var name: String = ""
}


/*
** Used on Add Sync to App page **
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package com.mongodb.realm.realmkmmapp

import io.realm.kotlin.Realm
import io.realm.kotlin.ext.query
import io.realm.kotlin.ext.realmListOf
import io.realm.kotlin.mongodb.App
import io.realm.kotlin.mongodb.Credentials
import io.realm.kotlin.mongodb.sync.SyncConfiguration
import io.realm.kotlin.mongodb.syncSession
import io.realm.kotlin.types.MutableRealmInt
import io.realm.kotlin.types.RealmAny
import io.realm.kotlin.types.RealmInstant
import kotlinx.coroutines.runBlocking
import org.mongodb.kbson.ObjectId
import kotlin.test.Test
import kotlin.time.Duration.Companion.seconds


// :replace-start: {
// "terms": {
// "ExampleSyncObject_": "",
// "ExampleSyncRelationship_": ""
// }
// }
class SchemaSyncTest : RealmTest() {

@Test
fun syncRealmObjectSchemaTest() {
val credentials = Credentials.anonymous(reuseExisting = false)
runBlocking {
val app = App.create(FLEXIBLE_APP_ID)
val user = app.login(credentials)
val flexSyncConfig = SyncConfiguration.Builder(user, setOf(ExampleSyncObject_Frog::class))
.initialSubscriptions { realm -> add(realm.query<ExampleSyncObject_Frog>(), "all-frogs") }
.build()
val syncRealm = Realm.open(flexSyncConfig)
Log.v("Successfully opened realm: ${syncRealm.configuration}")

val oid = ObjectId()
syncRealm.write {
copyToRealm(ExampleSyncObject_Frog().apply {
_id = oid
name = "Kermit"
weight = 4.5
hasSiblings = true
birthdate = RealmInstant.from(1_577_996_800, 0)
fliesEaten = MutableRealmInt.create(2)
favoriteThings = realmListOf(
RealmAny.create(42),
RealmAny.create("rainbows"),
RealmAny.create(ExampleSyncObject_Frog().apply {
name = "Kermit Jr."
})
)
})
}
syncRealm.syncSession.uploadAllLocalChanges(30.seconds)
user.delete()
syncRealm.close()
}
}
}

// :replace-end:
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Implements the `RealmObject` interface
class Frog : RealmObject {
@PrimaryKey
var _id: ObjectId = ObjectId()
var name: String = ""
var weight: Double = 0.0
var hasSiblings: Boolean = false
var birthdate: RealmInstant? = null
var fliesEaten: MutableRealmInt? = null
var favoriteThings: RealmList<RealmAny?> = realmListOf()
}
Loading

0 comments on commit 678ad4f

Please sign in to comment.