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() {
3333
3434 expect (entity (model, 'D' ).flags, equals (OBXEntityFlags .SYNC_ENABLED ));
3535 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 ));
3641 });
3742
3843 test ('types' , () {
Original file line number Diff line number Diff line change @@ -28,6 +28,15 @@ class D {
2828 D ();
2929}
3030
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+
3140@Entity ()
3241class T {
3342 int ? id;
Original file line number Diff line number Diff line change @@ -68,10 +68,13 @@ class EntityResolver extends Builder {
6868 null ,
6969 uidRequest: ! entityUid.isNull && entityUid.intValue == 0 );
7070
71- // Sync: check if enabled
72- if ( _syncChecker.hasAnnotationOfExact (classElement) ) {
71+ // Sync: check if enabled and options
72+ _syncChecker.runIfMatches (classElement, (annotation ) {
7373 entity.flags | = OBXEntityFlags .SYNC_ENABLED ;
74- }
74+ if (annotation.getField ('sharedGlobalIds' )! .toBoolValue ()! ) {
75+ entity.flags | = OBXEntityFlags .SHARED_GLOBAL_IDS ;
76+ }
77+ });
7578
7679 log.info (entity);
7780
Original file line number Diff line number Diff line change @@ -352,6 +352,31 @@ void main() {
352352 expect (vectorProperty.hnswParams! .reparationBacklinkProbability, 0.95 );
353353 expect (vectorProperty.hnswParams! .vectorCacheHintSizeKB, 2097152 );
354354 });
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+ });
355380 });
356381}
357382
Original file line number Diff line number Diff line change 11## latest
22
3+ * Sync: support option to enable [ shared global IDs] ( https://sync.objectbox.io/advanced/object-ids#shared-global-ids ) .
4+
35## 4.0.1 (2024-05-27)
46
57* 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> {
588588///
589589/// Start a client using [Sync.client()] and connect to a remote server.
590590class 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 });
593609
594610 static final bool _syncAvailable = C .has_feature (OBXFeature .Sync );
595611
You can’t perform that action at this time.
0 commit comments