Skip to content

Commit

Permalink
Initial draft
Browse files Browse the repository at this point in the history
  • Loading branch information
cbullinger committed Aug 8, 2023
1 parent c08a05e commit 09a957e
Show file tree
Hide file tree
Showing 8 changed files with 282 additions and 157 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,53 @@ class CreateTest: RealmTest() {
realm.close()
}
}

@Test
fun createToOneRelationship() {
class Owner : RealmObject {
var _id: ObjectId = ObjectId()
var name: String = ""
}
// :snippet-start: define-to-one-relationship
class Frog : RealmObject {
var _id: ObjectId = ObjectId()
var name: String = ""
var owner: Owner? = null
}
// :snippet-end:

runBlocking {
val config = RealmConfiguration.Builder(
schema = setOf(Frog::class, Owner::class) // Pass the defined class as the object schema
)
.inMemory()
.build()
val realm = Realm.open(config)
Log.v("Successfully opened realm: ${realm.configuration.name}")

realm.write {
val frogs = query<Frog>().find()
delete(frogs)
assertEquals(0, frogs.size)
}

// :snippet-start: create-to-one-relationship
realm.write {
copyToRealm(Frog().apply {
name = "Kermit"
owner = copyToRealm(Owner().apply {
name = "Jim Henson"
})
})
}
// :snippet-end:
val frogs = realm.query<Frog>().find()
assertEquals(1, frogs.size)
val thisFrog = frogs.first()
thisFrog.owner?.let { assertEquals("Jim Henson", it.name) }
realm.close()
}
}
}

// :replace-end:
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import io.realm.kotlin.ext.query
import io.realm.kotlin.ext.realmListOf
import io.realm.kotlin.ext.realmSetOf
import io.realm.kotlin.internal.platform.runBlocking
import io.realm.kotlin.notifications.SetChange
import io.realm.kotlin.notifications.SetChangeSet
import io.realm.kotlin.query.RealmResults
import io.realm.kotlin.types.*
import io.realm.kotlin.types.annotations.Ignore
Expand All @@ -19,8 +21,7 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.datetime.Instant
import org.mongodb.kbson.ObjectId
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.*

