forked from levelgraph/levelgraph-jsonld
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
472 lines (416 loc) · 15.5 KB
/
index.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
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
465
466
467
468
469
470
471
472
var jsonld = require('jsonld'),
uuid = require('uuid'),
RDFTYPE = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type',
RDFFIRST = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first',
RDFREST = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest',
RDFNIL = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#nil',
RDFLANGSTRING = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#langString',
XSDTYPE = 'http://www.w3.org/2001/XMLSchema#',
async = require('async'),
N3Util = require('n3/lib/N3Util'); // with browserify require('n3').Util would bundle more then needed!
function levelgraphJSONLD(db, jsonldOpts) {
if (db.jsonld) {
return db;
}
var graphdb = Object.create(db);
jsonldOpts = jsonldOpts || {};
jsonldOpts.base = jsonldOpts.base || '';
graphdb.jsonld = {
options: jsonldOpts
};
function doPut(obj, options, callback) {
// holds accumulated blank node identifiers
// example {"_:b0": "_:...uuid..."}
var blanks = {};
// the inverse of the above object for speady look-up
// example {"_:...uuid...": "_:b0"}
var blanks_inverted = {};
jsonld.expand(obj, function(err, expanded) {
if (err) {
return callback && callback(err);
}
if (options.base) {
if (expanded['@context']) {
expanded['@context']['@base'] = options.base;
} else {
expanded['@context'] = { '@base' : options.base };
}
}
jsonld.toRDF(expanded, options, function(err, triples) {
if (err || triples.length === 0) {
return callback(err, null);
}
var stream = graphdb.putStream();
stream.on('error', callback);
stream.on('close', function() {
if (options.blank_ids) {
// return rdf store scoped blank nodes
var blank_keys = Object.keys(blanks);
function framify(o) {
if (Array.isArray(o)) {
return o.map(framify)
} else if (typeof o == 'object') {
var clone_o = {}
Object.keys(o).forEach(function(key) {
if (Array.isArray(o[key]) && key != "@type") {
clone_o[key] = framify(o[key][0])
} else if (!Array.isArray(o[key]) && typeof o[key] === "object") {
clone_o[key] = framify(o[key])
} else {
clone_o[key] = framify(o[key])
}
})
return clone_o
} else {
return o
}
}
var frame = framify(obj)
if (blank_keys.length != 0) {
jsonld.frame(obj, frame, function(err, framed) {
if (err) {
return callback(err, null);
}
var framed_string = JSON.stringify(framed);
blank_keys.forEach(function(blank) {
framed_string = framed_string.replace(blank,blanks[blank])
})
var ided = JSON.parse(framed_string);
if (ided["@graph"].length == 1) {
var clean_reframe = Object.assign({}, { "@context": ided["@context"]}, ided["@graph"][0]);
return callback(null, clean_reframe);
} else if (ided["@graph"].length > 1) {
return callback(null, ided);
} else {
// Could not reframe the input, returning the original object
return callback(null, obj);
}
})
} else {
return callback(null, obj);
}
} else {
return callback(null, obj);
}
});
triples['@default'].map(function(triple) {
return ['subject', 'predicate', 'object'].reduce(function(acc, key) {
var node = triple[key];
// generate UUID to identify blank nodes
// uses type field set to 'blank node' by jsonld.js toRDF()
if (node.type === 'blank node') {
if (!(node.value in blanks)
// avoid adding newly generated blank node identifiers
&& !(node.value in blanks_inverted)) {
var bnode = '_:' + uuid.v1();
blanks[node.value] = bnode;
blanks_inverted[bnode] = node.value;
}
if (!(node.value in blanks_inverted)) {
// we've not seen this one before, so add it to the node
node.value = blanks[node.value];
}
}
// preserve object data types using double quotation for literals
// and don't keep data type for strings without defined language
if(key === 'object' && triple.object.datatype){
if(triple.object.datatype.match(XSDTYPE)){
if(triple.object.datatype === 'http://www.w3.org/2001/XMLSchema#string'){
node.value = '"' + triple.object.value + '"';
} else {
node.value = '"' + triple.object.value + '"^^' + triple.object.datatype;
}
} else if(triple.object.datatype.match(RDFLANGSTRING)){
node.value = '"' + triple.object.value + '"@' + triple.object.language;
} else {
node.value = '"' + triple.object.value + '"^^' + triple.object.datatype;
}
}
acc[key] = node.value;
return acc;
}, {});
}).forEach(function(triple) {
stream.write(triple);
});
stream.end();
});
});
}
function doDel(obj, options, callback) {
var blanks = {};
jsonld.expand(obj, options, function(err, expanded) {
if (err) {
return callback && callback(err);
}
var stream = graphdb.delStream();
stream.on('close', callback);
stream.on('error', callback);
if (options.base) {
if (expanded['@context']) {
expanded['@context']['@base'] = options.base;
} else {
expanded['@context'] = { '@base' : options.base };
}
}
jsonld.toRDF(expanded, options, function(err, triples) {
if (err || triples.length === 0) {
return callback(err, null);
}
triples['@default'].map(function(triple) {
return ['subject', 'predicate', 'object'].reduce(function(acc, key) {
var node = triple[key];
// mark blank nodes to skip deletion as per https://www.w3.org/TR/ldpatch/#Delete-statement
// uses type field set to 'blank node' by jsonld.js toRDF()
if (node.type === 'blank node') {
if (!blanks[node.value]) {
blanks[node.value] = '_:';
}
node.value = blanks[node.value];
}
// preserve object data types using double quotation for literals
// and don't keep data type for strings without defined language
if(key === 'object' && triple.object.datatype){
if(triple.object.datatype.match(XSDTYPE)){
if(triple.object.datatype === 'http://www.w3.org/2001/XMLSchema#string'){
node.value = '"' + triple.object.value + '"';
} else {
node.value = '"' + triple.object.value + '"^^' + triple.object.datatype;
}
} else if(triple.object.datatype.match(RDFLANGSTRING)){
node.value = '"' + triple.object.value + '"@' + triple.object.language;
} else {
node.value = '"' + triple.object.value + '"^^' + triple.object.datatype;
}
}
acc[key] = node.value;
return acc;
}, {});
}).forEach(function(triple) {
// Skip marked blank nodes.
if (triple.subject.indexOf('_:') !== 0 && triple.object.indexOf('_:') !== 0) {
stream.write(triple);
}
});
stream.end();
});
})
}
function doCut(obj, options, callback) {
var iri = obj;
if (typeof obj !=='string') {
iri = obj['@id'];
}
if (iri === undefined) {
return callback && callback(null);
}
var stream = graphdb.delStream();
stream.on('close', callback);
stream.on('error', callback);
(function delAllTriples(iri, done) {
graphdb.get({ subject: iri }, function(err, triples) {
async.each(triples, function(triple, cb) {
stream.write(triple);
if (triple.object.indexOf('_:') === 0 || (options.recurse && N3Util.isIRI(triple.object))) {
delAllTriples(triple.object, cb);
} else {
cb();
}
}, done);
});
})(iri, function(err) {
if (err) {
return callback(err);
}
stream.end();
});
}
graphdb.jsonld.put = function(obj, options, callback) {
if (typeof obj === 'string') {
obj = JSON.parse(obj);
}
if (typeof options === 'function') {
callback = options;
options = {};
}
options.base = options.base || this.options.base;
options.overwrite = options.overwrite !== undefined ? options.overwrite : ( this.options.overwrite !== undefined ? this.options.overwrite : false );
if (!options.overwrite) {
doPut(obj, options, callback);
} else {
graphdb.jsonld.del(obj, options, function(err) {
if (err) {
return callback && callback(err);
}
});
doPut(obj, options, callback);
}
};
graphdb.jsonld.del = function(obj, options, callback) {
if (typeof options === 'function') {
callback = options;
options = {};
}
options.cut = options.cut !== undefined ? options.cut : ( this.options.cut !== undefined ? this.options.cut : false );
options.recurse = options.recurse !== undefined ? options.recurse : ( this.options.recurse !== undefined ? this.options.recurse : false );
if (typeof obj === 'string') {
try {
obj = JSON.parse(obj);
} catch (e) {
if (N3Util.isIRI(obj) && !options.cut) {
callback(new Error("Passing an IRI to del is not supported anymore. Please pass a JSON-LD document."))
}
}
}
if (!options.cut) {
doDel(obj, options, callback)
} else {
doCut(obj, options, callback)
}
};
graphdb.jsonld.cut = function(obj, options, callback) {
if (typeof options === 'function') {
callback = options;
options = {};
}
options.recurse = options.recurse || this.options.recurse || false;
doCut(obj, options, callback);
}
// http://json-ld.org/spec/latest/json-ld-api/#data-round-tripping
function getCoercedObject(object) {
var TYPES = {
PLAIN: 'http://www.w3.org/1999/02/22-rdf-syntax-ns#PlainLiteral',
BOOLEAN: XSDTYPE + 'boolean',
INTEGER: XSDTYPE + 'integer',
DOUBLE: XSDTYPE + 'double',
STRING: XSDTYPE + 'string',
};
var value = N3Util.getLiteralValue(object);
var type = N3Util.getLiteralType(object);
var coerced = {};
switch (type) {
case TYPES.STRING:
case TYPES.PLAIN:
coerced['@value'] = value;
break;
case RDFLANGSTRING:
coerced['@value'] = value;
coerced['@language'] = N3Util.getLiteralLanguage(object);
break;
case TYPES.INTEGER:
coerced['@value'] = parseInt(value, 10);
break;
case TYPES.DOUBLE:
coerced['@value'] = parseFloat(value);
break;
case TYPES.BOOLEAN:
if (value === 'true' || value === '1') {
coerced['@value'] = true;
} else if (value === 'false' || value === '0') {
coerced['@value'] = false;
} else {
throw new Error('value not boolean!');
}
break;
default:
coerced = { '@value': value, '@type': type };
}
return coerced;
}
function fetchExpandedTriples(iri, memo, callback) {
if (typeof memo === 'function') {
callback = memo;
memo = {};
}
graphdb.get({ subject: iri }, function(err, triples) {
if (err || triples.length === 0) {
return callback(err, null);
}
async.reduce(triples, memo, function(acc, triple, cb) {
var key;
if (!acc[triple.subject] && !N3Util.isBlank(triple.subject)) {
acc[triple.subject] = { '@id': triple.subject };
} else if (N3Util.isBlank(triple.subject) && !acc[triple.subject]) {
acc[triple.subject] = {};
}
if (triple.predicate === RDFTYPE) {
if (acc[triple.subject]['@type']) {
acc[triple.subject]['@type'].push(triple.object);
} else {
acc[triple.subject]['@type'] = [triple.object];
}
return cb(null, acc);
} else if (!N3Util.isBlank(triple.object)) {
var object = {};
if (N3Util.isIRI(triple.object)) {
object['@id'] = triple.object;
} else if (N3Util.isLiteral(triple.object)) {
object = getCoercedObject(triple.object);
}
if(object['@id']) {
// expanding object iri
fetchExpandedTriples(triple.object, function(err, expanded) {
if (!acc[triple.subject][triple.predicate]) acc[triple.subject][triple.predicate] = [];
if (expanded !== null) {
acc[triple.subject][triple.predicate].push(expanded[triple.object]);
} else {
acc[triple.subject][triple.predicate].push(object);
}
return cb(err, acc);
});
}
else if (Array.isArray(acc[triple.subject][triple.predicate])){
acc[triple.subject][triple.predicate] = acc[triple.subject][triple.predicate].concat(object);
return cb(err, acc);
} else {
acc[triple.subject][triple.predicate] = Array.isArray(object) ? object : [object];
return cb(err, acc);
}
} else {
// deal with blanks
fetchExpandedTriples(triple.object, function(err, expanded) {
if (!acc[triple.subject][triple.predicate]) acc[triple.subject][triple.predicate] = [];
if (expanded !== null) {
acc[triple.subject][triple.predicate] = acc[triple.subject][triple.predicate].concat(expanded[triple.object]);
} else {
acc[triple.subject][triple.predicate] = acc[triple.subject][triple.predicate].concat(object);
}
return cb(err, acc);
});
}
}, function (err, nodes) {
if (err) return callback(err)
if (nodes[iri][RDFFIRST]) {
var list = { '@list': gatherList(nodes[iri]) }
nodes[iri] = list
}
callback(null, nodes)
})
function gatherList(node) {
var list = []
list = list.concat(node[RDFFIRST])
if (node[RDFREST]) {
if (node[RDFREST][0] && node[RDFREST][0]['@list']) {
list = list.concat(node[RDFREST][0]['@list'])
} else if (!(node[RDFREST][0] && node[RDFREST][0]['@id'] && node[RDFREST][0]['@id'] == RDFNIL)) {
list = list.concat(node[RDFREST])
}
}
return list
}
});
}
graphdb.jsonld.get = function(iri, context, options, callback) {
if (typeof options === 'function') {
callback = options;
options = {};
}
fetchExpandedTriples(iri, function(err, expanded) {
if (err || expanded === null) {
return callback(err, expanded);
}
jsonld.compact(expanded[iri], context, options, callback);
});
};
return graphdb;
}
module.exports = levelgraphJSONLD;