Skip to content

Commit

Permalink
Update Read page example
Browse files Browse the repository at this point in the history
  • Loading branch information
cbullinger committed Mar 18, 2024
1 parent f3abc18 commit bc24393
Show file tree
Hide file tree
Showing 5 changed files with 254 additions and 59 deletions.
127 changes: 74 additions & 53 deletions examples/dart/test/read_write_data_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import 'package:test/test.dart';
import 'package:realm_dart/realm.dart';
import 'utils.dart';

part 'read_write_data_test.g.dart';
part 'read_write_data_test.realm.dart';

// :snippet-start: models
@RealmModel()
Expand All @@ -23,7 +23,7 @@ class _Team {

late String name;
late List<_Person> crew;
late Map<String, RealmValue> log;
late RealmValue eventLog;
}
// :snippet-end:

Expand Down Expand Up @@ -174,66 +174,87 @@ void main() {
final config = Configuration.local([Person.schema, Team.schema]);
final realm = Realm(config);

// :snippet-start: filter-nested-collections
realm.write(() {
realm.addAll([
(Team(ObjectId(), 'Death Star Janitorial Staff', log: {
'1': RealmValue.from({
'date': RealmValue.from(DateTime.utc(5622, 8, 18, 12, 30, 0)),
'type': RealmValue.from(['maintenance', 'work_order']),
'security_clearance_needed': RealmValue.from(true),
'summary': RealmValue.from('leaking pipes in control room'),
'priority': RealmValue.from('high'),
'recurring': RealmValue.from(true)
}),
'2': RealmValue.from({
'date': RealmValue.from(DateTime.utc(5622, 9, 18, 12, 30, 0)),
'type': RealmValue.from('maintenance'),
'summary': RealmValue.from('trash compactor jammed. again.'),
'priority': RealmValue.from('low'),
'recurring': RealmValue.from(true),
'comments': RealmValue.from('this is the 5th time!')
})
})),
(Team(ObjectId(), 'Death Star IT', log: {
'1': RealmValue.from({
'date': RealmValue.from(DateTime.utc(5622, 9, 20, 12, 30, 0)),
'type': RealmValue.from(['hardware', 'repair']),
'security_clearance_needed': RealmValue.from(true),
'summary':
RealmValue.from('server racks damaged in last attack '),
'priority': RealmValue.from('critical'),
'recurring': RealmValue.from(false)
})
}))
(Team(ObjectId(), 'Janitorial Staff',
eventLog: RealmValue.from({
'1': {
'date': DateTime.utc(5622, 8, 18, 12, 30, 0),
'type': ['work_order', 'maintenance'],
'summary': 'leaking pipes in control room',
'priority': 'high',
},
'2': {
'date': DateTime.utc(5622, 9, 18, 12, 30, 0),
'type': ['maintenance'],
'summary': 'trash compactor jammed',
'priority': 'low',
'comment': 'this is the second time this week'
}
}))),
(Team(ObjectId(), 'IT',
eventLog: RealmValue.from({
'1': {
'date': DateTime.utc(5622, 9, 20, 12, 30, 0),
'type': ['hardware', 'repair'],
'summary': 'lightsaber damage to server room',
'priority': 'high',
}
})))
]);

// TODO: Add query examples
final teams = realm.all<Team>();
// Use [bracket notation] to query values at the specified path
final teamsWithHighPriorityEvents =
teams.query("eventLog[*].priority == 'high'");
print(teamsWithHighPriorityEvents.length); // prints `2`

cleanUpRealm(realm);
final teamsWithMaintenanceEvents =
teams.query("eventLog[*].type[FIRST] == 'maintenance'");
print(teamsWithMaintenanceEvents.length); // prints `1`

// Use @keys to query map keys
final eventsWithComments =
teams.query("eventLog[*].@keys == 'comment'");
print(eventsWithComments.length); // prints `1`

// Use @type to query the collection type
final teamsWithEventsAsLists =
teams.query("eventLog[*].type.@type == 'list'");
print(teamsWithEventsAsLists.length); // prints `2`
// :remove-start:
expect(teamsWithHighPriorityEvents.length, 2);
expect(teamsWithMaintenanceEvents.length, 1);
expect(eventsWithComments.length, 1);
expect(teamsWithEventsAsLists.length, 2);
// :remove-end:
});
// :snippet-end:
cleanUpRealm(realm);
});
test('Limit Results', () {
final config = Configuration.local([Person.schema, Team.schema]);
final realm = Realm(config);
// :snippet-start: limit
realm.write(() {
realm.addAll([
Person(ObjectId(), 'Luke'),
Person(ObjectId(), 'Luke'),
Person(ObjectId(), 'Luke'),
Person(ObjectId(), 'Luke')
]);
});
test('Limit Results', () {
final config = Configuration.local([Person.schema, Team.schema]);
final realm = Realm(config);
// :snippet-start: limit
realm.write(() {
realm.addAll([
Person(ObjectId(), 'Luke'),
Person(ObjectId(), 'Luke'),
Person(ObjectId(), 'Luke'),
Person(ObjectId(), 'Luke')
]);
});

final limitedPeopleResults =
realm.query<Person>('name == \$0 LIMIT(2)', ['Luke']);
final limitedPeopleResults =
realm.query<Person>('name == \$0 LIMIT(2)', ['Luke']);

// prints `2`
print(limitedPeopleResults.length);
// :snippet-end:
expect(limitedPeopleResults.length, 2);
// prints `2`
print(limitedPeopleResults.length);
// :snippet-end:
expect(limitedPeopleResults.length, 2);

cleanUpRealm(realm);
});
cleanUpRealm(realm);
});
});
test('Return from write block', () {
Expand Down
120 changes: 120 additions & 0 deletions examples/dart/test/read_write_data_test.realm.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
@@ -0,0 +1,49 @@
realm.write(() {
realm.addAll([
(Team(ObjectId(), 'Janitorial Staff',
eventLog: RealmValue.from({
'1': {
'date': DateTime.utc(5622, 8, 18, 12, 30, 0),
'type': ['work_order', 'maintenance'],
'summary': 'leaking pipes in control room',
'priority': 'high',
},
'2': {
'date': DateTime.utc(5622, 9, 18, 12, 30, 0),
'type': ['maintenance'],
'summary': 'trash compactor jammed',
'priority': 'low',
'comment': 'this is the second time this week'
}
}))),
(Team(ObjectId(), 'IT',
eventLog: RealmValue.from({
'1': {
'date': DateTime.utc(5622, 9, 20, 12, 30, 0),
'type': ['hardware', 'repair'],
'summary': 'lightsaber damage to server room',
'priority': 'high',
}
})))
]);

final teams = realm.all<Team>();
// Use [bracket notation] to query values at the specified path
final teamsWithHighPriorityEvents =
teams.query("eventLog[*].priority == 'high'");
print(teamsWithHighPriorityEvents.length); // prints `2`

final teamsWithMaintenanceEvents =
teams.query("eventLog[*].type[FIRST] == 'maintenance'");
print(teamsWithMaintenanceEvents.length); // prints `1`

// Use @keys to query map keys
final eventsWithComments =
teams.query("eventLog[*].@keys == 'comment'");
print(eventsWithComments.length); // prints `1`

// Use @type to query the collection type
final teamsWithEventsAsLists =
teams.query("eventLog[*].type.@type == 'list'");
print(teamsWithEventsAsLists.length); // prints `2`
});
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ class _Team {

late String name;
late List<_Person> crew;
late Map<String, RealmValue> log;
late RealmValue eventLog;
}
15 changes: 10 additions & 5 deletions source/sdk/flutter/crud/read.txt
Original file line number Diff line number Diff line change
Expand Up @@ -208,14 +208,19 @@ or primitives.

Query Nested Collections of Mixed Data
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

.. versionadded:: 2.0.0

In Flutter SDK v2.0.0 and later, :ref:`RealmValue <flutter-realm-value>` properties can contain collections (a list or
map) of mixed data. These collections can be nested within collections
and can contain other collections of mixed data. You can query these
the same way you would a normal collection.
In Flutter SDK v2.0.0 and later, :ref:`RealmValue <flutter-realm-value>`
properties can contain collections (a list or map) of mixed data. These
collections can be nested within collections and can contain other
collections of mixed data. You can query these with dot notation, the
same way you would a normal collection. You can also use bracket notation to query a specific element in the collection (for example, ``[FIRST]``, ``[1]``, ``[LAST]``, or ``[*]``).

.. literalinclude:: /examples/generated/flutter/read_write_data_test.snippet.filter-nested-collections.dart
:language: dart

.. TODO: Add query examples as part of RQL ticket
.. TODO Link to RQL documentation when available

.. _flutter-as-results:

Expand Down

0 comments on commit bc24393

Please sign in to comment.