Skip to content

Commit a3f8a77

Browse files
committed
Refactor SPARQL update patcher with promises.
This improves reuse for future parsers.
1 parent c92c6ad commit a3f8a77

File tree

1 file changed

+25
-15
lines changed

1 file changed

+25
-15
lines changed

lib/handlers/patch/sparql-update-patcher.js

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,29 +8,39 @@ const error = require('../../http-error')
88

99
// Patches the given graph
1010
function patch (targetKB, targetURI, patchText) {
11+
const patchKB = $rdf.graph()
12+
const target = patchKB.sym(targetURI)
13+
14+
// Must parse relative to document's base address but patch doc should get diff URI
15+
// @@@ beware the triples from the patch ending up in the same place
16+
const patchURI = targetURI
17+
18+
return parsePatchDocument(patchURI, patchText, patchKB)
19+
.then(patchObject => applyPatch(patchObject, target, targetKB))
20+
}
21+
22+
// Parses the given SPARQL UPDATE document
23+
function parsePatchDocument (patchURI, patchText, patchKB) {
24+
debug('PATCH -- Parsing patch...')
1125
return new Promise((resolve, reject) => {
12-
// Parse the patch document
13-
debug('PATCH -- Parsing patch...')
14-
const patchURI = targetURI // @@@ beware the triples from the patch ending up in the same place
15-
const patchKB = $rdf.graph()
16-
var patchObject
1726
try {
18-
// Must parse relative to document's base address but patch doc should get diff URI
19-
patchObject = $rdf.sparqlUpdateParser(patchText, patchKB, patchURI)
20-
} catch (e) {
21-
return reject(error(400, 'Patch format syntax error:\n' + e + '\n'))
27+
resolve($rdf.sparqlUpdateParser(patchText, patchKB, patchURI))
28+
} catch (err) {
29+
reject(error(400, 'Patch format syntax error:\n' + err + '\n'))
2230
}
23-
debug('PATCH -- reading target file ...')
31+
})
32+
}
2433

25-
// Apply the patch to the target graph
26-
var target = patchKB.sym(targetURI)
27-
targetKB.applyPatch(patchObject, target, function (err) {
34+
// Applies the patch to the target graph
35+
function applyPatch (patchObject, target, targetKB) {
36+
return new Promise((resolve, reject) =>
37+
targetKB.applyPatch(patchObject, target, (err) => {
2838
if (err) {
29-
var message = err.message || err // returns string at the moment
39+
const message = err.message || err // returns string at the moment
3040
debug('PATCH FAILED. Returning 409. Message: \'' + message + '\'')
3141
return reject(error(409, 'Error when applying the patch'))
3242
}
3343
resolve(targetKB)
3444
})
35-
})
45+
)
3646
}

0 commit comments

Comments
 (0)