-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathjanus2.coffee
119 lines (88 loc) · 3.2 KB
/
janus2.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
util = require 'util'
{createClient} = require 'gremlin'
class BetterClient
constructor: (opts...) ->
@client = createClient opts...
post: (q, values...) ->
q = util.format q, values...
console.log "Submitting query", q
new Promise (resolve, reject) =>
@client.execute q, (e, r) ->
if e then reject e else resolve r
client = new BetterClient
{Vertex, Property} = (require 'janus-classes') client
module.exports =
p: p = (promise) ->
promise
.then (d) -> console.log util.inspect global.d = d, colors: true
.catch (e) -> console.log "error", (global.e = e).response?.statusMessage or util.inspect e, colors: true
englishList: englishList = (l, conjunction) ->
[first..., last] = l
(switch l.length
when 0 then []
when 1 then [l[0]]
when 2 then [l[0], conjunction, last]
else [first.join(", "), conjunction, last]
).join " "
expectOneOf: expectOneOf = (value, types...) ->
if t = typeof value not in types
throw new TypeError "Wanted one of #{englishList types, 'or'}, got #{t}"
expectString: expectString = (value...) ->
value.forEach (v) ->
if 'string' isnt t = typeof v
throw new TypeError "Wanted string, got #{t}"
client: client
makeQuery: makeQuery = (queryTemplate, staticValues...) ->
if staticValues.length
queryTemplate = util.format queryTemplate, staticValues...
(dynamicValues...) -> client.post queryTemplate, dynamicValues...
makeVertexQuery: makeVertexQuery = (args...) ->
q = makeQuery args...
(moreArgs...) ->
q moreArgs...
.then (vertexes) ->
vertexes.map (v) -> new Vertex v
getAll: getAll = makeVertexQuery "g.V()"
has: has = (propName, propValue) ->
ret = ".has(%j, %j)"
if propName
ret = util.format ret, propName
if propValue
ret = util.format ret, propValue
ret
lookup: lookup = makeQuery "g.V()" + has()
property: property = (key, value) -> util.format '.property(%j, %j)', key, value
named: named = (name) -> lookup 'name', name
as: as = (alias) ->
expectString alias
util.format ".as(%j)", alias
create: create = (subjects) ->
for type, instances of subjects
for name, properties of instances
do (type, name, properties) ->
client.post template, type
.then (created) ->
created = new Vertex created
properties.name = name
for key, value of properties
created.property key, value
connect: connect = (a, dir, type, b, props) ->
expectString a, b
q = "g" + named(a) + as('a') + named(b)
q += util.format ".addE(%j).%s('a')", type, dir
if props
for key, value of props
q += property key, value
client.post q
connectTo: connectTo = (a, type, b, props) -> connect a, 'to', type, props
connectFrom: connectFrom = (a, type, b, props) -> connect a, 'from', type, props
list: list = (regex) ->
select =
if regex
(v) -> v.properties.name.filter(({id, value}) -> value.match regex).length
else
-> true
getAll()
.then (data) ->
data.filter select
Object.assign global, module.exports, {Vertex, Property}