Last active
March 22, 2019 15:48
-
-
Save kevinhillinger/7a3266c413ec0a591f81b1fdef88171c to your computer and use it in GitHub Desktop.
Cosmos DB Stored Procedure - Graph / Gremlin - vertex creation and replacement
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// create vertex by label | |
// both vertex and/or edge should come in as objects. This is inline for clarity of requirements | |
// the vertex and the edge in this sample demonstrate the minimum required fields and the shape to conform to graph | |
function create_vertex(label, id) { | |
var context = getContext(); | |
var collection = context.getCollection(); | |
var selfLink = collection.getSelfLink(); | |
var newVertex = { | |
"label": label, | |
"id": id, | |
"type": "vertex", | |
"firstName": [ | |
{ "_value": id, "id": "f54e5f92-610a-4b38-850a-1771b7102e95" } | |
], | |
"age": [ | |
{ "_value": 38, "id": "bf9928f3-9c6e-4eb4-8d18-e9739411c6b6" } | |
] | |
}; | |
function handler(err, document) { | |
if (err) { | |
throw new Error(err.message); | |
} | |
context.getResponse().setBody(document.id); | |
} | |
var err = !collection.createDocument(selfLink, newVertex, handler); | |
if (err) { | |
throw new Error('The query was not accepted by the server.'); | |
} | |
var newEdge = { | |
"label": "knows", | |
"_sink": "mary", | |
"_sinkLabel": "person", | |
"_vertexId": id, | |
"_vertexLabel": label, | |
"_isEdge": true | |
}; | |
err = !collection.createDocument(selfLink, newEdge, handler); | |
if (err) { | |
throw new Error('The query was not accepted by the server.'); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// create vertex by label | |
// both vertex and/or edge should come in as objects. This is inline for clarity of requirements | |
// the vertex and the edge in this sample demonstrate the minimum required fields and the shape to conform to graph | |
function replace_vertex(oldVertexId, newVertexLabel, newVertexId) { | |
var context = getContext(); | |
var collection = context.getCollection(); | |
var selfLink = collection.getSelfLink(); | |
var newVertex = { | |
"label": newVertexLabel, | |
"id": newVertexId, | |
"type": "vertex", | |
"firstName": [ | |
{ "_value": newVertexId, "id": "f54e5f92-610a-4b38-850a-1771b7102e95" } | |
], | |
"age": [ | |
{ "_value": 38, "id": "bf9928f3-9c6e-4eb4-8d18-e9739411c6b6" } | |
] | |
}; | |
// Main execution | |
// --------------------- | |
getById(oldVertexId, function(oldVertex) { | |
replaceVertex(oldVertex, newVertex); | |
}); | |
// private methods | |
// --------------------- | |
function getById(docId, callback) { | |
collection.filter(function(doc) { | |
return doc.id == docId; | |
}, function(err, documents) { | |
if (err) { | |
throw new Error(err.message); | |
} | |
if (documents.length != 1) { | |
throw new Error("Unable to locate records for id " + docId); | |
} | |
callback(documents[0]); | |
}) | |
}; | |
function replaceVertex(oldVertex, newVertex) { | |
var oldVertexId = oldVertex.id; | |
oldVertex.label = newVertex.label; | |
oldVertex.id = newVertex.id; | |
oldVertex.properties = newVertex.properties; | |
collection.replaceDocument(oldVertex._self, oldVertex, function(err, docReplaced) { | |
if (err) { | |
throw new Error("Unable to update record for " + oldVertexId); | |
} | |
//update all edges | |
collection.queryDocuments(selfLink, 'SELECT * FROM Persons p WHERE p._vertexId = "' + oldVertexId + '"', | |
function(queryErr, edgeDocuments) { | |
if (queryErr) { | |
throw new Error("Error " + queryErr.message); | |
} | |
if (edgeDocuments.length == 0) { | |
throw new Error("error. No edges found for " + oldVertexId); | |
} | |
for (i = 0; i < edgeDocuments.length; i++) { | |
var edge = edgeDocuments[i]; | |
edge._vertexId = newVertex.id; | |
edge._vertexLabel = newVertex.label; | |
collection.replaceDocument(edge._self, edge, function(edgeErr, edgeReplaced) { | |
if (edgeErr) { | |
throw new Error("Error " + edgeErr.message); | |
} | |
return; | |
}); | |
} | |
}); | |
var err = context.getResponse().setBody(docReplaced); | |
if (err) { | |
throw new Error('The query was not accepted by the server.'); | |
} | |
}) | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Does the
SELECT * FROM Persons p WHERE ...
need to be replaced with something more generic?