Skip to content

Commit a07b338

Browse files
Add documentation for box
1 parent 2ff0ec6 commit a07b338

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed

lib/src/box.dart

+22-3
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,11 @@ class Box<T> {
4545
}
4646
}
4747

48-
// if the respective ID property is given as null or 0, a newly assigned ID is returned, otherwise the existing ID is returned
48+
/// Puts the given Object in the box (aka persisting it). If this is a new entity (its ID property is 0), a new ID
49+
/// will be assigned to the entity (and returned). If the entity was already put in the box before, it will be
50+
/// overwritten.
51+
///
52+
/// Performance note: if you want to put several entities, consider [putMany] instead.
4953
int put(T object, {_PutMode mode = _PutMode.Put}) {
5054
var propVals = _entityReader(object);
5155
if (propVals[_modelEntity.idPropName] == null || propVals[_modelEntity.idPropName] == 0) {
@@ -67,7 +71,8 @@ class Box<T> {
6771
return propVals[_modelEntity.idPropName];
6872
}
6973

70-
// only instances whose ID property ot null or 0 will be given a new, valid number for that. A list of the final IDs is returned
74+
/// Puts the given [objects] into this Box in a single transaction. Returns a list of all IDs of the inserted
75+
/// Objects.
7176
List<int> putMany(List<T> objects, {_PutMode mode = _PutMode.Put}) {
7277
if (objects.isEmpty) return [];
7378

@@ -129,6 +134,7 @@ class Box<T> {
129134
return allPropVals.map((p) => p[_modelEntity.idPropName] as int).toList();
130135
}
131136

137+
/// Retrieves the stored object with the ID [id] from this box's database. Returns null if not found.
132138
get(int id) {
133139
final dataPtrPtr = allocate<Pointer<Uint8>>();
134140
final sizePtr = allocate<IntPtr>();
@@ -163,7 +169,8 @@ class Box<T> {
163169
});
164170
}
165171

166-
// returns list of ids.length objects of type T, each corresponding to the location of its ID in the ids array. Non-existant IDs become null
172+
/// Returns a list of [ids.length] Objects of type T, each corresponding to the location of its ID in [ids].
173+
/// Non-existant IDs become null.
167174
List<T> getMany(List<int> ids) {
168175
if (ids.isEmpty) return [];
169176

@@ -174,14 +181,18 @@ class Box<T> {
174181
() => checkObxPtr(bindings.obx_box_get_many(_cBox, ptr), "failed to get many objects from box")));
175182
}
176183

184+
/// Returns all stored objects in this Box.
177185
List<T> getAll() {
178186
const bool allowMissing = false; // throw if null is encountered in the data found
179187
return _getMany(
180188
allowMissing, () => checkObxPtr(bindings.obx_box_get_all(_cBox), "failed to get all objects from box"));
181189
}
182190

191+
/// Returns a builder to create queries for Object matching supplied criteria.
183192
QueryBuilder query(Condition qc) => QueryBuilder<T>(_store, _fbManager, _modelEntity.id.id, qc);
184193

194+
/// Returns the count of all stored Objects in this box or, if [limit] is not zero, the given [limit], whichever
195+
/// is lower.
185196
int count({int limit = 0}) {
186197
Pointer<Uint64> count = allocate<Uint64>();
187198
try {
@@ -192,6 +203,7 @@ class Box<T> {
192203
}
193204
}
194205

206+
/// Returns true if no objects are in this box.
195207
bool isEmpty() {
196208
Pointer<Uint8> isEmpty = allocate<Uint8>();
197209
try {
@@ -202,6 +214,7 @@ class Box<T> {
202214
}
203215
}
204216

217+
/// Returns true if this box contains an Object with the ID [id].
205218
bool contains(int id) {
206219
Pointer<Uint8> contains = allocate<Uint8>();
207220
try {
@@ -212,6 +225,7 @@ class Box<T> {
212225
}
213226
}
214227

228+
/// Returns true if this box contains objects with all of the given [ids] using a single transaction.
215229
bool containsMany(List<int> ids) {
216230
Pointer<Uint8> contains = allocate<Uint8>();
217231
try {
@@ -224,13 +238,16 @@ class Box<T> {
224238
}
225239
}
226240

241+
/// Removes (deletes) the Object with the ID [id]. Returns true if an entity was actually removed and false if no
242+
/// entity exists with the given ID.
227243
bool remove(int id) {
228244
final err = bindings.obx_box_remove(_cBox, id);
229245
if (err == OBXError.OBX_NOT_FOUND) return false;
230246
checkObx(err); // throws on other errors
231247
return true;
232248
}
233249

250+
/// Removes (deletes) Objects by their ID in a single transaction. Returns a list of IDs of all removed Objects.
234251
int removeMany(List<int> ids) {
235252
Pointer<Uint64> removedIds = allocate<Uint64>();
236253
try {
@@ -243,6 +260,7 @@ class Box<T> {
243260
}
244261
}
245262

263+
/// Removes (deletes) ALL Objects in a single transaction.
246264
int removeAll() {
247265
Pointer<Uint64> removedItems = allocate<Uint64>();
248266
try {
@@ -253,5 +271,6 @@ class Box<T> {
253271
}
254272
}
255273

274+
/// The low-level pointer to this box.
256275
get ptr => _cBox;
257276
}

lib/src/store.dart

+2-2
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,9 @@ class Store {
5353
return _entityDefinitions[T];
5454
}
5555

56-
/// Executes a given function inside a transaction
56+
/// Executes a given function inside a transaction.
5757
///
58-
/// Returns type of [fn] if [return] is called in [fn]
58+
/// Returns type of [fn] if [return] is called in [fn].
5959
R runInTransaction<R>(TxMode mode, R Function() fn) {
6060
bool write = mode == TxMode.Write;
6161
Pointer<Void> txn = write ? bindings.obx_txn_write(_cStore) : bindings.obx_txn_read(_cStore);

0 commit comments

Comments
 (0)