evo-terren / Gremlin.Net.CosmosDb

Helper library when using Gremlin.Net in conjunction with a Cosmos DB graph
MIT License
62 stars 41 forks source link

Updating vertex from properties #57

Closed kirk-marple closed 5 years ago

kirk-marple commented 5 years ago

I have a use case, where I'd like to update a vertex in-place, from its properties.

Looking at code for AddV, I believe this method is what creates the initial properties:

traversal = TraversalHelper.AddObjectProperties(traversal, vertex, serializationSettings);

But I can't see a way to use this to update a vertex from its properties? This method isn't public so I can't use it directly, but can likely just make my own similar helper function for it.

Would it make any sense to extend Gremlin.Net for this?

evo-terren commented 5 years ago

You should be able to utilize the Property() step like so:

g.V<YourVertex>("id-of-vertex")
 .Property(v => v.YourProperty, newPropertyValue);

Let me know if I'm misunderstanding what you're trying to do.

kirk-marple commented 5 years ago

In my situation, I'm 'upserting' the vertex, so want to re-assign all the properties from my Vertex class.

I copied AddObjectProperties method into my helper class, made it an extension method, and added this little helper, so I can do what I want in one line:

            var s = g.V(vertex.Id, vertex);

This will lookup the vertex by ID, and then upsert all the properties.

My version of AddObjectProperties:

    private static GraphTraversal<S, E> AddObjectProperties<S, E>(this GraphTraversal<S, E> traversal, object obj)

And my helper extension:

    //
    // select vertex by id, and update with properties from object
    //
    public static GraphTraversal<Vertex, Vertex> V<S, E>(this IGraphTraversalSource g, string vertexId, object obj) where S : Vertex where E : Vertex
    {
        return g.V(vertexId).AddObjectProperties(obj);
    }
evo-terren commented 5 years ago

I understand now. While I agree this would be a very helpful extension method and could definitely see its use, I did my absolute best to keep this library strictly to the Tinkerpop steps and not a fully-featured ORM (or something of the sort). This was meant to purely be a stop-gap solution to connect the Gremlin.Net library to CosmosDb Graph.

kirk-marple commented 5 years ago

No worries, I really appreciate all the work you did on the library. It's accelerated dev greatly on this project. It was easy to make my own extensions for this purpose.