Skip to content

Commit 6ab66b4

Browse files
ToOne/ToMany: improve class descriptions, notable things first
1 parent ad5a9ab commit 6ab66b4

File tree

2 files changed

+52
-58
lines changed

2 files changed

+52
-58
lines changed

objectbox/lib/src/relations/to_many.dart

Lines changed: 26 additions & 31 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
///

objectbox/lib/src/relations/to_one.dart

Lines changed: 26 additions & 27 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
///

0 commit comments

Comments
 (0)