diff --git a/README.md b/README.md index 071e60e63..deb8ced68 100644 --- a/README.md +++ b/README.md @@ -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). diff --git a/example/README.md b/example/README.md index 492c51821..0df7ac63e 100644 --- a/example/README.md +++ b/example/README.md @@ -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}"; } diff --git a/generator/lib/src/entity_resolver.dart b/generator/lib/src/entity_resolver.dart index 1dec3299e..58c61f2e7 100644 --- a/generator/lib/src/entity_resolver.dart +++ b/generator/lib/src/entity_resolver.dart @@ -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 build(BuildStep buildStep) async { @@ -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; diff --git a/lib/src/annotations.dart b/lib/src/annotations.dart index 4b0fab408..aff1fd12b 100644 --- a/lib/src/annotations.dart +++ b/lib/src/annotations.dart @@ -20,3 +20,7 @@ class Id { final int uid; const Id({this.uid}); } + +class Transient { + const Transient(); +} diff --git a/test/entity.dart b/test/entity.dart index b88c1f7a2..2533346db 100644 --- a/test/entity.dart +++ b/test/entity.dart @@ -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 @@ -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; + } } diff --git a/test/query_test.dart b/test/query_test.dart index 79e309d15..04d433f1e 100644 --- a/test/query_test.dart +++ b/test/query_test.dart @@ -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),