3

I have a neo4j DB in which user data and relations between them would be stored, the end user will interact with this data from a mobile app (app is in Flutter, we use a nestjs neo4j connector in between). Now we have to enable offline access to data. So the idea was to export the data of the user from neo4j as json and use it when offline, when the device gets online we will make the changes to the DB. I have some problem getting the data as json

This is a rough sample what I am trying to do enter image description here

The cypher commands to create these nodes

CREATE (c:Computer {name: 'Andy',uid:'123'})
CREATE (d1:Drive {name: 'Drive1',capacity:"2gb",uid:'223'})
CREATE (d2:Drive {name: 'Drive2',capacity:"4gb",uid:'233'})
CREATE (f1:Folder {name: 'desktop',type:"special",uid:'323'})
CREATE (f2:Folder {name: 'mydocuments',type:"special",uid:'333'})
CREATE (f3:Folder {name: 'myprojects',type:"normal",uid:'343'})

CREATE (t1:File {name: 'text1',type:"txt",size:"1kb",uid:'423'})
CREATE (t2:File {name: 'text2',type:"txt",size:"1.5kb",uid:'433'})
CREATE (t3:File {name: 'text3',type:"txt",size:"2kb",uid:'443'})

CREATE (do1:File {name: 'doc1',type:"doc",size:"1mb",uid:'523'})
CREATE (do2:File {name: 'doc2',type:"doc",size:"1.5mb",uid:'533'})
CREATE (do3:File {name: 'doc3',type:"doc",size:"2mb",uid:'543'})


CREATE (c)-[r1:PARTITION{during: 'osinstall'}]->(d1)
CREATE (c)-[r2:PARTITION{during: 'setup'}]->(d2)

CREATE (d1)-[r3:AutoCreated{during: 'osinstall',type:"folder"}]->(f1)
CREATE (d1)-[r4:AutoCreated{during: 'osinstall',type:"folder"}]->(f2)
CREATE (f1)-[r5:Shortcut{type:"folder"}]->(c)
CREATE (f2)-[r6:Shortcut{type:"folder"}]->(c)
CREATE (d2)-[r7:UserCreated{type:"folder"}]->(f3)

CREATE (d2)-[r8:UserCreated{type:"file"}]->(t1)
CREATE (d2)-[r9:UserCreated{type:"file"}]->(t2)
CREATE (f3)-[r10:UserCreated{type:"file"}]->(t3)
CREATE (f3)-[r11:UserCreated{type:"file"}]->(do1)
CREATE (d2)-[r12:UserCreated{type:"file"}]->(do2)
CREATE (do2)-[r13:Shortcut{type:"file"}]->(f1)
CREATE (f3)-[r14:Shortcut{type:"folder"}]->(f1)
CREATE (f1)-[r15:UserCreated{type:"file"}]->(do3)
CREATE (do3)-[r16:Shortcut{type:"file"}]->(f3)

CREATE (c1:Computer {name: 'Randy',uid:'c1-123'})
CREATE (c1d1:Drive {name: 'Drive1',capacity:"1gb",uid:'c1-223'})
CREATE (c1t1:File {name: 'text1',type:"txt",size:"1kb",uid:'c1-423'})
CREATE (c1t2:File {name: 'text2',type:"txt",size:"1.5kb",uid:'c1-433'})
CREATE (c1sh1:SharedDrive {name:"SharedDrive",uid:'c1-s1'})

CREATE (c1)-[c1r1:PARTITION{during: 'osinstall'}]->(c1d1)
CREATE (c1d1)-[c1r2:UserCreated{type:"file"}]->(c1t1)
CREATE (c1d1)-[c1r3:UserCreated{type:"file"}]->(c1t2)
CREATE (c1t1)-[c1r4:Share{type:"file"}]->(c1sh1)
CREATE (c1)-[c1r5:SHAREDPARTITION{during: 'osinstall'}]->(c1sh1)
CREATE (c1)-[common:Network]->(c)

I want to query a root node(say Andy) with the users uid and get the data in the format

    {
    "name": "Andy",
    "uid":"123",
    "PARTITION":[
        {
            "name": "Drive1",
            "capacity":"2gb",
            "uid":"223",
            "Folder":[
               { "name": "desktop","type":"special","uid":"323",
               "File":[
                   "... Detail about file doc3 here.."
               ],
               "Shortcut":[
                   "... Detail about file doc2 here.."
               ]
               
               },
               {"name": "mydocuments","type":"special","uid":"333"}
            ]
        },
        {
            "name": "Drive2",
            "capacity":"4gb",
            "uid":"233",
            "Folder":[
               { "name": "myprojects","type":"normal","uid":"343",
                "File":[
                    "...Detail about Files doc1, text3 here..."
                ],
                "Shortcut":[
                    "... Detail about file doc3 here.."
                ]
                
               }
            ],
            "File":[
                "...Detail about Files text1,text2,doc 2 here..."
            ]

        }
    ],
    "Shortcut":[
        {
            "name": "desktop","type":"special","uid":"323"
        },{
            "name": "mydocuments","type":"special","uid":"333"
        }
    ],
    "Network":[
        {
            "name": "Randy",
            "uid":"c1-123",
            "SHAREDPARTITION":["...HERE ONLY NEED THE files and folders from shareddrive other drives should not show up..."]

        }
    ]
}

