Skip to content

Commit

Permalink
Add more examples
Browse files Browse the repository at this point in the history
  • Loading branch information
cbullinger committed Feb 6, 2024
1 parent 11180e7 commit 6eba3af
Show file tree
Hide file tree
Showing 10 changed files with 104 additions and 25 deletions.
70 changes: 63 additions & 7 deletions examples/dart/test/backlinks_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ class _Task {
// :snippet-end:

void main() {
test('Filter Backlinks with RQL', () {
test('Query Inverse Backlinks', () {
final config = Configuration.local([User.schema, Task.schema]);
final realm = Realm(config);

final han = User(ObjectId(), 'han', tasks: [
Task(ObjectId(), 'Pay Chewie back', false),
Task(ObjectId(), 'Get a haircut', true)
Task(ObjectId(), 'Get a haircut', false)
]);
realm.write(() => realm.add(han));
final jarjar = User(ObjectId(), 'jarjar_binks', tasks: [
Expand All @@ -48,20 +48,39 @@ void main() {
realm.write(() => realm.add(jarjar));

// :snippet-start: filter-backlinks-rql
// Filter tasks through the User's backlink property
// Filter Tasks through the User's backlink property
// using `@links.<ObjectType>.<PropertyName>` syntax
final jarjarsIncompleteTasks = realm.query<Task>(
"ALL @links.User.tasks.username == 'jarjar_binks' AND isComplete == false");

final tasksForHan = realm.query<Task>("ALL @links.User.tasks.username == 'han'");
final tasksForHan =
realm.query<Task>("ALL @links.User.tasks.username == 'han'");
// :snippet-end:

expect(jarjarsIncompleteTasks.length, 2);
expect(tasksForHan.length, 2);

// :snippet-start: query-backlink-inverse-relationship
// Tasks have an inverse relationship to Users
final inCompleteTasks = realm.query<Task>("isComplete == false");

// Find all Users who have an incomplete Task
for (final task in inCompleteTasks) {
final ownersWithIncompleteTasks = task.getBacklinks<User>('tasks');
for (final user in ownersWithIncompleteTasks) {
print("User ${user.username} has incomplete tasks.");
}
}
// :snippet-end:
expect(
inCompleteTasks
.any((element) => element.description == 'Get a haircut'),
true);

cleanUpRealm(realm);
});

test('Query Backlinks', () {
test('Query To-One Backlinks', () {
// Use to-one models from Schemas.dart
final config = Configuration.local([Person.schema, Bike.schema]);
final realm = Realm(config);
Expand All @@ -78,15 +97,52 @@ void main() {
realm.add(Bike(ObjectId(), 'Speeder Bike', owner: owner));
});

// :snippet-start: query-backlinks
// :snippet-start: query-backlink-to-one-relationship
// Persons have a to-one relationship with Bikes
final person = realm.query<Person>("firstName == 'Anakin'").first;

// Find all bikes that have an owner named Anakin
// Find all Bikes that have an Owner named 'Anakin'
final allBikes = person.getBacklinks<Bike>('owner');
// :snippet-end:
expect(allBikes.length, 2);
expect(allBikes.any((element) => element.id == bikeId), true);
expect(allBikes.any((element) => element.owner!.id == ownerId), true);
cleanUpRealm(realm);
});

test('Query To-Many Backlinks', () {
// Use to-many models from Schemas.dart
final config = Configuration.local(
[Scooter.schema, ScooterShop.schema, Person.schema]);
final realm = Realm(config);

final scooterId = ObjectId();
final shopId = ObjectId();

realm.write(() {
Scooter roadHog = realm.add(Scooter(ObjectId(), 'Road Hog 9000'));
Scooter scooterbug = realm.add(Scooter(scooterId, 'Scooterbug'));
Scooter bigBen = realm.add(Scooter(ObjectId(), 'Big Ben'));

realm.add(ScooterShop(ObjectId(), 'The Scoot Zone',
scooters: [roadHog, bigBen, scooterbug]));

realm.add(
ScooterShop(ObjectId(), 'Scooterz', scooters: [scooterbug, bigBen]));

realm.add(ScooterShop(shopId, 'Scoot n Shop', scooters: [roadHog]));
});
// :snippet-start: query-backlinks-to-many-relationship
// Scooters have a to-many relationship with ScooterShops
final scooters = realm.query<Scooter>("name == 'Scooterbug'").first;

// Find all ScooterShops that sell the Scooterbug
final shops = scooters.getBacklinks<ScooterShop>('scooters');

// :snippet-end:
expect(shops.length, 2);
expect(scooters.id, scooterId);
expect(shops.any((element) => element.id == shopId), false);
cleanUpRealm(realm);
});
}
2 changes: 1 addition & 1 deletion examples/dart/test/schemas.dart
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class _ScooterShop {
late ObjectId id;

late String name;
late List<_Scooter> owner;
late List<_Scooter> scooters;
}
// :snippet-end:

Expand Down
12 changes: 6 additions & 6 deletions examples/dart/test/schemas.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Filter tasks through the User's backlink property
// Filter Tasks through the User's backlink property
// using `@links.<ObjectType>.<PropertyName>` syntax
final jarjarsIncompleteTasks = realm.query<Task>(
"ALL @links.User.tasks.username == 'jarjar_binks' AND isComplete == false");

final tasksForHan = realm.query<Task>("ALL @links.User.tasks.username == 'han'");
final tasksForHan =
realm.query<Task>("ALL @links.User.tasks.username == 'han'");
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Tasks have an inverse relationship to Users
final inCompleteTasks = realm.query<Task>("isComplete == false");

// Find all Users who have an incomplete Task
for (final task in inCompleteTasks) {
final ownersWithIncompleteTasks = task.getBacklinks<User>('tasks');
for (final user in ownersWithIncompleteTasks) {
print("User ${user.username} has incomplete tasks.");
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Persons have a to-one relationship with Bikes
final person = realm.query<Person>("firstName == 'Anakin'").first;

// Find all bikes that have an owner named Anakin
// Find all Bikes that have an Owner named 'Anakin'
final allBikes = person.getBacklinks<Bike>('owner');
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// Scooters have a to-many relationship with ScooterShops
final scooters = realm.query<Scooter>("name == 'Scooterbug'").first;

// Find all ScooterShops that sell the Scooterbug
final shops = scooters.getBacklinks<ScooterShop>('scooters');

Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ class _ScooterShop {
late ObjectId id;

late String name;
late List<_Scooter> owner;
late List<_Scooter> scooters;
}
14 changes: 7 additions & 7 deletions source/sdk/flutter/crud/read.txt
Original file line number Diff line number Diff line change
Expand Up @@ -152,21 +152,21 @@ Retrieve a collection of all objects of a data model in the database with the

.. _flutter-get-backlinks:

Query Linked Objects
~~~~~~~~~~~~~~~~~~~~
Query Related Objects
~~~~~~~~~~~~~~~~~~~~~

.. versionadded:: 1.9.0

You can find all objects with an inverse relationship to a given object by calling the
object's :flutter-sdk:`getBacklinks() <realm/RealmObjectBase/getBacklinks.html>` method.
If your data model includes objects that reference other objects, you can query the :ref:`relationship <flutter-client-relationships>` using the :flutter-sdk:`getBacklinks() <realm/RealmObjectBase/getBacklinks.html>` method.

In this example, we have a ``Bike`` object model with an ``owner`` property of type ``Person``:
This method returns a :flutter-sdk:`results collection <realm/RealmResults-class.html>` of all objects that link to the given object through a to-one, to-many, or inverse relationship.

In this example, we have a ``Bike`` object model with a to-one relationship defined through an ``owner`` property of type ``Person``:

.. literalinclude:: /examples/generated/flutter/schemas.snippet.many-to-one-models.dart
:language: dart

We can use the ``getBacklinks()`` method to find any linked objects. This
method returns a :flutter-sdk:`results collection <realm/RealmResults-class.html>` of ``Bike``
We can use the ``getBacklinks()`` method to find any ``Bike``
objects that link to the specified person:

.. literalinclude:: /examples/generated/flutter/backlinks_test.snippet.query-backlinks.dart
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ Embedded objects are similar to relationships, but provide additional constraint
For more information about these constraints and how to create embedded objects,
refer to the :ref:`Embedded Objects data type documentation <flutter-embedded-objects>`.

.. tip:: Querying Related Objects

In Flutter v1.9.0 and later, you can use the :flutter-sdk:`getBacklinks() <realm/RealmObjectBase/getBacklinks.html>` method to find objects that link to another object through a relationship.
For more information, refer to :ref:`flutter-get-backlinks`.

.. _flutter-many-to-one-relationship:

To-One Relationship
Expand Down

0 comments on commit 6eba3af

Please sign in to comment.