SyncCollectionConfig<T extends dynamic> class
Configuration for a single collection/model to be synced between Realm and MongoDB.
This class defines how a Realm collection should sync with the server, including serialization, conflict resolution, and custom processing logic.
Minimum Required Configuration (4 fields)
SyncCollectionConfig<ChatMessage>(
results: realm.all<ChatMessage>(), // Required: What to sync
collectionName: 'chat_messages', // Required: MongoDB collection name
idSelector: (obj) => obj.id, // Required: How to get object ID
needsSync: (obj) => obj.syncUpdateDb, // Required: When to sync
)
Automatic Behaviors (Zero Configuration Needed)
- Serialization: Automatically uses generated
toEJson()from.realm.dartfiles - Nested Objects: All relationships (to-one, to-many, embedded) serialize recursively
- Conflict Resolution: Last-write-wins based on
sync_updated_attimestamps - Sanitization: Removes internal 'sync_update_db' field before sending to server
- Flag Management: Automatically clears
syncUpdateDbflag after successful sync
Optional Customization
Custom Serialization
toSyncMap: (obj) => {'_id': obj.id, 'custom': obj.computed},
fromServerMap: (map) => MyModel(map['_id'], custom: map['custom']),
Pre-Processing Hook (NEW!)
emitPreProcessor: (rawJson) {
rawJson['clientVersion'] = '1.0.0';
rawJson['deviceId'] = DeviceInfo.id;
return rawJson;
},
Lifecycle Callbacks
applyAckSuccess: (obj) => print('Synced: ${obj.id}'),
applyNoDiff: (obj) => print('No changes: ${obj.id}'),
Custom Sanitization
sanitize: (map) {
map.remove('localOnlyField');
return map;
},
Schema Hints (Rarely Needed)
Only required if auto-detection fails or for advanced nested object scenarios:
propertyNames: ['id', 'name', 'timestamp', 'sync_updated_at'],
embeddedProperties: {'user': ['id', 'name']},
embeddedCreators: {'user': (map) => User(map['id'], map['name'])},
create: () => MyModel('', '', DateTime.now()),
Constructors
-
SyncCollectionConfig({required dynamic results, required String collectionName, required String idSelector(dynamic model), required bool needsSync(dynamic model), Map<
String, dynamic> toSyncMap(dynamic model)?, dynamic fromServerMap(Map<String, dynamic> serverMap)?, List<String> ? propertyNames, Map<String, List< ? embeddedProperties, Map<String> >String, dynamic Function(Map< ? embeddedCreators, T create()?, void applyAckSuccess(dynamic model)?, void applyNoDiff(dynamic model)?, Map<String, dynamic> )>String, dynamic> sanitize(Map<String, dynamic> )?, Map<String, dynamic> emitPreProcessor(Map<String, dynamic> rawJson)?, dynamic decode(Map<String, dynamic> data)?})
Properties
- applyAckSuccess → void Function(dynamic model)?
-
Optional callback invoked after successful sync acknowledgment from server.
final
- applyNoDiff → void Function(dynamic model)?
-
Optional callback when no changes detected (diff was empty).
final
- collectionName → String
-
MongoDB collection name where data will be synced.
final
- create → T Function()?
-
Optional factory function to create empty model instances.
final
-
decode
→ dynamic Function(Map<
String, dynamic> data)? -
Internal decoder function that preserves generic type T.
final
-
embeddedCreators
→ Map<
String, dynamic Function(Map< ?String, dynamic> )> -
Optional factory functions for creating embedded/nested objects.
final
-
embeddedProperties
→ Map<
String, List< ?String> > -
Optional nested object property mapping for custom serialization.
final
-
emitPreProcessor
→ Map<
String, dynamic> Function(Map<String, dynamic> rawJson)? -
Optional pre-processor to modify payload before emitting to server.
final
-
fromServerMap
→ dynamic Function(Map<
String, dynamic> serverMap)? -
Optional custom function to deserialize server data into Realm objects.
final
- hashCode → int
-
The hash code for this object.
no setterinherited
- idSelector → String Function(dynamic model)
-
Function to extract the unique identifier from a model instance.
final
- needsSync → bool Function(dynamic model)
-
Predicate function to determine if an object needs syncing.
final
-
propertyNames
→ List<
String> ? -
Optional list of property names for schema introspection fallback.
final
- results → dynamic
-
The Realm query results to sync. Changes to these objects trigger sync operations.
final
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
-
sanitize
→ Map<
String, dynamic> Function(Map<String, dynamic> )? -
Optional function to sanitize data before sending to server.
final
-
toSyncMap
→ Map<
String, dynamic> Function(dynamic model)? -
Optional custom function to serialize objects before sending to server.
final
Methods
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited