Skip to content

Commit

Permalink
DOCSP-30343: Move RealmSet content to CRUD pages (mongodb#2942)
Browse files Browse the repository at this point in the history
## Pull Request Info

Move RealmSet content from [Data
Types](https://www.mongodb.com/docs/realm/sdk/kotlin/realm-database/schemas/supported-types/)
page to respective CRUD pages

### Jira

- https://jira.mongodb.org/browse/DOCSP-30343

### Staged Changes

- [Data
Types](https://docs-mongodbcom-staging.corp.mongodb.com/realm/docsworker-xlarge/docsp-30343-create/sdk/kotlin/realm-database/schemas/supported-types/#realmset)
- [Define Object
Model](https://docs-mongodbcom-staging.corp.mongodb.com/realm/docsworker-xlarge/docsp-30343-create/sdk/kotlin/realm-database/schemas/define-realm-object-model/#define-a-realmset)
-
[Create](https://docs-mongodbcom-staging.corp.mongodb.com/realm/docsworker-xlarge/docsp-30343-create/sdk/kotlin/realm-database/crud/create/)
-
[Read](https://docs-mongodbcom-staging.corp.mongodb.com/realm/docsworker-xlarge/docsp-30343-create/sdk/kotlin/realm-database/crud/read/)
-
[Update](https://docs-mongodbcom-staging.corp.mongodb.com/realm/docsworker-xlarge/docsp-30343-create/sdk/kotlin/realm-database/crud/update/)
-
[Delete](https://docs-mongodbcom-staging.corp.mongodb.com/realm/docsworker-xlarge/docsp-30343-create/sdk/kotlin/realm-database/crud/delete/)

### Reminder Checklist

If your PR modifies the docs, you might need to also update some
corresponding
pages. Check if completed or N/A.

- [x] Create Jira ticket for corresponding docs-app-services update(s),
if any
- [x] Checked/updated Admin API
- [x] Checked/updated CLI reference

### Review Guidelines


[REVIEWING.md](https://github.com/mongodb/docs-realm/blob/master/REVIEWING.md)
  • Loading branch information
cbullinger authored Aug 23, 2023
1 parent 493cf40 commit 1ca3519
Show file tree
Hide file tree
Showing 26 changed files with 468 additions and 331 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,20 @@ import io.realm.kotlin.Realm
import io.realm.kotlin.RealmConfiguration
import io.realm.kotlin.ext.query
import io.realm.kotlin.ext.realmDictionaryOf
import io.realm.kotlin.ext.realmSetOf
import io.realm.kotlin.internal.platform.runBlocking
import io.realm.kotlin.query.RealmResults
import io.realm.kotlin.types.RealmDictionary
import io.realm.kotlin.types.RealmObject
import io.realm.kotlin.types.RealmSet
import org.mongodb.kbson.ObjectId
import kotlin.test.Test
import kotlin.test.assertEquals

// :replace-start: {
// "terms": {
// "CreateTest_": ""
// "CreateTest_": "",
// "RealmSet_": ""
// }
// }

Expand All @@ -26,7 +29,75 @@ class CreateTest_Frog : RealmObject {
}
// :snippet-end:

// :snippet-start: define-a-realm-set
class RealmSet_Frog : RealmObject {
var _id: ObjectId = ObjectId()
var name: String = ""
var favoriteSnacks: RealmSet<Snack> = realmSetOf()
}

class Snack : RealmObject {
var _id: ObjectId = ObjectId()
var name: String = ""
}
// :snippet-end:

class CreateTest: RealmTest() {

@Test
fun createRealmSetType() {
runBlocking {
val config = RealmConfiguration.Builder(setOf(RealmSet_Frog::class, Snack::class))
.inMemory()
.build()
val realm = Realm.open(config)
Log.v("Successfully opened realm: ${realm.configuration.path}")

// :snippet-start: create-realm-set
realm.write {
// Create a Frog object named 'Kermit'
// Add item to the RealmSet using the add() method
val frog = copyToRealm(
RealmSet_Frog().apply {
name = "Kermit"
favoriteSnacks.add(Snack().apply { name = "flies" })
}
)
assertEquals(1, frog.favoriteSnacks.size) // :remove:
assertEquals("flies", frog.favoriteSnacks.first().name) // :remove:
println(frog.favoriteSnacks.first().name) // prints "flies"
}
// :snippet-end:
// :snippet-start: add-all-to-realm-set
realm.write {
val frog = query<RealmSet_Frog>().find().first()
val snackSet = frog.favoriteSnacks

// Create two more Snack objects
val cricketsSnack = copyToRealm(
Snack().apply {
name = "crickets"
}
)
val wormsSnack = copyToRealm(
Snack().apply {
name = "worms"
}
)

// Add multiple items to the RealmSet using the addAll() method
snackSet.addAll(setOf(cricketsSnack, wormsSnack))
assertEquals(3, snackSet.size) // :remove:
}
// :snippet-end:
realm.writeBlocking {
val frogs = query<RealmSet_Frog>().find().first()
delete(frogs)
}
realm.close()
}
}

@Test
fun createRealmDictionaryType() {
runBlocking {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@ import io.realm.kotlin.types.RealmObject
import org.mongodb.kbson.ObjectId
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertTrue
import kotlin.test.assertFalse
import kotlin.test.assertTrue

// :replace-start: {
// "terms": {
// "UpdateTest_": ""
// "UpdateTest_": "",
// "RealmSet_": ""
// }
// }

Expand All @@ -27,7 +28,71 @@ class DeleteTest_Frog : RealmObject {
}

class DeleteTest: RealmTest() {

@Test
fun deleteRealmSetType() {
runBlocking {
val config = RealmConfiguration.Builder(setOf(RealmSet_Frog::class, Snack::class))
.inMemory()
.build()
val realm = Realm.open(config)
Log.v("Successfully opened realm: ${realm.configuration.path}")

realm.write { copyToRealm(
RealmSet_Frog().apply {
name = "Kermit"
favoriteSnacks.add(Snack().apply { name = "Flies" })
favoriteSnacks.add(Snack().apply { name = "Crickets" })
favoriteSnacks.add(Snack().apply { name = "Worms" })
})
}
// :snippet-start: remove-item-from-set
realm.write {
val myFrog = realm.query<RealmSet_Frog>("name == $0", "Kermit").find().first()
val snackSet = findLatest(myFrog)!!.favoriteSnacks

// Remove the Flies snack from the set
val fliesSnack = snackSet.first { it.name == "Flies" }
snackSet.remove(fliesSnack)
assertFalse(snackSet.contains(fliesSnack)) // :remove:

// Remove all snacks from the set
val allSnacks = findLatest(myFrog)!!.favoriteSnacks
snackSet.removeAll(allSnacks)
// :remove-start:
// TODO update test once https://github.com/realm/realm-kotlin/issues/1097 is fixed in v1.11.0
// assertTrue(set.isEmpty())
snackSet.removeAll(allSnacks) // have to call twice to actually remove all items until bug is fixed
// :remove-end:
}
// :snippet-end:
realm.write {
val myFrog = realm.query<RealmSet_Frog>("name == $0", "Kermit").find().first()
val snackSet = findLatest(myFrog)!!.favoriteSnacks
val snack1 = this.copyToRealm(Snack().apply {
name = "snack1"
})
val snack2 = this.copyToRealm(Snack().apply {
name = "snack2"
})
val snack3 = this.copyToRealm(Snack().apply {
name = "snack3"
})

snackSet.addAll(setOf(snack1, snack2, snack3))
assertEquals(3, snackSet.size)

// :snippet-start: clear-set
// Clear all snacks from the set
snackSet.clear()
// :snippet-end:
assertTrue(snackSet.isEmpty())
}
realm.close()
}
}

@Test
fun deleteRealmDictionaryType() {
runBlocking {
val config = RealmConfiguration.Builder(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@ import io.realm.kotlin.types.annotations.PrimaryKey
import org.mongodb.kbson.ObjectId
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertNotNull
import kotlin.test.assertTrue

// :replace-start: {
// "terms": {
// "ReadTest_": ""
// "ReadTest_": "",
// "RealmSet_": ""
// }
// }

Expand All @@ -28,6 +30,61 @@ class ReadTest_Frog : RealmObject {
}

class ReadTest: RealmTest() {

@Test
fun readRealmSetType() {
runBlocking {
val config = RealmConfiguration.Builder(setOf(RealmSet_Frog::class, Snack::class))
.inMemory()
.build()
val realm = Realm.open(config)
Log.v("Successfully opened realm: ${realm.configuration.path}")

// :snippet-start: read-realm-set
realm.write {
// Create a Frog object named 'Kermit'
// with a RealmSet of favorite snacks
val frog = copyToRealm(
RealmSet_Frog().apply {
name = "Kermit"
favoriteSnacks.add(Snack().apply { name = "Flies" })
favoriteSnacks.add(Snack().apply { name = "Crickets" })
favoriteSnacks.add(Snack().apply { name = "Worms" })
}
)
// Query for frogs that have worms as a favorite snack
val frogs = query<RealmSet_Frog>("favoriteSnacks.name == $0", "Worms").find().first()
assertEquals("Kermit", frogs.name) // :remove:

// Query for specific snacks
val wormsSnack = frog.favoriteSnacks.first { it.name == "Worms" }
assertNotNull(wormsSnack) // :remove:
}
// :snippet-end:
realm.write {
// :snippet-start: realm-set-contains
val frog = query<RealmSet_Frog>("name == $0", "Kermit").find().first()
val snackSet = findLatest(frog)?.favoriteSnacks

// Check if the set contains a particular value
val wormSnack = snackSet?.first { it.name == "Worms" }
Log.v("Does Kermit eat worms?: ${snackSet?.contains(wormSnack)}") // true

// Check if the set contains multiple values
val containsAllSnacks = snackSet?.containsAll(snackSet)
Log.v("Does Kermit eat flies, crickets, and worms?: $containsAllSnacks") // true
// :snippet-end:
assertTrue(snackSet?.contains(wormSnack)?: false)
assertTrue(containsAllSnacks?: false)
}
realm.writeBlocking {
val frogs = query<RealmSet_Frog>().find().first()
delete(frogs)
}
realm.close()
}
}

@Test
fun readRealmDictionaryType() {
runBlocking {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,9 @@ import io.realm.kotlin.types.annotations.Ignore
import io.realm.kotlin.types.annotations.Index
import io.realm.kotlin.types.annotations.PersistedName
import io.realm.kotlin.types.annotations.PrimaryKey
import kotlinx.coroutines.CoroutineScope
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

// :replace-start: {
// "terms": {
Expand Down Expand Up @@ -69,16 +65,14 @@ class Post: RealmObject {
}
// :snippet-end:

// :snippet-start: define-a-realm-set
class Frog2 : RealmObject {
var name: String = ""
var favoriteSnacks: RealmSet<Snack> = realmSetOf<Snack>()
var favoriteSnacks: RealmSet<Snack2> = realmSetOf<Snack2>()
}

class Snack : RealmObject {
class Snack2 : RealmObject {
var name: String? = null
}
// :snippet-end:

// :snippet-start: uuid
class Cat: RealmObject {
Expand Down Expand Up @@ -221,93 +215,6 @@ class SchemaTest: RealmTest() {
realm.close()
}
}
@Test
@kotlin.test.Ignore // ignored until bugfix in 1.7.1 release
fun createRealmSetTypes() {
runBlocking {
val config = RealmConfiguration.Builder(setOf(Frog2::class, Snack::class))
.build()
val realm = Realm.open(config)
Log.v("Successfully opened realm: ${realm.configuration.path}")

// :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 {
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 {
name = "flies"
})

// Add the flies to the RealmSet of Kermit's favorite snacks
set.add(fliesSnack)
assertEquals(1, set.size) // :remove:
}
// :snippet-end:

// :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 cricketsSnack = this.copyToRealm(Snack().apply {
name = "crickets"
})
val earthWormsSnack = this.copyToRealm(Snack().apply {
name = "earthworms"
})
val waxWormsSnack = this.copyToRealm(Snack().apply {
name = "waxworms"
})

set.addAll(setOf(cricketsSnack, earthWormsSnack, waxWormsSnack))
println("$set") // :remove:
assertEquals(4, set.size) // :remove:
// :uncomment-start:
//}
// :uncomment-end:
// :snippet-end:

// :snippet-start: set-contains
Log.v("Does Kermit eat earthworms?: ${set.contains(earthWormsSnack)}") // true
// :snippet-end:

// :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:

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

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

// :snippet-start: remove-multiple-items-from-set
set.removeAll(set)
// :snippet-end:
}


// :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)
}
}
}

// :replace-end:
Loading

0 comments on commit 1ca3519

Please sign in to comment.