chbrown / rfc6902

Complete implementation of RFC6902 in TypeScript
https://chbrown.github.io/rfc6902/
322 stars 39 forks source link

Question: How to ignore id fields if possible. #14

Closed crh225 closed 7 years ago

crh225 commented 7 years ago

lets say we have input:

{
  "name": "Christopher Brown",
  "repositories": [
{"id":1,"text":"test"},
{"id":2,"text":"test"},
{"id":3,"text":"test"}
  ]
}

and output:

{
  "name": "Christopher Brown",
  "repositories": [
{"id":3,"text":"test3"},
{"id":4,"text":"test4"},
{"id":1,"text":"testing"}
  ]
}

Is there a way to ignore ids if they get rearranged?

chbrown commented 7 years ago

Currently, no — the array diff algorithm is not pluggable with a custom equality/comparison function.

I'm not sure what your use-case is, or why you'd want to ignore ids. Can you describe the sort of output you'd want?

chbrown commented 7 years ago

I'm guessing maybe you want to factor the changes into two different steps?

  1. create patches for each changed item, depending on the id
  2. create reordering patches

The library currently optimizes for a few wholesale patches, rather than many smaller/incremental patches. While that sort of factoring I imagine you want would be cool, it's going to be very use-case specific. If you have ideas on how to make the diff algorithm more user-configurable, I'm all ears.

crh225 commented 7 years ago

If some how the child object changes order, (id is no longer the first element) I dont want the Id's and all data to be moved around in the database.

chbrown commented 7 years ago

I'm still not clear what you want. The key-value 'elements' of plain old JavaScript objects don't have any proper order, so id being the 'first' element or not doesn't have any real significance.

The return value of rfc6902.createPatch(input, output) is a list of JSON Patch objects. You've provided your input and output, but it would be helpful if you could also describe the output you want in precise terms, i.e., the specific list of JSON Patch objects, for your specific case.

crh225 commented 7 years ago

Im new to patch and have tried to read the rfc6902. Im no expert. Here is the output from the inputs and outputs described on the original post. My issue is in the database I cannot just replace/update Id columns. it would need to delete id 1 and then insert id 3. Im sorry I have not tested it out yet to see what actually happens in the database. Im my head, it should remove operation the value of 1, and add a value of 3.

[
  {
    "op": "replace",
    "path": "/repositories/0/id",
    "value": 3
  },
  {
    "op": "replace",
    "path": "/repositories/0/text",
    "value": "test3"
  },
  {
    "op": "replace",
    "path": "/repositories/1/id",
    "value": 4
  },
  {
    "op": "replace",
    "path": "/repositories/1/text",
    "value": "test4"
  },
  {
    "op": "replace",
    "path": "/repositories/2/id",
    "value": 1
  },
  {
    "op": "replace",
    "path": "/repositories/2/text",
    "value": "testing"
  }
]
chbrown commented 7 years ago

I suspect the database that's taking issue with your usage is a relational databases? This library, rfc6902 is primarily for document/object manipulation. So it would be relevant for use with MongoDB or Couchbase or whatever, but not PostgreSQL or MySQL etc.

If your database isn't going to let you change id values wholesale, you're using rfc6902 for the wrong thing.

In your case, you would need something much more custom. Given input and output arrays of repositories with ids, you would need to write, in your own code, methods for finding items in the input that match each item in the output, and then, if you found something, you could call createPatch(...) on the individual items.