Anna-Team / AnnaDB

Developer-first database
Apache License 2.0
56 stars 4 forks source link

Feature Request: Node References and Nested Inserts #11

Open amaster507 opened 1 year ago

amaster507 commented 1 year ago

What you wanted to do

Nested inserts of nodes. Insert items into a collection and in a nested manner, insert items into the same or different collections.

What you did instead

I could insert vectors and maps, but these vectors and maps would all be part of the same item in the parent collection and could not be queried from other collections.

collection|person|:q[
  insert[
    person|foo|:m{
      s|name|:s|Foo|
    }
  ],
  insert[
    m{
      s|name|:s|Bar|,
      s|friends|:v[
        person|foo|
      ]
    }
  ]
]

The work around to this is to do back to back inserts into collections and handle the linking in subsequent queries insert operations.

Ideas on what this might look like

What if inserts could be cascaded. Right now it seems nothing can come in the pipe after an insert. But what if you could do something like this for starters:

collection|person|:q[
  insert[
    person|foo|:m{
      s|name|:s|Foo|
    }
  ],
  insert[
    m{
      s|name|:s|Bar|,
      s|friends|:v[
        person|foo|
      ]
    }
  ]
]

The person|foo| is a temporary id that is not persisted. It is used for cross reference in the same transaction. And then if you could do that then you could also probably do something like:

collection|person|:insert[
  m{
    s|name|:s|Bar|,
    s|friends|:v[
      person|:m{
        s|name|:s|Foo|
      }
    ]
  }
]

And combine these two to be able to create inverse relationships (see issue __):

collection|person|:insert[
  person|bar|:m{
    s|name|:s|Foo|,
    s|friends|:v[
      person|foo|:m{
        s|name|:s|Foo|,
        s|friends|:v[
          person|bar|
        ]
      }
    ]
  }
]

The person|foo| in this query is not being referenced anywhere else so in theory, this could be person| and work the same way.

And this would also allow to insert more than one collection at a time. This is where it might get complicated not knowing how the resolvers for insert work.

collection|post|:insert[
  post|p1|:m{
    s|title|:s|Check Out AnnaDB!|,
    s|comments|:v[
      comment|c1|:m{
        s|text|:s|This is pretty awesome!|,
        s|onPost|:post|p1|
        replies:v[
          comment|:m{
            s|text|:s|For sure!|,
            s|replyTo|:comment|c1|
          }
        ]
      }
    ]
  }
]
amaster507 commented 1 year ago

This might depend or correlate also with: https://github.com/Anna-Team/AnnaDB/issues/14#issuecomment-1256816548