mirror of https://gitee.com/bigwinds/arangodb
Merge pull request #651 from triAGENS/add-edge-takes-id
Allow addEdge() to take vertex ids in the JS library
This commit is contained in:
commit
16328720d3
|
@ -253,12 +253,12 @@ Graph.prototype.drop = function () {
|
|||
/// @brief saves an edge to the graph
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
Graph.prototype._saveEdge = function(id, out_vertex, in_vertex, params) {
|
||||
Graph.prototype._saveEdge = function(id, out_vertex_id, in_vertex_id, params) {
|
||||
var results;
|
||||
|
||||
params._key = id;
|
||||
params._from = out_vertex._properties._key;
|
||||
params._to = in_vertex._properties._key;
|
||||
params._from = out_vertex_id;
|
||||
params._to = in_vertex_id;
|
||||
|
||||
results = GraphAPI.postEdge(this._properties._key, params);
|
||||
return new Edge(this, results.edge);
|
||||
|
|
|
@ -704,21 +704,25 @@ Graph.prototype.getOrAddVertex = function (id) {
|
|||
///
|
||||
/// Creates a new edge from @FA{out} to @FA{in} and returns the edge object. The
|
||||
/// identifier @FA{id} must be a unique identifier or null.
|
||||
/// out and in can either be vertices or their IDs
|
||||
///
|
||||
/// @FUN{@FA{graph}.addEdge(@FA{out}, @FA{in}, @FA{id}, @FA{label})}
|
||||
///
|
||||
/// Creates a new edge from @FA{out} to @FA{in} with @FA{label} and returns the
|
||||
/// edge object.
|
||||
/// out and in can either be vertices or their IDs
|
||||
///
|
||||
/// @FUN{@FA{graph}.addEdge(@FA{out}, @FA{in}, @FA{id}, @FA{data})}
|
||||
///
|
||||
/// Creates a new edge and returns the edge object. The edge contains the
|
||||
/// properties defined in @FA{data}.
|
||||
/// out and in can either be vertices or their IDs
|
||||
///
|
||||
/// @FUN{@FA{graph}.addEdge(@FA{out}, @FA{in}, @FA{id}, @FA{label}, @FA{data})}
|
||||
///
|
||||
/// Creates a new edge and returns the edge object. The edge has the
|
||||
/// label @FA{label} and contains the properties defined in @FA{data}.
|
||||
/// out and in can either be vertices or their IDs
|
||||
///
|
||||
/// @EXAMPLES
|
||||
///
|
||||
|
@ -728,7 +732,25 @@ Graph.prototype.getOrAddVertex = function (id) {
|
|||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
Graph.prototype.addEdge = function (out_vertex, in_vertex, id, label, data, waitForSync) {
|
||||
return this._saveEdge(id, out_vertex, in_vertex, this._prepareEdgeData(data, label), waitForSync);
|
||||
var out_vertex_id, in_vertex_id;
|
||||
|
||||
if (is.string(out_vertex)) {
|
||||
out_vertex_id = out_vertex;
|
||||
} else {
|
||||
out_vertex_id = out_vertex._properties._id;
|
||||
}
|
||||
|
||||
if (is.string(in_vertex)) {
|
||||
in_vertex_id = in_vertex;
|
||||
} else {
|
||||
in_vertex_id = in_vertex._properties._id;
|
||||
}
|
||||
|
||||
return this._saveEdge(id,
|
||||
out_vertex_id,
|
||||
in_vertex_id,
|
||||
this._prepareEdgeData(data, label),
|
||||
waitForSync);
|
||||
};
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
|
|
@ -250,6 +250,25 @@ function GraphBasicsSuite() {
|
|||
assertEqual("testValue", edge.getProperty("testProperty"));
|
||||
},
|
||||
|
||||
testAddEdgeViaId : function () {
|
||||
var v1,
|
||||
v2,
|
||||
edge;
|
||||
|
||||
v1 = graph.addVertex("vertex1");
|
||||
v2 = graph.addVertex("vertex2");
|
||||
|
||||
edge = graph.addEdge(v1._properties._id,
|
||||
v2._properties._id,
|
||||
"edge1",
|
||||
"label",
|
||||
{ testProperty: "testValue" });
|
||||
|
||||
assertEqual("edge1", edge.getId());
|
||||
assertEqual("label", edge.getLabel());
|
||||
assertEqual("testValue", edge.getProperty("testProperty"));
|
||||
},
|
||||
|
||||
testAddEdgeWithLabelSetViaData : function () {
|
||||
var v1,
|
||||
v2,
|
||||
|
|
|
@ -563,17 +563,17 @@ Graph.prototype.drop = function (waitForSync) {
|
|||
/// @brief saves an edge to the graph
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
Graph.prototype._saveEdge = function(id, out_vertex, in_vertex, shallow, waitForSync) {
|
||||
Graph.prototype._saveEdge = function(id, out_vertex_id, in_vertex_id, shallow, waitForSync) {
|
||||
this.emptyCachedPredecessors();
|
||||
|
||||
if (id !== undefined && id !== null) {
|
||||
shallow._key = String(id);
|
||||
}
|
||||
|
||||
var ref = this._edges.save(out_vertex._properties._id,
|
||||
in_vertex._properties._id,
|
||||
shallow,
|
||||
waitForSync);
|
||||
var ref = this._edges.save(out_vertex_id,
|
||||
in_vertex_id,
|
||||
shallow,
|
||||
waitForSync);
|
||||
|
||||
return this.constructEdge(ref._id);
|
||||
};
|
||||
|
|
Loading…
Reference in New Issue