I want to add the relations from and to the node as key and for the value add a list of nodes(with their related properties) connected to it and move to the next one. I don't know how to do so. So far I have tried

match (n:Computer{uid:"123"})-[r:PARTITION]->(x)
match b=(x)-[*]->(y)
with collect(b) as c
call apoc.convert.toTree(c) yield value
return value

but this does not return the shortcut file paths properly ie., if I add a shortcut from doc3(at desktop) to myproject, I don't find the file detail with myproject shortcuts I need it at both places at desktop(under files) and myproject(under shortcut) folder. Also the shared computer details are not fetched(all drives must not be fetched just the sharedpartition). Apart from this the return data is not in the expected format and I have to process it in the app after fetching it.

Can someone help me with this?

I am also open to different solutions for neo4j flutter offline.

1 Answers1

0

You can create a connection from the root node then collect them all together. Just make sure you are using the relationship that you want to extract. Below is not exactly you described but closest to your json format.

match b=(n:Computer{uid:"123"})-[r:PARTITION]->(x:Drive)-[]-(y:Folder)-[]-(z:File) 
match c=(n)-[:Shortcut]-()
match d=(n)-[:Network]-()-[:SHAREDPARTITION]-()
with collect(b) + collect(c) + collect(d) as t
call apoc.convert.toTree(t) yield value
return value

Result:

enter image description here

 {
  "name": "Andy",
  "uid": "123",
  "_type": "Computer",
  "_id": 298,
  "partition": [
    {
      "uid": "233",
      "_type": "Drive",
      "name": "Drive2",
      "_id": 300,
      "partition.during": "setup",
      "capacity": "4gb",
      "usercreated": [
        {
          "uid": "343",
          "shortcut": [
            {
              "uid": "543",
              "size": "2mb",
              "shortcut.type": "file",
              "_type": "File",
              "name": "doc3",
              "_id": 309,
              "type": "doc"
            }
          ],
          "_type": "Folder",
          "name": "myprojects",
          "usercreated": [
            {
              "uid": "443",
              "size": "2kb",
              "_type": "File",
              "name": "text3",
              "_id": 306,
              "type": "txt",
              "usercreated.type": "file"
            },
            {
              "uid": "523",
              "size": "1mb",
              "_type": "File",
              "name": "doc1",
              "_id": 307,
              "type": "doc",
              "usercreated.type": "file"
            }
          ],
          "_id": 303,
          "type": "normal",
          "usercreated.type": "folder"
        }
      ]
    },
    {
      "uid": "223",
      "_type": "Drive",
      "name": "Drive1",
      "_id": 299,
      "partition.during": "osinstall",
      "capacity": "2gb",
      "autocreated": [
        {
          "autocreated.during": "osinstall",
          "autocreated.type": "folder",
          "uid": "323",
          "shortcut": [
            {
              "uid": "533",
              "size": "1.5mb",
              "shortcut.type": "file",
              "_type": "File",
              "name": "doc2",
              "_id": 308,
              "type": "doc"
            }
          ],
          "_type": "Folder",
          "name": "desktop",
          "usercreated": [
            {
              "uid": "543",
              "size": "2mb",
              "_type": "File",
              "name": "doc3",
              "_id": 309,
              "type": "doc",
              "usercreated.type": "file"
            }
          ],
          "_id": 301,
          "type": "special"
        }
      ]
    }
  ],
  "shortcut": [
    {
      "uid": "333",
      "shortcut.type": "folder",
      "_type": "Folder",
      "name": "mydocuments",
      "_id": 302,
      "type": "special"
    },
    {
      "uid": "323",
      "shortcut.type": "folder",
      "_type": "Folder",
      "name": "desktop",
      "_id": 301,
      "type": "special"
    }
  ],
  "network": [
    {
      "_type": "Computer",
      "name": "Randy",
      "uid": "c1-123",
      "_id": 310,
      "sharedpartition": [
        {
          "_type": "SharedDrive",
          "name": "SharedDrive",
          "uid": "c1-s1",
          "_id": 473,
          "sharedpartition.during": "osinstall"
        }
      ]
    }
  ]
}
jose_bacoy
  • 12,227
  • 1
  • 20
  • 38
  • Thank you, but it doesn't fetch files directly in drive ie., text1 and 2. Also there might be folders inside folder and then maybe a file. Also I think it didn't fetch the file inside the shared drive(it could be a nested folder structure too). Any possibilities to wildcard the other nodes except the root node(computer) and network relation – Pratheesh Russell Feb 26 '22 at 11:29
  • You cannot change the structure of the tree acording to your "want", you need cut, trim and polish the tree to make a cabinet. Trees do not grow as a cabinet. :P – jose_bacoy Feb 26 '22 at 11:40