forked from zazuko/kopflos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStoreResourceLoader.js
56 lines (45 loc) · 1.29 KB
/
StoreResourceLoader.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
const clownface = require('clownface')
const ns = require('@tpluscode/rdf-ns-builders')
const { fromStream, toStream } = require('rdf-dataset-ext')
const rdf = { ...require('@rdfjs/data-model'), ...require('@rdfjs/dataset') }
const TermSet = require('@rdfjs/term-set')
class StoreResourceLoader {
constructor ({ store }) {
this.store = store
}
async load (term) {
const dataset = await fromStream(rdf.dataset(), this.store.match(null, null, null, term))
if (dataset.size === 0) {
return null
}
const types = new TermSet(clownface({ dataset, term }).out(ns.rdf.type).terms)
return {
term,
prefetchDataset: dataset,
async dataset () {
return dataset
},
quadStream () {
return toStream(dataset)
},
types
}
}
async forClassOperation (term) {
const resource = await this.load(term)
return resource ? [resource] : []
}
async forPropertyOperation (term) {
const dataset = await fromStream(rdf.dataset(), this.store.match(null, null, term, null))
const result = []
for (const quad of dataset) {
result.push({
property: quad.predicate,
object: quad.object,
...await this.load(quad.subject)
})
}
return result
}
}
module.exports = StoreResourceLoader