Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

is there any apply method ? #71

Open
maxired opened this issue Feb 2, 2022 · 8 comments
Open

is there any apply method ? #71

maxired opened this issue Feb 2, 2022 · 8 comments

Comments

@maxired
Copy link

maxired commented Feb 2, 2022

Hi,

I was not able to find any apply method which would allow us to take a diff, and apply it to an object.
Is there one somewhere? Maybe in another repo?

As a user, I want to be able to compute a diff, send it to a remote host, and apply it (reverse it?), to compute the whole object.
Or should I rather use libraries based on jsonpatch ?

@jorgebodega
Copy link

That will be really great to have!

@mattphillips
Copy link
Owner

Hi @maxired this library doesn't currently support anything like this. Are you able to give a concrete example of what you would like to happen?

I've never used jsonpatch but I would imagine to compute an original object from a diff you would need some sort of metadata to describe the changes (which isn't something currently supported by deep-object-diff).

@jorgebodega
Copy link

Maybe I can give you an use case.

I have a functionality where the users must fulfill a multistep form, stored like JSON object in database, and must support versioning, because users can fulfill that as many times as they want.

Will be great if I could store just the diffs, and apply them to the schema.

Right now I'm busy and couldn't provide an example with code, I would do it later

@maxired
Copy link
Author

maxired commented Feb 16, 2022

Hi @maxired this library doesn't currently support anything like this. Are you able to give a concrete example of what you would like to happen?

Hi @mattphillips, thanks for your answer.
I am already using your library in my current project.
We use it server-side to do some diff of our redux store, and then update our DB based on diff computed by deep-object-diff. To make it clear, we don't store the diff in that case, but use it to know request to do.

But we have this other user use case where we are reevaluating our options.
Since we are already using deep-object-diff, we decided to check if deep-object-diff would fit our needs.
We have a real-time server, dealing with multiple web clients.
The clients and the server are sharing replicated shared states.

The goal of the server is to merge the different changes and propagate them to other clients.
It also has its own business, so it's not only a matter of sharing data sent by other clients.

Anyway, at each interval, the server computes a new state and needs to propagate it to the other clients.
To reduce bandwidth and client CPU, we would like to be able to compute a diff between the new computed states and the previous state from a client, and then only send that diff. Upon reception, the client would apply that diff, to locally compute its new state.
That's where we imagine using a diff and apply solution.

I've never used jsonpatch but I would imagine to compute an original object from a diff you would need some sort of metadata to describe the changes (which isn't something currently supported by deep-object-diff).

TBH, I don't know much about JSON Patch either, but I am not sure it really creates more data than detailedDiff is doing.

here is the output corresponding to the diff of detailedDiff

[
    {
        "op": "replace",
        "path": "/buzz",
        "value": "fizz"
    },
    {
        "op": "remove",
        "path": "/foo/bar/e"
    },
    {
        "op": "add",
        "path": "/foo/bar/c/2",
        "value": "z"
    },
    {
        "op": "remove",
        "path": "/foo/bar/a/1"
    },
    {
        "op": "add",
        "path": "/foo/bar/d",
        "value": "Hello, world!"
    }
]

we can see it uses an array. Not sure if that's important to keep the order of operation.

PS: we are not dealing with conflict between our clients, and so don't need CRDT or similar solutions

@lmaccherone
Copy link

lmaccherone commented May 16, 2023

Here is applyDiff module that works with the diff() method not the detailedDiff() method.

It passes all of the deep-object-diff tests as you can see here.

If you want, I can publish it as it's own npm package and/or create a pull request to upgrade deep-object-diff to include this code and test.

@lmaccherone
Copy link

I ended up publishing this as its own npm package: https://www.npmjs.com/package/@transformation-dev/deep-object-diff-apply

@gregfenton
Copy link

@lmaccherone Looks like your master branch on GH hasn't yet merged in the deep-object-diff-apply package?

@gregfenton
Copy link

gregfenton commented Aug 15, 2023

Use case is pretty straight-forward: Using a document-store database (think MongoDB or Firestore). A "document" is simply a JS object (or JSON object). When an update is being done to the document, I want to calculate the diff and store the diff in some type of history or audit collection (table). I can then use those deltas over time to identify who changed what in that object. The document in the main collection is the "current state" of the data, and the diffs in the audit table are the changes to the data over time (in reverse order).

A storage-level "version control" for business objects!! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants