-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathRelationship.js
149 lines (131 loc) · 4.03 KB
/
Relationship.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import Entity from './Entity';
import UpdateRelationship from './Services/UpdateRelationship';
import DeleteRelationship from './Services/DeleteRelationship';
import { DIRECTION_IN, } from './RelationshipType';
export default class Relationship extends Entity {
/**
*
* @param {Neode} neode Neode instance
* @param {RelationshipType} definition Relationship type definition
* @param {Integer} identity Identity
* @param {String} relationship Relationship type
* @param {Map} properties Map of properties for the relationship
* @param {Node} start Start Node
* @param {Node} end End Node
* @param {String} node_alias Alias given to the Node when converting to JSON
*/
constructor(neode, definition, identity, type, properties, start, end, node_alias) {
super();
this._neode = neode;
this._definition = definition;
this._identity = identity;
this._type = type;
this._properties = properties || new Map;
this._start = start;
this._end = end;
this._node_alias = node_alias;
}
/**
* Get the definition for this relationship
*
* @return {Definition}
*/
definition() {
return this._definition;
}
/**
* Get the relationship type
*/
type() {
return this._type;
}
/**
* Get the start node for this relationship
*
* @return {Node}
*/
startNode() {
return this._start;
}
/**
* Get the start node for this relationship
*
* @return {Node}
*/
endNode() {
return this._end;
}
/**
* Get the node on the opposite end of the Relationship to the subject
* (ie if direction is in, get the end node, otherwise get the start node)
*/
otherNode() {
return this._definition.direction() == DIRECTION_IN
? this.startNode()
: this.endNode();
}
/**
* Convert Relationship to a JSON friendly Object
*
* @return {Promise}
*/
toJson() {
const output = {
_id: this.id(),
_type: this.type(),
};
const definition = this.definition();
// Properties
definition.properties().forEach((property, key) => {
if ( property.hidden() ) {
return;
}
if ( this._properties.has(key) ) {
output[ key ] = this.valueToJson(property, this._properties.get( key ));
}
});
// Get Other Node
return this.otherNode().toJson()
.then(json => {
output[ definition.nodeAlias() ] = json;
return output;
});
}
/**
* Update the properties for this relationship
*
* @param {Object} properties New properties
* @return {Node}
*/
update(properties) {
// TODO: Temporary fix, add the properties to the properties map
// Sorry, but it's easier than hacking the validator
this._definition.properties().forEach(property => {
const name = property.name();
if ( property.required() && !properties.hasOwnProperty(name) ) {
properties[ name ] = this._properties.get( name );
}
});
return UpdateRelationship(this._neode, this._definition, this._identity, properties)
.then(properties => {
Object.entries(properties).forEach(( [key, value] ) => {
this._properties.set( key, value );
});
})
.then(() => {
return this;
});
}
/**
* Delete this relationship from the Graph
*
* @return {Promise}
*/
delete() {
return DeleteRelationship(this._neode, this._identity)
.then(() => {
this._deleted = true;
return this;
});
}
}