Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,19 @@ final qt = box.query(Entity_.text.notNull())
.build();
```

### Transient fields
Fields that you *don't* wish to persist in objectbox, could be
annotated with `@Transient`.

```dart
@Entity class k {
@Transient()
String ignoreThis;

//...
}
```

Help wanted
-----------
ObjectBox for Dart is still in an early stage with limited feature set (compared to other languages).
Expand Down
6 changes: 6 additions & 0 deletions example/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ class Note {
int id;
String text;

@Transient()
int these, fields, are, ignored;

@Transient()
String thisToo;

Note({this.text}); // empty default constructor needed but it can have optional args
toString() => "Note{id: $id, text: $text}";
}
Expand Down
7 changes: 7 additions & 0 deletions generator/lib/src/entity_resolver.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class EntityResolver extends Builder {
final _annotationChecker = const TypeChecker.fromRuntime(obx.Entity);
final _propertyChecker = const TypeChecker.fromRuntime(obx.Property);
final _idChecker = const TypeChecker.fromRuntime(obx.Id);
final _transientChecker = const TypeChecker.fromRuntime(obx.Transient);

@override
FutureOr<void> build(BuildStep buildStep) async {
Expand Down Expand Up @@ -54,6 +55,12 @@ class EntityResolver extends Builder {
// read all suitable annotated properties
bool hasIdProperty = false;
for (var f in element.fields) {

if (_transientChecker.hasAnnotationOfExact(f)) {
log.info(" skipping property ${f.name} (annotated with @Transient)");
continue;
}

int fieldType, flags = 0;
int propUid;

Expand Down
4 changes: 4 additions & 0 deletions lib/src/annotations.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,7 @@ class Id {
final int uid;
const Id({this.uid});
}

class Transient {
const Transient();
}
15 changes: 14 additions & 1 deletion test/entity.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ class TestEntity {
double tDouble;
bool tBool;

@Transient()
int ignore;

@Transient()
int omit, disregard;

// explicitly declared types, see OB-C, objectbox.h

// OBXPropertyType.Byte | 1 byte
Expand Down Expand Up @@ -50,5 +56,12 @@ class TestEntity {
this.tShort,
this.tChar,
this.tInt,
this.tFloat});
this.tFloat,
this.ignore
});

TestEntity.ignoredExcept(this.tInt) {
this.omit = -1;
this.disregard = 1;
}
}
31 changes: 31 additions & 0 deletions test/query_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,37 @@ void main() {
box = env.box;
});

test("ignore transient field", () {
box.put(
TestEntity(tDouble: 0.1, ignore: 1337));

final d = TestEntity_.tDouble;

final q = box.query(d.between(0.0, 0.2)).build();

expect(q.count(), 1);
expect(q.findFirst().ignore, null);
});

test("ignore multiple transient fields", () {
final entity = TestEntity.ignoredExcept(1337);

box.put(entity);

expect(entity.omit, -1);
expect(entity.disregard, 1);

final i = TestEntity_.tInt;

final q = box.query(i.equals(1337)).build();

final result = q.findFirst();

expect(q.count(), 1);
expect(result.disregard, null);
expect(result.omit, null);
});

test(".null and .notNull", () {
box.putMany([
TestEntity(tDouble: 0.1, tBool: true),
Expand Down