@tpluscode/sparql-builder
Version:
Simple JS library to build SPARQL queries
34 lines (33 loc) • 1.13 kB
JavaScript
import TermSet from '@rdfjs/term-set';
import { sparql } from '@tpluscode/rdf-string';
export default function () {
const builder = {
defaultGraph: new TermSet(),
fromNamed: new TermSet(),
fromClause() {
const clause = [...this.defaultGraph.values()].reduce((current, graph) => sparql `${current}\nFROM ${graph}`, sparql ``);
return [...this.fromNamed.values()].reduce((current, graph) => sparql `${current}\nFROM NAMED ${graph}`, clause);
},
};
return {
...builder,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
FROM(defaultGraph) {
if (!defaultGraph) {
return {
NAMED: (graph) => {
this.fromNamed.add(graph);
return this;
},
};
}
if (defaultGraph.termType === 'DefaultGraph') {
this.defaultGraph.clear();
}
else {
this.defaultGraph.add(defaultGraph);
}
return this;
},
};
}