Comparing version 0.0.2 to 0.1.0
@@ -10,3 +10,3 @@ const methods = require('./src'); | ||
function getClient(options = defaultOptions) { | ||
const { host, port, namespace } = options; | ||
const { host, port, namespace } = Object.assign({}, defaultOptions, options); | ||
const blazegraphUrl = `http://${host}:${port}/blazegraph/namespace/${namespace}/sparql`; | ||
@@ -13,0 +13,0 @@ const passUrl = fn => (...args) => fn(blazegraphUrl, ...args); |
{ | ||
"name": "blazegraph", | ||
"version": "0.0.2", | ||
"version": "0.1.0", | ||
"description": "Blazegraph JavaScript API", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
const request = require('request'); | ||
const createRdfParser = require('n3').Parser; | ||
const nquadsMimeType = 'text/x-nquads'; | ||
const parser = createRdfParser({ format: 'N-Quads' }); | ||
const trigMimeType = 'application/x-trig; charset=utf-8'; | ||
@@ -52,6 +51,6 @@ /* ---------- | ||
// Serializes a quad into the nquad format (has to be complete) | ||
// Serializes a quad into the trig format (has to be complete) | ||
// NOTE: this middleware enforces the usage of named graph on every triple | ||
function serializeQuad({ subject, predicate, object, graph }) { | ||
return `${subject} ${predicate} ${object} ${graph} .`; | ||
return `${graph} { ${subject} ${predicate} ${object} . }`; | ||
} | ||
@@ -63,3 +62,45 @@ | ||
// Perform a SPARQL query | ||
// NOTE: this does not allow to perform a SPARQL update query | ||
function querySparql(blazegraphUrl, query, includeInferred = false) { | ||
if (!isValidString(query)) return Promise.reject(new Error('Query must be a non-empty string')); | ||
return makeRequest({ | ||
url: `${blazegraphUrl}?query=${encodeQuery(query)}&includeInferred=${!!includeInferred}`, | ||
headers: { | ||
Accept: 'application/json', | ||
}, | ||
}) | ||
.then(body => { | ||
if (typeof body !== 'string') throw new Error('TODO: Learn when this is possible'); | ||
// Can throw ? Can be something else ? | ||
return JSON.parse(body).results.bindings; | ||
}); | ||
} | ||
const resultRegex = /<data result="(\w*)"/; | ||
/* | ||
Returns true is some quads match a pattern | ||
{ | ||
subject?: <IRI> | ||
predicate?: <IRI> | ||
object?: <IRI> or "Literal" | ||
graph?: <IRI> | ||
graphs?: [<IRI>] | ||
} | ||
*/ | ||
function checkPatternExistence(blazegraphUrl, input, includeInferred = false) { | ||
if (isNotObject(input)) return rejectInput(input); | ||
let fullUrl = `${blazegraphUrl}?HASSTMT&${encodeQuad(input)}&includeInferred=${!!includeInferred}`; | ||
if (Array.isArray(input.graphs)) input.graphs.forEach(g => fullUrl += `&c=${g}`); | ||
return makeRequest(fullUrl) | ||
.then(result => result && typeof result === 'string' && resultRegex.exec(result)[1] === 'true'); | ||
} | ||
/* | ||
Read all quads matching a pattern | ||
@@ -74,6 +115,6 @@ { | ||
*/ | ||
function readQuads(blazegraphUrl, input) { | ||
function readQuads(blazegraphUrl, input, includeInferred = false) { | ||
if (isNotObject(input)) return rejectInput(input); | ||
let fullUrl = `${blazegraphUrl}?GETSTMTS&includeInferred=false&${encodeQuad(input)}`; | ||
let fullUrl = `${blazegraphUrl}?GETSTMTS&includeInferred=${!!includeInferred}&${encodeQuad(input)}`; | ||
@@ -86,3 +127,4 @@ if (Array.isArray(input.graphs)) input.graphs.forEach(g => fullUrl += `&c=${g}`); | ||
parser.parse(nquads, (error, triple) => { | ||
if (!nquads) return resolve(quads); | ||
createRdfParser().parse(nquads, (error, triple) => { | ||
if (error) return reject(error); | ||
@@ -115,5 +157,5 @@ if (triple) return quads.push(triple); | ||
headers: { | ||
'Content-Type': nquadsMimeType, | ||
'Content-Type': trigMimeType, | ||
}, | ||
body: inputs.map(serializeQuad).join('\n'), | ||
body: inputs.map(serializeQuad).join(''), | ||
}); | ||
@@ -137,3 +179,3 @@ } | ||
const newQuad = serializeQuad(Object.assign({}, input, { object: input.newObject })); | ||
const options = { contentType: nquadsMimeType }; | ||
const options = { contentType: trigMimeType }; | ||
@@ -201,2 +243,4 @@ return makeRequest({ | ||
module.exports = { | ||
querySparql, | ||
checkPatternExistence, | ||
readQuads, | ||
@@ -203,0 +247,0 @@ createQuads, |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
10602
7
218