-
Notifications
You must be signed in to change notification settings - Fork 1
/
datomic-wrapper.coffee
218 lines (197 loc) · 5.96 KB
/
datomic-wrapper.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
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
request = require 'request'
edn = require 'edn'
async = require 'async'
util = require './util'
class Attribute
constructor: (@id, @name, @type, @cardinality)->
class Entity
constructor: (@name)->
@attributes = []
addAttribute: (name)->
@attributes.push name
class DatomicWrapper
constructor: (@datomic_host, @datomic_port, @alias, @db_name)->
log: ->
# console.log.apply console, arguments
base_url: ->
'http://'+@datomic_host+':'+@datomic_port
db_alias: ->
@alias+'/'+@db_name
create_database: (db_name, done)->
req =
url: @base_url()+'/data/'+@alias+'/'
method: 'POST'
headers:
Accept: 'application/edn'
form:
'db-name': db_name
@log 'Creating database: '+db_name
request req, (error, response, body)=>
@log 'Server response:', error, response.statusCode, body
if error
return done error, null
if response.statusCode >= 400
return done body, null
done null, edn.parse body
transact: (data_edn, done)->
req =
url: @base_url()+'/data/'+@db_alias()+'/'
method: 'POST'
headers:
Accept: 'application/edn'
form:
'tx-data': data_edn
@log 'Transacting data: '+data_edn
request req, (error, response, body)=>
@log 'Server response:', error, response.statusCode, body
if error
return done error, null
if response.statusCode >= 400
return done body, null
done null, edn.parse body
query: (query_edn, done)->
req =
url: @base_url()+'/api/query?q='+escape(query_edn)+'&args='+escape('[{:db/alias "'+@db_alias()+'"}]')
method: 'GET'
headers:
Accept: 'application/edn'
@log 'Querying the database:', 'req'
request req, (error, response, body)=>
@log 'Server response:', error, response.statusCode, body
if error
return done error, null
if response.statusCode >= 400
return done body, null
done null, response, body
query_entity: (entity_id, done)->
req =
url: @base_url()+'/data/'+@db_alias()+'/-/entity?e='+entity_id
method: 'GET'
headers:
Accept: 'application/edn'
@log 'Querying entity:', req
request req, (error, response, body)=>
if error
return done error, null
try
@log 'Server response:', error, response.statusCode, body
if error
return done error, null
if response.statusCode >= 400
return done body, null
data = edn.parse body
entity = {}
for key in data.keys
entity[key.name] = util.edn_to_json data.get key
# console.log entity
done null, entity
catch e
done e, null
query_schema: (query_edn, done)->
@query query_edn, (error, response, body)->
parsed = edn.parse(body)
entities = []
parsed.forEach (rec)->
id = rec[0]
entity_name = rec[1].namespace
attr_name = rec[1].name
attr_type = rec[3].name
cardinality = rec[5].name
e = null
for ent in entities
if ent.name == entity_name
e = ent
break
if e == null
e = new Entity entity_name
entities.push e
e.addAttribute new Attribute id, attr_name, attr_type, cardinality
done null, entities
schemas_all: (done)->
@query_schema '
[:find ?e ?ident ?vt ?vt_ident ?card ?card_ident
:in $
:where
[?e :db/ident ?ident]
[?e :db/valueType ?vt]
[?vt :db/ident ?vt_ident]
[?e :db/cardinality ?card]
[?card :db/ident ?card_ident]
]', done
create_schema: (entity_schema, done)->
# console.log entity_schema
entity_name = entity_schema.name
data = []
for attr in entity_schema.attributes
data.push edn.map [
edn.keyword 'db/id'
edn.generic 'db/id', [edn.keyword 'db.part/db']
edn.keyword 'db/ident'
edn.keyword entity_name + '/' + attr.name
edn.keyword 'db/valueType'
edn.keyword 'db.type/' + attr.type
edn.keyword 'db/cardinality'
edn.keyword 'db.cardinality/' + attr.cardinality
edn.keyword 'db.install/_attribute'
edn.keyword 'db.part/db'
]
@transact edn.stringify(data), (result)->
done()
get_schema: (entity_name, done)->
@schemas_all (err, entities)->
for ent in entities
if ent.name == entity_name
return done(null, ent)
done new Error 'Entity with name "'+entity_name+'" not found'
rest_index: (entity_name, done)->
@get_schema entity_name, (err, entity_schema)=>
query_ids_for_attribute = (attr, done)=>
@query '[:find ?c :where [?c :'+entity_name+'/'+attr.name+']]', (error, response, body)->
done error, edn.parse(body).map (row)->
row[0]
query_entity_with_id = (id, done)=>
@query_entity id, (error, entity)->
done error, entity
async.map entity_schema.attributes, query_ids_for_attribute, (err, results)=>
# console.log 'async map:', err, results
ids = results.reduce((a,b)-> a.concat b).filter((el,i,a)->i==a.indexOf el)
# console.log 'ids:', ids
async.map ids, query_entity_with_id, (err, results)=>
# console.log 'err:', err
# console.log 'results:', results
done err, results
rest_get: (entity_name, id, done)->
@query_entity id, (error, entity)->
done error, entity
create_entity: (entity_name, attributes, done)->
data = edn.Map()
data.set edn.keyword('db/id'), edn.generic('db/id', [edn.keyword 'db.part/user'])
for key, value of attributes
data.set edn.keyword(entity_name+'/'+key), value
data = edn.stringify [data]
@transact data, (err, result)->
# console.log 'err:', err
# console.log result
if err
done err, null
else
id = result.get(edn.keyword 'tempids').values[0]
res = util.extend {id: id}, attributes
done null, res
update_entity: (entity_name, id, attributes, done)->
data = edn.Map()
data.set edn.keyword('db/id'), edn.generic('db/id', [edn.keyword('db.part/user'), id])
for key, value of attributes
if key != 'id'
data.set edn.keyword(entity_name+'/'+key), value
data = edn.stringify [data]
@transact data, (err, result)->
# console.log 'err:', err
# console.log result
if err
done err, null
else
done null, attributes
module.exports.Attribute = Attribute
module.exports.Entity = Entity
module.exports.DatomicWrapper = DatomicWrapper