| 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464 |
1
8
8
8
8
104
104
104
104
104
104
104
31
1
30
28
2
2
104
29
104
31
1
30
28
2
2
104
29
104
104
104
104
104
30
8
8
8
8
8
8
8
8
8
8
8
8
8
8
8
8
8
8
8
8
8
10
10
10
10
8
10
8
58
58
58
58
58
58
8
128
128
128
128
128
8
8
10
10
10
10
7
3
8
38
38
38
38
38
38
8
38
38
38
38
38
38
38
38
38
38
7
1
1
1
1
1
1
6
31
31
31
31
8
2
2
2
2
8
62
62
62
62
62
62
62
62
62
62
62
62
62
62
62
62
62
62
62
62
62
8
2
2
2
8
61
61
61
61
61
122
122
122
122
122
122
61
8
61
61
61
61
8
61
61
61
61
8
61
8
61
61
8
61
8
2
2
8
8
8
125
125
1
125
1
125
8
124
124
8
38
8
38
8
38
38
8
8
10
8
8
8
32
32
32
32
32
32
32
32
32
32
8
8
8
8
8
8
8
8
8
8
8
31
8
8
1
1
| // # Relationship
/*
* TODO:
* * make query mapper from Node available for relationships as well
* * make relationships queryable with custom queries
*/
var __initRelationship__ = function(neo4jrestful, Graph, Node) {
Iif (typeof window === 'object') {
var helpers = window.Neo4jMapper.helpers;
var _ = window._;
} else {
var helpers = require('./helpers');
var _ = require('underscore');
}
// Constructor of Relationship
var Relationship = function Relationship(type, data, start, end, id, cb) {
this.type = this._type_ = type || null;
this.from = {
id: null,
uri: null
};
this.to = {
id: null,
uri: null
};
this.data = data || {};
var startID = null;
var endID = null;
if (start)
if (Number(start.id))
startID = Number(start.id);
else if (Number(start))
startID = Number(start);
else Eif (start)
// we assume we have a url here
this.setPointIdByUri('from', start);
if (startID)
this.setPointUriById('from', startID);
if (end)
if (Number(end.id))
endID = Number(end.id);
else if (Number(end))
endID = Number(end);
else Eif (end)
// we assume we have a url here
this.setPointIdByUri('to', end);
if (endID)
this.setPointUriById('to', endID);
this.fields = _.extend({},{
defaults: _.extend({}, this.fields.defaults),
indexes: _.extend({}, this.fields.indexes) // TODO: implement
});
this._is_instanced_ = true;
Iif (typeof id === 'number') {
this.setUriById(id);
this.id = this._id_ = id;
} else {
cb = id;
}
if (typeof cb === 'function') {
return this.save(cb);
}
}
Relationship.prototype.classification = 'Relationship'; // only needed for toObject()
Relationship.prototype.data = {};
Relationship.prototype.start = null;
Relationship.prototype.type = null;
Relationship.prototype._type_ = null; // like `_id_` to keep a reference to the legacy type
Relationship.prototype.end = null;
Relationship.prototype.from = null;
Relationship.prototype.to = null;
Relationship.prototype.id = null;
Relationship.prototype._id_ = null;
Relationship.prototype._hashedData_ = null;
Relationship.prototype.uri = null;
Relationship.prototype._response_ = null;
Relationship.prototype._is_singleton_ = false;
Relationship.prototype._is_persisted_ = false;
Relationship.prototype.cypher = null;
Relationship.prototype._is_instanced_ = null;
Relationship.prototype.fields = {
defaults: {},
indexes: {}
};
// should **never** be changed
Relationship.prototype.__TYPE__ = 'relationship';
Relationship.prototype.__TYPE_IDENTIFIER__ = 'r';
Relationship.prototype.singleton = function(id) {
var relationship = new Relationship();
Iif (typeof id === 'number') {
this.id = this._id_ = id;
}
relationship._is_singleton_ = true;
// relationship.resetQuery();
return relationship;
}
Relationship.singleton = function(id) {
return this.prototype.singleton(id);
}
Relationship.prototype.setPointUriById = function(startOrEnd, id) {
Iif (typeof startOrEnd !== 'string')
startOrEnd = 'from';
Iif ((startOrEnd !== 'from')&&(startOrEnd !== 'to'))
throw Error("You have to set startOrEnd argument to 'from' or 'to'");
Eif (_.isNumber(id)) {
this[startOrEnd].uri = neo4jrestful.absoluteUrl('/relationship/'+id);
this[startOrEnd].id = id;
}
return this;
}
Relationship.prototype.setPointIdByUri = function(startOrEnd, uri) {
Iif (typeof startOrEnd !== 'string')
startOrEnd = 'from';
Iif ((startOrEnd !== 'from')&&(startOrEnd !== 'to'))
throw Error("You have to set startOrEnd argument to 'from' or 'to'");
Eif (uri.match(/[0-9]+$/)) {
this[startOrEnd].uri = uri;
this[startOrEnd].id = Number(uri.match(/[0-9]+$/)[0]);
}
}
Relationship.prototype.applyDefaultValues = null; // will be initialized
Relationship.prototype.findById = function(id, cb) {
var self = this;
Eif ( (_.isNumber(Number(id))) && (typeof cb === 'function') ) {
// to reduce calls we'll make a specific restful request for one node
return Graph.request().get(this.__TYPE__+'/'+id, function(err, object) {
if ((object) && (typeof self.load === 'function')) {
// && (typeof node.load === 'function')
object.load(cb);
} else {
cb(err, object);
}
});
}
return this;
}
Relationship.prototype.save = function(cb) {
var self = this;
self.onBeforeSave(self, function(err) {
// don't execute if an error is passed through
Iif ((typeof err !== 'undefined')&&(err !== null))
cb(err, null);
else
self.onSave(function(err, relationship, debug) {
Iif (err)
return cb(err, relationship, debug);
else
return self.onAfterSave(self, cb, debug);
});
});
}
Relationship.prototype.onSave = function(cb) {
var self = this;
Iif (this._is_singleton_)
return cb(Error('Singleton instances can not be persisted'), null);
Iif (!this.hasValidData())
return cb(Error('relationship does not contain valid data. `'+this.__TYPE__+'.data` must be an object.'));
this.resetQuery();
this.applyDefaultValues();
this.id = this._id_;
Iif (!this.type)
throw Error("Type for a relationship is mandatory, e.g. `relationship.type = 'KNOW'`");
Iif ((!(this.from))||(isNaN(this.from.id)))
throw Error('Relationship requires a `relationship.from` startnode');
Iif ((!(this.to))||(isNaN(this.to.id)))
throw Error('Relationship requires a `relationship.to` endnode');
if (this.hasId()) {
if ((this._type_) && (this.type !== this._type_)) {
// type has changed
// since we can't update a relationship type (only properties)
// we have to create a new relationship and delete the "old" one
return Relationship.create(this.type, this.data, this.start, this.end, function(err, relationship, debug) {
Iif (err) {
return cb(err, relationship, debug);
} else {
self.remove(function(err, res, debugDelete) {
Iif (err) {
return cb(err, res, debugDelete);
} else {
relationship.copyTo(self);
return cb(null, self, debug);
}
})
}
})
}
// UPDATE properties
// url = 'relationship/'+this._id_+'/properties';
Graph
.start('r = relationship({id})', {
id: Number(this.id),
})
.setWith( { r: this.dataForCypher() } )
.return('r')
.exec(cb);
} else {
Graph
.start('n = node({from}), m = node({to})', {
from: Number(this.from.id),
to: Number(this.to.id),
})
.create([ '(n)-[r: '+helpers.escapeProperty(this.type), this.dataForCypher(), ']->(m)'])
.return('r')
.limit(1)
.exec(function(err, relationship, debug) {
Iif ((err) || (!relationship))
return cb(err, relationship, debug);
else {
relationship.copyTo(self);
return cb(null, self, debug);
}
});
}
}
Relationship.prototype.update = function(data, cb) {
Eif (helpers.isObjectLiteral(data)) {
this.data = _.extend(this.data, data);
data = this.flattenData();
} else {
cb = data;
}
return this.save(cb);
}
Relationship.prototype.populateWithDataFromResponse = function(data, create) {
create = (typeof create !== 'undefined') ? create : false;
// if we are working on the prototype object
// we won't mutate it and create a new relationship instance insetad
var relationship = (this._is_instanced_ !== null) ? this : new Relationship();
Iif (create)
relationship = new Relationship();
Eif (data) {
Eif (_.isObject(data) && (!_.isArray(data)))
relationship._response_ = data;
else
relationship._response_ = data[0];
relationship.data = relationship._response_.data;
relationship.data = helpers.unflattenObject(this.data);
relationship.uri = relationship._response_.self;
relationship.type = relationship._type_ = relationship._response_.type;
Eif ((relationship._response_.self) && (relationship._response_.self.match(/[0-9]+$/))) {
relationship.id = relationship._id_ = Number(relationship._response_.self.match(/[0-9]+$/)[0]);
}
Eif ((relationship._response_.start) && (relationship._response_.start.match(/[0-9]+$/))) {
relationship.from.uri = relationship.start = relationship._response_.start;
relationship.setPointIdByUri('from', relationship._response_.start);
}
Eif ((relationship._response_.end) && (relationship._response_.end.match(/[0-9]+$/))) {
relationship.to.uri = relationship.end = relationship._response_.end;
relationship.setPointIdByUri('to', relationship._response_.end);
}
}
relationship._is_persisted_ = true;
relationship.isPersisted(true);
return relationship;
}
Relationship.prototype.remove = function(cb) {
Iif (this._is_singleton_)
return cb(Error("To delete results of a query use delete(). remove() is for removing a relationship."),null);
Eif (this.hasId()) {
return Graph.request().delete('relationship/'+this.id, cb);
}
return this;
}
Relationship.prototype.loadFromAndToNodes = function(cb) {
var self = this;
var attributes = ['from', 'to'];
var done = 0;
var errors = [];
for (var i = 0; i < 2; i++) {
(function(point){
Node.findById(self[point].id,function(err,node) {
self[point] = node;
Iif (err)
errors.push(err);
done++;
if (done === 2) {
cb((errors.length === 0) ? null : errors, self);
}
});
})(attributes[i]);
}
}
Relationship.prototype.load = function(cb) {
var self = this;
this._onBeforeLoad(self, function(err, relationship){
Iif (err)
cb(err, relationship);
else
self._onAfterLoad(relationship, cb);
})
}
Relationship.prototype._onBeforeLoad = function(relationship, next) {
return this.onBeforeLoad(relationship, function(err, relationship) {
Eif (relationship.hasId()) {
relationship.loadFromAndToNodes(function(err, relationship){
next(err, relationship);
});
} else {
next(null, relationship);
}
});
}
Relationship.prototype.onBeforeLoad = function(relationship, next) {
return next(null, relationship);
}
Relationship.prototype._onAfterLoad = function(relationship, next) {
return this.onAfterLoad(relationship, function(err, relationship) {
return next(null, relationship);
})
}
Relationship.prototype.onAfterLoad = function(relationship, next) {
return next(null, relationship);
}
Relationship.prototype.toQuery = function() {
Eif (this.hasId()) {
return Graph
.start('r = relationship('+this.id+')')
.return('r').toQuery();
}
return Graph.start('r = relationship(*)').toQuery();
}
Relationship.prototype.toQueryString = Node.prototype.toQueryString;
Relationship.prototype.toCypherQuery = Node.prototype.toCypherQuery;
Relationship.prototype.toObject = function() {
var o = {
id: this.id,
classification: this.classification,
data: _.clone(this.data),
start: this.start,
end: this.end,
from: _.extend(this.from),
to: _.extend(this.to),
uri: this.uri,
type: this.type
};
if ( (o.from) && (typeof o.from.toObject === 'function') )
o.from = o.from.toObject();
if ( (o.to) && (typeof o.to.toObject === 'function') )
o.to = o.to.toObject();
return o;
}
Relationship.prototype._hashData_ = function() {
Eif (this.hasValidData())
return helpers.md5(JSON.stringify(this.toObject()));
else
return null;
}
Relationship.prototype.onBeforeSave = function(node, next) {
next(null, null);
}
Relationship.prototype.onAfterSave = function(relationship, next, debug) {
return next(null, relationship, debug);
}
Relationship.prototype.resetQuery = function() {
this.cypher = new helpers.CypherQuery();
return this;
}
Relationship.prototype._load_hook_reference_ = null;
/*
* Static singleton methods
*/
Relationship.findById = function(id, cb) {
return Relationship.singleton().findById(id, cb);
}
Relationship.recommendConstructor = function() {
return Relationship;
}
/* from Node */
Relationship.prototype.applyDefaultValues = Node.prototype.applyDefaultValues
// Copys only the node's relevant data(s) to another object
Relationship.prototype.copyTo = function(r) {
r.id = r._id_ = this._id_;
r.data = _.clone(this.data);
r.uri = this.uri;
r._response_ = _.clone(this._response_);
r.from = _.clone(this.from);
r.to = _.clone(this.to);
r.start = this.start;
r.end = this.end;
r.type = r._type_ = this._type_;
return r;
}
Relationship.prototype.hasValidData = Node.prototype.hasValidData;
Relationship.prototype.flattenData = Node.prototype.flattenData;
Relationship.prototype.setUriById = Node.prototype.setUriById;
Relationship.prototype.isPersisted = Node.prototype.isPersisted;
Relationship.prototype.hasId = Node.prototype.hasId;
Relationship.prototype.dataForCypher = Node.prototype.dataForCypher;
Relationship.setDefaultFields = Node.setDefaultFields;
Relationship.setIndexFields = Node.setIndexFields;
Relationship.setUniqueFields = Node.setUniqueFields;
Relationship._setModelFields = Node._setModelFields;
Relationship.new = function(type, data, start, end, id, cb) {
return new Relationship(type, data, start, end, id, cb);
}
Relationship.create = Relationship.new;
return neo4jrestful.Relationship = Relationship;
}
Eif (typeof window !== 'object') {
module.exports = exports = {
init: __initRelationship__
}
} else {
window.Neo4jMapper.initRelationship = __initRelationship__;
}
|