json-patch / json-patch2

A possibile revision to the JSON-Patch format
44 stars 0 forks source link

Patching entities: Temporary and permanent IDs #20

Open HappyNomad opened 7 years ago

HappyNomad commented 7 years ago

I use JSON Patch for persistent object models where collections are unordered sets rather than lists. It's a common scenario, so let's standardize this specialized behavior. I want to help craft an "Entity JSON Patch" RFC. It would be based on the JSON Patch RFC but with key differences. Collections have no order and are instead indexed by ID.

I described how to use permanent IDs in paths at https://github.com/json-patch/json-patch2/issues/12#issuecomment-328221082. As for add or replace operations, the value can be a permanent ID. The applied new value is the existing object indicated by that ID.

In issue #5, @briancavalier "raises the problem of server-assigned ids: how can a client submit a patch to add an item where the object key is supposed to be an id that needs to be assigned by the server?" For add or replace operations that create a new value, the value is of the form "MyNamespace.MyType/$1". This consists of the type of object to create, followed by a / separator, followed by the temporary ID. The temporary ID consists of a "$" followed by a number that's incremented for each new temporary ID. The temporary ID is thereafter used to reference the new object throughout the scope of the patch document transaction. it can be used in the same ways that permanent IDs can be used.

HappyNomad commented 6 years ago

My implementation of these concepts is steadily progressing. I've settled on prefixing permanent IDs with @ for children and ^ for references, temporary IDs with #, and writing new values as temp ID followed by type. Examples are as follows:

HappyNomad commented 5 years ago

Regarding my progress, I have client-to-server sync working. The client tracks changes and packages them into an "Entity JSON Patch" document, that the server then translates into MongoDB's update syntax. I'm now testing peer-to-peer sync. It involves patching in-memory objects rather than the database. In the process of implementation, I've further clarified the syntax described above:

HappyNomad commented 5 years ago

My framework has a website: https://chainreactive.org/