Syncing deeply nested list of maps with client

I am working with a mobile app syncing deeply nested data structures. Due to one of the requirements being offline functionality on mobile with server sync, I can’t break up each of the actions taken on the client into individual calls to the respective model, so I need to send the whole list of maps and do a compare with currently available stored data.

Furthermore, when a new item is added on the client, it is given a temporary id, that needs to be replaced when sync is available, so the data structure that gets sent back needs to reference that temporary id.

The best way to think of the data structures is a list of maps with a nested list of maps. So the data coming from client would look something like this:

  [
    {
       name: "list 1",
       id: 1,
       order: 0,
       items:[
         {
           id: 100,
           order: 0,
           name: "sub item 1"
         },
         {
           id: 200,
           order: 1,
           name: "sub item 2"
         },
         {
           id: 999999,
           order: 2,
           name: "new sub list item we got from incoming json request that needs to be saved"
         },
      ]
   },
   {
       name: "list 2",
       id: 2,
       order: 1,
       items: [
          {
           id: 300,
           order: 0,
           name: "sub item 1 with an updated name that needs to sync name"
         },
         {
           id: 400,
           order: 1,
           name: "sub item 2 in list 2"
         },
      ]
    },
    {
       id: 99999999,
       name: "list 3 that needs to be created",
       order: 2,
       items: [
         {
           id: 9998,
           order: 0,
           name: "sub item 1 needs save"
         },
         {
           id: 9999,
           order: 1,
           name: "sub item 2 needs save"
         },
      ],
  }
]

So in this data structure I need to:

  • sync order of parent/sub lists
  • sync addition/removal of parent list items and child list items
  • update meta info of items in the sub list of maps

I am passing the data from client to server in JSON and using Ecto/Postgres for storage. I’ve started picking at this and I’m currently putting together a normalize and reduce function to get the difference, then thinking about building an Ecto Multi query for the majority of the differences, but I could use any guidance and feedback, or possible direction where this may have been done in the past.

1 Like

Here we’re merging a map (which came from JSON) and a previously known BigQuery schema to generate a new BigQuery schema:

Basically using DeepMerge with a custom resolver.

Hope this example helps!

5 Likes

Thanks @chasers! Giving this a try, likely in parallel with my other route and will report back.

1 Like