Skip to content

Commit 2bb12c3

Browse files
Merge branch 'relation-docs-updates' into 'main'
Relation docs updates See merge request objectbox/objectbox-dart!87
2 parents ad5a9ab + 772c1f1 commit 2bb12c3

File tree

2 files changed

+78
-65
lines changed

2 files changed

+78
-65
lines changed

objectbox/lib/src/relations/to_many.dart

Lines changed: 39 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -8,48 +8,43 @@ import '../store.dart';
88
import '../transaction.dart';
99
import 'info.dart';
1010

11-
/// A lazily loaded `List` of target objects representing a to-many relation,
12-
/// a unidirectional link from a "source" entity to multiple objects of a
13-
/// "target" entity.
14-
///
15-
/// It tracks changes (adds and removes) that can be later applied (persisted)
16-
/// to the database. This happens either when the source entity of this relation
17-
/// is put or using [applyToDb]. For some important details about applying
18-
/// changes, see the notes about relations of [Box.put].
19-
///
20-
/// The objects are loaded lazily on first access of this list, and then cached.
21-
/// Subsequent calls to any method, like [length], do not query the database,
22-
/// even if the relation was changed elsewhere. To get the latest data [Box.get]
23-
/// the source object again.
24-
///
25-
/// You can:
26-
/// - [add] new objects to the relation.
27-
/// - [removeAt] target objects from the relation at a specific list index.
28-
/// - [remove] target objects from the relation by an ID.
11+
/// A to-many relation of an entity that references multiple objects of a "target" entity [EntityT].
2912
///
13+
/// Example:
3014
/// ```
15+
/// @Entity()
3116
/// class Student {
3217
/// final teachers = ToMany<Teacher>();
3318
/// }
19+
/// ```
3420
///
35-
/// // Example 1: create a relation
36-
/// final teacher1 = Teacher();
37-
/// final teacher2 = Teacher();
21+
/// Implements the `List` interface and uses lazy initialization.
22+
/// The target objects are only read from the database when the list is first accessed.
3823
///
39-
/// final student1 = Student();
40-
/// student1.teachers.add(teacher1);
41-
/// student1.teachers.add(teacher2);
24+
/// Tracks when target objects are added and removed. Common usage:
25+
/// - [add] target objects to the relation.
26+
/// - [remove] target objects from the relation.
27+
/// - [removeAt] target objects at a specific index.
4228
///
43-
/// final student2 = Student();
44-
/// student2.teachers.add(teacher2);
29+
/// To apply (persist) the changes to the database, call [applyToDb] or put the object with the ToMany.
30+
/// For important details, see the notes about relations of [Box.put].
4531
///
46-
/// // saves students as well as teachers in the database
47-
/// store.box<Student>().putMany([student1, student2]);
32+
/// ```
33+
/// // Example 1: add target objects to a relation
34+
/// student.teachers.add(teacher1);
35+
/// student.teachers.add(teacher2);
36+
/// store.box<Student>().put(student);
4837
///
49-
/// // Example 2: remove a relation
50-
/// student.teachers.removeAt(index)
51-
/// student.teachers.applyToDb(); // or store.box<Student>().put(student);
38+
/// // Example 2: remove a target object from the relation
39+
/// student.teachers.removeAt(index);
40+
/// student.teachers.applyToDb();
41+
/// // or store.box<Student>().put(student);
5242
/// ```
43+
///
44+
/// In the database, the target objects are referenced by their IDs, which are
45+
/// persisted as part of the relation of the object with the ToMany.
46+
///
47+
/// To get all objects with a ToMany that reference a target object, see [Backlink].
5348
class ToMany<EntityT> extends Object with ListMixin<EntityT> {
5449
/// Store-related configuration attached to this.
5550
///
@@ -92,6 +87,9 @@ class ToMany<EntityT> extends Object with ListMixin<EntityT> {
9287
_items.length = newLength;
9388
}
9489

90+
/// Gets the target object at the given index.
91+
///
92+
/// [ToMany] uses lazy initialization, so on first access this will read the target objects from the database.
9593
@override
9694
EntityT operator [](int index) => _items[index];
9795

@@ -109,6 +107,10 @@ class ToMany<EntityT> extends Object with ListMixin<EntityT> {
109107
_track(element, 1);
110108
}
111109

110+
/// Prepares to add the given target object to this relation.
111+
///
112+
/// To apply changes, call [applyToDb] or put the object with the ToMany.
113+
/// For important details, see the notes about relations of [Box.put].
112114
@override
113115
void add(EntityT element) {
114116
ArgumentError.checkNotNull(element, 'element');
@@ -121,6 +123,7 @@ class ToMany<EntityT> extends Object with ListMixin<EntityT> {
121123
}
122124
}
123125

126+
/// Like [add], but for multiple target objects.
124127
@override
125128
void addAll(Iterable<EntityT> iterable) {
126129
iterable.forEach(_track);
@@ -132,8 +135,11 @@ class ToMany<EntityT> extends Object with ListMixin<EntityT> {
132135
}
133136
}
134137

135-
// note: to override, arg must be "Object", same as in the base class.
136-
@override
138+
/// Prepares to remove the target object from this relation.
139+
///
140+
/// To apply changes, call [applyToDb] or put the object with the ToMany.
141+
/// For important details, see the notes about relations of [Box.put].
142+
@override // note: to override, arg must be "Object", same as in the base class.
137143
bool remove(Object? element) {
138144
if (!_items.remove(element)) return false;
139145
if (element != null) _track(element as EntityT, -1);

objectbox/lib/src/relations/to_one.dart

Lines changed: 39 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,48 @@
1+
import '../annotations.dart';
12
import '../box.dart';
23
import '../modelinfo/entity_definition.dart';
34
import '../native/transaction.dart';
45
import '../store.dart';
56

6-
/// Manages a to-one relation, an unidirectional link from a "source" entity to
7-
/// a "target" entity. The target object is referenced by its ID, which is
8-
/// persisted in the source object.
9-
///
10-
/// You can:
11-
/// - set [target]=null or [targetId]=0 to remove the relation.
12-
/// - set [target] to an object to set the relation.
13-
/// Call [Box<SourceEntity>.put()] to persist the changes. If the target
14-
/// object is a new one (its ID is 0), it will be also saved automatically.
15-
/// - set [targetId] to an existing object's ID to set the relation.
16-
/// Call [Box<SourceEntity>.put()] to persist the changes.
7+
/// A to-one relation of an entity that references one object of a "target" entity [EntityT].
178
///
9+
/// Example:
1810
/// ```
1911
/// @Entity()
2012
/// class Order {
2113
/// final customer = ToOne<Customer>();
22-
/// ...
2314
/// }
15+
/// ```
2416
///
25-
/// // Example 1: create a relation
26-
/// final order = Order(...);
27-
/// final customer = Customer();
28-
/// order.customer.target = customer;
17+
/// Uses lazy initialization.
18+
/// The [target] object is only read from the database when it is first accessed.
2919
///
30-
/// // Or you could create the target object in place:
31-
/// // order.customer.target = Customer()
20+
/// Common usage:
21+
/// - set the [target] object to create a relation.
22+
/// When the object with the ToOne is put, if the target object is new (its ID is 0), it will be put as well.
23+
/// Otherwise, only the target ID in the database is updated.
24+
/// - set the [targetId] of the target object to create a relation.
25+
/// - set [target] to `null` or [targetId] to `0` to remove the relation.
3226
///
33-
/// // attach() must be called when creating new instances. On objects (e.g.
34-
/// // "orders" in this example) read with box.get() its done automatically.
35-
/// order.customer.attach(store);
27+
/// Then, to persist the changes [Box.put] the object with the ToOne.
3628
///
37-
/// // saves both [customer] and [order] in the database
29+
/// ```
30+
/// // Example 1: create a relation
31+
/// order.customer.target = customer;
32+
/// // or order.customer.targetId = customerId;
3833
/// store.box<Order>().put(order);
3934
///
40-
///
41-
/// // Example 2: remove a relation
42-
///
35+
/// // Example 2: remove the relation
4336
/// order.customer.target = null
44-
/// // ... or ...
45-
/// order.customer.targetId = 0
37+
/// // or order.customer.targetId = 0
38+
/// store.box<Order>().put(order);
4639
/// ```
40+
///
41+
/// The target object is referenced by its ID.
42+
/// This [targetId] is persisted as part of the object with the ToOne in a special
43+
/// property created for each ToOne (named like "customerId").
44+
///
45+
/// To get all objects with a ToOne that reference a target object, see [Backlink].
4746
class ToOne<EntityT> {
4847
/// Store-related configuration attached to this.
4948
///
@@ -73,7 +72,9 @@ class ToOne<EntityT> {
7372
}
7473
}
7574

76-
/// Get target object. If it's the first access, this reads from DB.
75+
/// Returns the target object or `null` if there is none.
76+
///
77+
/// [ToOne] uses lazy initialization, so on first access this will read the target object from the database.
7778
EntityT? get target {
7879
if (_value._state == _ToOneState.lazy) {
7980
final configuration = _getStoreConfigOrThrow();
@@ -92,8 +93,11 @@ class ToOne<EntityT> {
9293
return _value._object;
9394
}
9495

95-
/// Set relation target object. Note: this does not store the change yet, use
96-
/// [Box.put()] on the containing (relation source) object.
96+
/// Prepares to set the target object of this relation.
97+
/// Pass `null` to remove an existing one.
98+
///
99+
/// To apply changes, put the object with the ToOne.
100+
/// For important details, see the notes about relations of [Box.put].
97101
set target(EntityT? object) {
98102
// If not attached, yet, avoid throwing and set the ID to unknown instead.
99103
// If the targetId getter is used later, it will call this to re-try
@@ -123,8 +127,11 @@ class ToOne<EntityT> {
123127
return _value._id;
124128
}
125129

126-
/// Set ID of a relation target object. Note: this does not store the change
127-
/// yet, use [Box.put()] on the containing (relation source) object.
130+
/// Prepares to set the target of this relation to the object with the given [id].
131+
/// Pass `0` to remove an existing one.
132+
///
133+
/// To apply changes, put the object with the ToOne.
134+
/// For important details, see the notes about relations of [Box.put].
128135
set targetId(int? id) {
129136
id ??= 0;
130137
if (id == 0) {

0 commit comments

Comments
 (0)