forked from mongodb/docs-realm
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4b0f02f
commit 678ad4f
Showing
5 changed files
with
541 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
examples/kotlin/shared/src/commonTest/kotlin/com/mongodb/realm/realmkmmapp/SchemaSyncTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: |
11 changes: 11 additions & 0 deletions
11
source/examples/generated/kotlin/SchemaSync.snippet.sync-define-realm-object.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
Oops, something went wrong.