Allow clients to specify/override local id generator for new objects #26
Description
Background
When creating new object in sync result function returns auto generated id that may be used to reference and obtain that specific object in the future. Once this object is synced actual value will be replaced with the server side id. Fact that object id is auto generated and that we replacing it later is causing a lots of issues and making entire sync really hard to understand.
Proposition
As solution sync users may just generate identifiers themselves (or use already existing fields that they know they are unique. Then sync framework should have option to specify function or field that will be used as id. Thanks to that users can use this ID's in other objects without worrying about swapping id or holding localy generated identifiers as separate fields.
How this will work
var obj = {
id:"1234325234",
name:"Wojtek"
}
var idGenerator = function(object){
return object.id;
};
// Suggestion to override method
sync.manage("test",{idGenerator: idGenerator},{},{});
// We can reference ID at any time and assign that to different dataset.
var ref={ testRef:object.id };
// We save object and do not care about what's being returned
sync.doSave("test", obj);
// Saving reference that has object ID.
sync.doSave("testReference", ref);
// This should work not matter if we are in sync with server or offfline
sync.doRead("test", ref.testRef);
Additional requirements
In order for this to fully work we will also need to provide custom data handler that
will use the same obj.id as id instead of the generated mongo ObjectId.
I see this as the best pattern for the moment to work with the sync.
This will ensure that ID stays the same no matter if user is offline/online, data was synced etc.