-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathjanus-classes.coffee
136 lines (105 loc) · 3.58 KB
/
janus-classes.coffee
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
util = require 'util'
module.exports = (client) ->
class JanusEntity
constructor: (@client) ->
class Graph extends JanusEntity
constructor: (client) ->
super client
V: (selector) ->
(
if selector
@client.post "g.V(%j)", selector
else
@client.post "g.V()"
).then (vectors) -> @wrapVectors vectors
class QueryBuilder
@parameters: Object.assign {},
("addV addE as from to hasKey".split(' ').map (f) -> "#{f}": 1)...
V: [0, 1]
property: 2
properties: [0, 1]
hasLabel: [1, Infinity]
has: [2, 3]
hasId: [1, Infinity]
constructor: (@soFar = "g") ->
submit: ->
client.post @soFar
fn: (qInfo) ->
for name, args of qInfo
if not Array.isArray args
args = [args]
if not paramCount = QueryBuilder.templates[name]
throw new Error "No template for function '#{name}'"
if 'number' is paramCount and args.length isnt paramCount
throw new Error "Wrong number of args (#{args.length}) for function #{name} (#{paramCount})"
if Array.isArray paramCount and paramCount.length is 2 and not paramCount[0] <= args.length <= paramCount[1]
throw new Error "Wrong number of args (#{args.length}) for function #{name} (#{paramCount.join ".."})"
paramStr = new Array args.length
.fill '%j'
.join ','
@soFar += util.format ".#{name}(#{paramStr})", args...
class Property
@create: (queryBuilder, name, values) ->
values.forEach (v) -> queryBuilder.fn property: [name, v]
return queryBuilder
constructor: (@attachedTo, @name, @values) ->
@first = @values[0]
values: -> @values.map (v) -> v.value
addValues: (values) ->
query = new QueryBuilder
.fn V: @attachedTo.id
for v in values
query.fn property: [@name, v]
query.submit()
.then ->
query = @attachedTo.query()
.fn properties: @name
.fn valueMap: true
.then (valueMap) ->
class Vertex
@create: (type, name, properties) ->
query = new QueryBuilder
.fn addV: type
properties.name = name
for k, v of properties
Property.create query, k, v
query.submit()
.then (created) ->
created = new Vertex created
constructor: ({@id, @label, @type, properties}) ->
@properties = {}
@_addProp name, values for name, values of properties
_addProp: (name, values) ->
@properties[name] = new Property @, name, values
setProp: (name, values) ->
if not @properties[name]
@property name, values
else
@properties[name].addValues values
getProp: (propName) -> @properties[propName]
allValues: (propName) -> @properties[propName].map (p) -> p.value
firstVal: (propName) -> @properties[propName].first.value
propNames: -> Object.getOwnPropertyNames @properties
name: (nameToAdd) ->
if nameToAdd
if p = @properties.name
p.addValue nameToAdd
@allValues 'name'
else
@property 'name', nameToAdd
else
@firstVal 'name'
property: (name, value) ->
client.post 'g.V(%j).property(%j,%j)', @id, name, value
.then (prop) ->
@props[name] = new Property name, prop
connectTo: (other) ->
new Edge @, to
query: ->
new QueryBuilder
.fn V: @id
class Edge extends Vertex
constructor: (from, to, props = {}) ->
from.query
.fn as: 'a'
{Vertex, Property, QueryBuilder, Edge, client}