File tree Expand file tree Collapse file tree 6 files changed +65
-5
lines changed Expand file tree Collapse file tree 6 files changed +65
-5
lines changed Original file line number Diff line number Diff line change @@ -33,6 +33,11 @@ void main() {
33
33
34
34
expect (entity (model, 'D' ).flags, equals (OBXEntityFlags .SYNC_ENABLED ));
35
35
expect (entity (jsonModel, 'D' ).flags, equals (OBXEntityFlags .SYNC_ENABLED ));
36
+
37
+ expect (entity (model, 'E' ).flags,
38
+ equals (OBXEntityFlags .SYNC_ENABLED | OBXEntityFlags .SHARED_GLOBAL_IDS ));
39
+ expect (entity (jsonModel, 'E' ).flags,
40
+ equals (OBXEntityFlags .SYNC_ENABLED | OBXEntityFlags .SHARED_GLOBAL_IDS ));
36
41
});
37
42
38
43
test ('types' , () {
Original file line number Diff line number Diff line change @@ -28,6 +28,15 @@ class D {
28
28
D ();
29
29
}
30
30
31
+ @Entity ()
32
+ @Sync (sharedGlobalIds: true )
33
+ class E {
34
+ @Id (assignable: true )
35
+ int id = 0 ;
36
+
37
+ E ({this .id = 0 });
38
+ }
39
+
31
40
@Entity ()
32
41
class T {
33
42
int ? id;
Original file line number Diff line number Diff line change @@ -68,10 +68,13 @@ class EntityResolver extends Builder {
68
68
null ,
69
69
uidRequest: ! entityUid.isNull && entityUid.intValue == 0 );
70
70
71
- // Sync: check if enabled
72
- if ( _syncChecker.hasAnnotationOfExact (classElement) ) {
71
+ // Sync: check if enabled and options
72
+ _syncChecker.runIfMatches (classElement, (annotation ) {
73
73
entity.flags | = OBXEntityFlags .SYNC_ENABLED ;
74
- }
74
+ if (annotation.getField ('sharedGlobalIds' )! .toBoolValue ()! ) {
75
+ entity.flags | = OBXEntityFlags .SHARED_GLOBAL_IDS ;
76
+ }
77
+ });
75
78
76
79
log.info (entity);
77
80
Original file line number Diff line number Diff line change @@ -352,6 +352,31 @@ void main() {
352
352
expect (vectorProperty.hnswParams! .reparationBacklinkProbability, 0.95 );
353
353
expect (vectorProperty.hnswParams! .vectorCacheHintSizeKB, 2097152 );
354
354
});
355
+
356
+ test ('Sync annotation with shared global IDs' , () async {
357
+ final source = r'''
358
+ library example;
359
+ import 'package:objectbox/objectbox.dart';
360
+
361
+ @Entity()
362
+ @Sync(sharedGlobalIds: true)
363
+ class Example {
364
+ @Id(assignable: true)
365
+ int id = 0;
366
+ }
367
+ ''' ;
368
+
369
+ final testEnv = GeneratorTestEnv ();
370
+ await testEnv.run (source);
371
+
372
+ // Assert final model created by generator
373
+ var entity = testEnv.model.entities[0 ];
374
+ expect (entity.flags & OBXEntityFlags .SYNC_ENABLED != 0 , true );
375
+ expect (entity.flags & OBXEntityFlags .SHARED_GLOBAL_IDS != 0 , true );
376
+ // Only a single property
377
+ final idProperty = testEnv.model.entities[0 ].properties[0 ];
378
+ expect (idProperty.flags & OBXPropertyFlags .ID_SELF_ASSIGNABLE != 0 , true );
379
+ });
355
380
});
356
381
}
357
382
Original file line number Diff line number Diff line change 1
1
## latest
2
2
3
+ * Sync: support option to enable [ shared global IDs] ( https://sync.objectbox.io/advanced/object-ids#shared-global-ids ) .
4
+
3
5
## 4.0.1 (2024-05-27)
4
6
5
7
* Export ` ObjectWithScore ` and ` IdWithScore ` used by the new find with score ` Query ` methods. [ #637 ] ( https://github.com/objectbox/objectbox-dart/issues/637 )
Original file line number Diff line number Diff line change @@ -588,8 +588,24 @@ class _SyncListenerGroup<StreamValueType> {
588
588
///
589
589
/// Start a client using [Sync.client()] and connect to a remote server.
590
590
class Sync {
591
- /// Create a Sync annotation, enabling synchronization for an entity.
592
- const Sync ();
591
+ /// Set to `true` to enable shared global IDs for a Sync-enabled entity class.
592
+ ///
593
+ /// By default, each Sync client has its own local ID space for Objects.
594
+ /// IDs are mapped to global IDs when syncing behind the scenes.
595
+ /// Turn this on to treat Object IDs as global and turn of ID mapping.
596
+ /// The ID of an Object will then be the same on all clients.
597
+ ///
598
+ /// When using this, it is recommended to use assignable IDs (`@Id(assignable: true)` )
599
+ /// to turn off automatically assigned IDs. Without special care, two Sync
600
+ /// clients are likely to overwrite each others Objects if IDs are assigned
601
+ /// automatically.
602
+ final bool sharedGlobalIds;
603
+
604
+ /// Enables sync for an `@Entity` class.
605
+ ///
606
+ /// Note that currently sync can not be enabled or disabled for existing entities.
607
+ /// Also synced entities can not have relations to non-synced entities.
608
+ const Sync ({this .sharedGlobalIds = false });
593
609
594
610
static final bool _syncAvailable = C .has_feature (OBXFeature .Sync );
595
611
You can’t perform that action at this time.
0 commit comments