// :replace-start: {
// "terms": {
Expand Down Expand Up @@ -222,25 +223,45 @@ class SchemaTest: RealmTest() {
}
}
@Test
@kotlin.test.Ignore // ignored until bugfix in 1.7.1 release
fun createRealmSetTypes() {
runBlocking {
val config = RealmConfiguration.Builder(setOf(Frog2::class, Snack::class))
.inMemory()
.build()
val realm = Realm.open(config)
Log.v("Successfully opened realm: ${realm.configuration.path}")

val kermitFrog = realm.query<Frog2>("name = 'Kermit'").first().find()
val kermitSnacks = kermitFrog?.favoriteSnacks
val job = CoroutineScope(Dispatchers.Default).launch {
kermitSnacks
?.asFlow()
?.collect() { changes: SetChange<Snack> ->
when (changes) {
is SetChangeSet -> {
changes.insertions
Log.v("Set of snacks changed: $changes")
}
else -> {
Log.v("Set of snacks unchanged")
}

}
val set = kermitFrog.favoriteSnacks

assertTrue(set.last().name.equals("new snack"))
}
}
// :snippet-start: add-item-to-realm-set
realm.write {
// Create a Frog object named 'Kermit' that will have a RealmSet of favorite snacks
val frog = this.copyToRealm(Frog2().apply {
val frog = copyToRealm(Frog2().apply {
name = "Kermit"
})
// Get the RealmSet of favorite snacks from the Frog object we just created
val set = frog.favoriteSnacks

// Create a Snack object for the Frog to add to Kermit's favorite snacks
val fliesSnack = this.copyToRealm(Snack().apply {
val fliesSnack = copyToRealm(Snack().apply {
name = "flies"
})

Expand All @@ -252,16 +273,16 @@ class SchemaTest: RealmTest() {

// :snippet-start: add-all-to-realm-set
realm.write {
val myFrog: Frog2 = realm.query<Frog2>("name = 'Kermit'").first().find()!!
val set = findLatest(myFrog)!!.favoriteSnacks
val myFrog = query<Frog2>("name = 'Kermit'").find().first()
val set = myFrog.favoriteSnacks

val cricketsSnack = this.copyToRealm(Snack().apply {
val cricketsSnack = copyToRealm(Snack().apply {
name = "crickets"
})
val earthWormsSnack = this.copyToRealm(Snack().apply {
val earthWormsSnack = copyToRealm(Snack().apply {
name = "earthworms"
})
val waxWormsSnack = this.copyToRealm(Snack().apply {
val waxWormsSnack = copyToRealm(Snack().apply {
name = "waxworms"
})

Expand All @@ -276,38 +297,40 @@ class SchemaTest: RealmTest() {
// :snippet-start: set-contains
Log.v("Does Kermit eat earthworms?: ${set.contains(earthWormsSnack)}") // true
// :snippet-end:
assertTrue(set.contains(earthWormsSnack))

// :snippet-start: set-contains-multiple-items
val containsAllSnacks = set.containsAll(set)
Log.v("Does Kermit eat crickets, earthworms, and wax worms?: $containsAllSnacks") // true
// :snippet-end:
assertTrue(containsAllSnacks)

// :snippet-start: remove-item-from-set
val fliesSnack = realm.query<Snack>("name = 'flies'").first().find()
val fliesSnack = query<Snack>("name = 'flies'").first().find()

set.remove(fliesSnack)
// :snippet-end:
assertFalse(set.contains(fliesSnack))

val deleteSnacks = findLatest(myFrog)!!.favoriteSnacks
// :snippet-start: remove-multiple-items-from-set
set.removeAll(set)
set.removeAll(deleteSnacks)
// :snippet-end:
}

// :snippet-start: react-to-changes-from-the-set
val newSnack = Snack().apply {
name = "new snack"
}
set.add(newSnack)
}

// :snippet-start: react-to-changes-from-the-set
val kermitFrog = realm.query<Frog2>("name = 'Kermit'").first().find()
val job = CoroutineScope(Dispatchers.Default).launch {
kermitFrog?.favoriteSnacks
?.asFlow()
?.collect() {
// Listen for changes to the RealmSet
}
}
// :snippet-end:
job.cancel()
realm.close()
Realm.deleteRealm(config)
// :snippet-end:

realm.close()
Realm.deleteRealm(config)
}
}
}
}

// :replace-end:
48 changes: 43 additions & 5 deletions source/sdk/kotlin/realm-database/crud/create.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ Create Realm Objects - Kotlin SDK
You can only insert new objects into a realm within a
:ref:`write transaction <kotlin-write-transactions>`.

Create a New Realm Object
-------------------------
Create an Object
----------------

Instantiate Realm objects as you would any other object. In a
transaction, you can add the object to the realm if the
Expand Down Expand Up @@ -51,17 +51,55 @@ To persist a new object to a realm:
:ref:`Upsert a Realm Object <kotlin-upsert-an-object>`.

Create an Embedded Object
-------------------------
~~~~~~~~~~~~~~~~~~~~~~~~~

To create an :ref:`embedded object <kotlin-embedded-objects>`, assign an instance of the
To create an :ref:`embedded object <kotlin-embedded-objects>`, assign an
instance of the
`EmbeddedRealmObject <{+kotlin-local-prefix+}io.realm.kotlin.types/-embedded-realm-object/index.html>`__
to a parent object's property:

.. literalinclude:: /examples/generated/kotlin/DataTypesTest.snippet.create-embedded-object.kt
:language: kotlin

Create an Object with a RealmSet Property
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

You can create objects that contain :ref:`RealmSet <kotlin-realm-set>`
properties as you would any Realm object, but you can only set
the values of a mutable set property within a write transaction.

Add an Item to a RealmSet
`````````````````````````

To add an item to a ``RealmSet``, pass the object you want to add to the
`set.add() <{+kotlin-local-prefix+}io.realm.kotlin.types/-realm-set/index.html#-153241610%2FFunctions%2F-1651551339>`__
method.

In the following example, we get the ``favoriteSnacks`` set, then add a new
``Snack`` object to the set of favorite snacks:

.. literalinclude:: /examples/generated/kotlin/SchemaTest.snippet.add-item-to-realm-set.kt
:language: kotlin

Add Many Items to a RealmSet
````````````````````````````

To add multiple items to a ``RealmSet``, pass the elements you want
to add to the `set.addAll()
<{+kotlin-local-prefix+}io.realm.kotlin.types/-realm-set/index.html#-800009087%2FFunctions%2F-1651551339>`__
method.

In the following example, we create several ``Snack`` objects and use the
`setOf
<https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/set-of.html>`__
method to create a read-only set of the given elements. Then, we pass these
elements to the ``set.addAll()`` method to add them to our ``Frog`` object's favorite snacks:

.. literalinclude:: /examples/generated/kotlin/SchemaTest.snippet.add-all-to-realm-set.kt
:language: kotlin

Create an Object with a Dictionary Property
-------------------------------------------
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

You can create objects with
`RealmDictionary <{+kotlin-local-prefix+}io.realm.kotlin.types/-realm-dictionary/index.html>`__
Expand Down
32 changes: 32 additions & 0 deletions source/sdk/kotlin/realm-database/crud/delete.txt
Original file line number Diff line number Diff line change
Expand Up @@ -134,3 +134,35 @@ Deleting the parent object automatically deletes all of its embedded objects.

.. literalinclude:: /examples/generated/kotlin/DataTypesTest.snippet.delete-embedded-object.kt
:language: kotlin

Delete Items from a RealmSet
----------------------------

:ref:`RealmSet <kotlin-realm-set>` instances that contain Realm objects
only store references to those objects, so deleting a Realm object from a
realm also deletes that object from any ``RealmSet`` instances that contain
the object.

Remove an Item from a RealmSet
``````````````````````````````

To remove an item from a ``RealmSet``, pass the element you want to
delete to
`set.remove() <{+kotlin-local-prefix+}io.realm.kotlin.types/-realm-set/index.html#-1503494415%2FFunctions%2F-1651551339>`__.

In this example, we add

.. literalinclude:: /examples/generated/kotlin/SchemaTest.snippet.remove-item-from-set.kt
:language: kotlin

Remove Multiple Items from a RealmSet
`````````````````````````````````````

To remove multiple items from a ``RealmSet``, pass the elements you want to
delete to
`set.removeAll() <{+kotlin-local-prefix+}io.realm.kotlin.types/-realm-set/index.html#430447804%2FFunctions%2F-1651551339>`__

In the following example, we delete the set of favorite snacks we created earlier:

.. literalinclude:: /examples/generated/kotlin/SchemaTest.snippet.remove-multiple-items-from-set.kt
:language: kotlin
46 changes: 42 additions & 4 deletions source/sdk/kotlin/realm-database/crud/read.txt
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ and pass the type as a type parameter to `realm.query()
.. literalinclude:: /examples/generated/kotlin/CRUDTest.snippet.find-all-objects-of-a-type.kt
:language: kotlin

Find an Embedded Object
~~~~~~~~~~~~~~~~~~~~~~~
Query an Embedded Object
~~~~~~~~~~~~~~~~~~~~~~~~

You can query an :ref:`embedded object <kotlin-embedded-objects>` directly or
through the parent object. You can also use the
Expand All @@ -61,8 +61,8 @@ method to access the parent of the embedded object.
.. literalinclude:: /examples/generated/kotlin/DataTypesTest.snippet.query-embedded-objects.kt
:language: kotlin

Read an Object with a Dictionary Property
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Query a Dictionary Property
~~~~~~~~~~~~~~~~~~~~~~~~~~~

You can iterate and check the values of a
`RealmDictionary <{+kotlin-local-prefix+}io.realm.kotlin.types/-realm-dictionary/index.html>`__
Expand All @@ -71,6 +71,44 @@ as you would a Kotlin Map.
.. literalinclude:: /examples/generated/kotlin/ReadTest.snippet.read-realm-dictionary.kt
:language: kotlin

Query a RealmSet Property
~~~~~~~~~~~~~~~~~~~~~~~~~

You can query a :ref:`RealmSet <kotlin-realm-set>` to check if it contains
an element.

Check if the RealmSet Contains an Item
``````````````````````````````````````

To check if the ``RealmSet`` contains a particular item, pass the element to
`set.contains()
<{+kotlin-local-prefix+}io.realm.kotlin.types/-realm-set/index.html#607101300%2FFunctions%2F-1651551339>`__.
The method returns ``true`` if the set contains the element.

In the following example, we pass a ``Snack`` object to the
``setOf()`` method to create a read-only set of favorite snacks.
Then, we check if the ``RealmSet`` contains this ``Snack`` object
by passing the read-only set to ``set.contains()``:

.. literalinclude:: /examples/generated/kotlin/SchemaTest.snippet.set-contains.kt
:language: kotlin

Check if the RealmSet Contains Multiple Items
`````````````````````````````````````````````

To check if the ``RealmSet`` contains multiple items, pass the elements to
`set.containsAll()
<{+kotlin-local-prefix+}io.realm.kotlin.types/-realm-set/index.html#1318510207%2FFunctions%2F-1651551339>`__.
The method returns ``true`` if the set contains the specified elements.

In the following example, we pass ``Snack`` objects to
the ``setOf()`` method to create a read-only set of favorite snacks.
Then, we check if the ``RealmSet`` contains all of these ``Snack`` objects
by passing the read-only set to ``set.containsAll()``:

.. literalinclude:: /examples/generated/kotlin/SchemaTest.snippet.set-contains-multiple-items.kt
:language: kotlin

Filter Data
-----------

Expand Down
Loading

0 comments on commit 09a957e

Please sign in to comment.