loopback icon indicating copy to clipboard operation
loopback copied to clipboard

Batch and Tree based Upsert and Delete...

Open tonysoft opened this issue 12 years ago • 6 comments

Now that LoopBack is working on filter/include to any depth of Relationships for example:

http://localhost:3000/api/Students/1/teachers?filter={"include": {"relation": "classes", "scope": {"include": "students"}}}

would return "ALL Teachers related to a specific Student return ALL Classes related to each Teacher and for each of these Classes ALL Students related to the Class".

The results look like the sample JSON below.

Once this JSON hits the Client, it might display a Tree View Edit Form where fields from any Teacher, Class, or Student could be edited. The Client Logic would expose which records were editable. In some "admin" mode, perhaps all of the records would be editable.

I proposed that a PUT (Upsert) to .../api/Teachers could pass in the JSON Below and, if an ARRAY rather than an Object is passed in the BODY, a Deep Batch Modification (Upsert or Delete) would take place on any Object decorated with UPSERT or DELETE OPERATION properties. This should be self explanatory from the decorated JSON below... The Client will be totally responsible for specifying the OPERATION based on UX actions.

  {
    "OPERATION": "UPSERT",
    "firstName": "Issac",
    "lastName": "Roth",
    "department": "Computer Science",
    "id": 1,
    "classes": [
      {
        "subject": "LoopBack",
        "id": 1,
        "teacherId": 1,
        "students": [
          {
            "OPERATION": "UPSERT",
            "firstName": "Tony",
            "lastName": "Sukiennik",
            "major": "Economics",
            "id": 1
          },
          {
            "OPERATION": "DELETE_RELATION",
            "firstName": "Dennis",
            "lastName": "Ashby",
            "id": 8
          }
        ]
      },
      {
        "OPERATION": "DELETE",
        "subject": "REST APIs",
        "id": 3,
        "teacherId": 1,
        "students": []
      }
    ]
  }
]

tonysoft avatar Oct 14 '14 15:10 tonysoft

IMO, two formats are ideal:

  1. Batch - sending in multiple requests using multipart/mixed mime type, see https://cloud.google.com/storage/docs/json_api/v1/how-tos/batch
  2. Graph update - sending in a data graph with associated operations. Maybe we can use a format like http://jsonpatch.com/.

raymondfeng avatar Oct 14 '14 16:10 raymondfeng

I just wanted to get the ball rolling on the functional requirement.

With that said, on the Client Side, if I'm writing something like an Angular App, my API calls return nicely formatted JSON that maps directly to the UI with 2-Way Data Binding. It would be great to use the JSON Format returned in the API Call without a lot of Transformation (seems like jsonpatch would need the data to be transformed).

So here is the flow

  • Get the JSON from the API
  • Bind it to the UI
  • Watch the JSON for changes
  • Edit some fields
  • Delete some nodes
  • Break some relationships.
  • Add the OPERATIONS to specific JSON Nodes within the WATCH
  • Present a SAVE Button in the UI when anything changes
  • On SAVE, Upsert the root of the JSON (in the example JSON above PUT Teachers)
  • All the specified OPERATIONS within JSON nodes get performed on the Server

If jsonpatch is less different than the APIs JSON than it looks, than that might be a good solution.

Again, I'm just putting out the feature request which I think would be a very powerful complement to the Nested Includes feature just released.

tonysoft avatar Oct 14 '14 16:10 tonysoft

FWIW, a similar mechanism is used by the data sync/replication algorithm - see PersistedModel.bulkUpdate().

bajtos avatar Oct 15 '14 15:10 bajtos

Understood... So, if the client has access to LoopBacks Synchronization Interfaces, they could traverse their tree and create the update records and then trigger an immediate batch update.

The scenario and Dev experience I'm pushing would allow the client to make the same Upsert API calls with an Array of Nested Nodes decorated with the OPERATIONS. This approach would not require the Developer to learn more.

With that said, I could accept that this is another use case for LoopBack's Online/Offline Sync infrastructure and if that's the solution we'd just have to Document that Use Case.

tonysoft avatar Oct 15 '14 16:10 tonysoft

I don't have a strong opinion on this. My goal was to point out a similar feature that is already in place, so that we can come up with a consistent approach for both replication and batch updates of nested structures.

bajtos avatar Oct 16 '14 07:10 bajtos

I am closing this issue, since there were not enough people asking for this enhancement and it clearly was not a priority for us so far. We can reopen it again later when there is more demand.

bajtos avatar Jun 02 '16 11:06 bajtos