Just a simple, zero-dependency library for creating SPARQL queries.
This library was made in order to speed up development process when creation of SPARQL queries is needed.
SELECT Query- CONSTRUCT Query
- UPDATE Query
npm i @vterzic/sparql-builder
Examples can also be found in ./test/select-query.spec.ts
SELECT * WHERE {
?s ?p ?o .
}
const { builder } = require('@vterzic/sparql-builder');
const q = new Select()
.select('*')
.where('s', 'p', 'o');
q.render();
SELECT ?s ?p WHERE {
?s ?p ?o .
}
const { builder } = require('@vterzic/sparql-builder');
const q = builder.selectQuery()
.select('s', 'p')
.where('s', 'p', 'o');
q.render();
SELECT * WHERE {
?s ?p ?o .
OPTIONAL {
?o ?p2 ?o2 .
?o ?p3 ?o3 .
}
}
const { builder } = require('@vterzic/sparql-builder');
const q = builder.selectQuery()
.select('*')
.where('s', 'p', 'o')
.optional(
builder
.optional()
.where('o', 'p2', 'o2')
.where('o', 'p3', 'o3')
);
q.render();
SELECT ?s ?p WHERE {
?s ?p ?o .
} OFFSET 5 LIMIT 10
const { builder } = require('@vterzic/sparql-builder');
const q = builder.selectQuery()
.select('s', 'p')
.where('s', 'p', 'o')
.offset(5)
.limit(10);
q.render();
SELECT ?location (SUM(?amount) as ?totalAmount) WHERE {
?location schema:amount ?amount .
} GROUP BY ?location HAVING(?totalAmount > 10) OFFSET 5 LIMIT 10
const { builder, operators: op } = require('@vterzic/sparql-builder');
const q = builder.selectQuery()
.prefix(op.prefix('schema', op.iri('http://schema.org/')))
.select('location', op.sum('?amount', 'totalAmount'))
.where('location', 'schema:amount', '?amount')
.groupBy('location')
.having('?totalAmount > 10')
.offset(5)
.limit(10);
q.render();
SELECT * WHERE {
?s ?p ?o .
} ORDER BY DESC(?p) ASC(?o)
const { builder, operators: op } = require('@vterzic/sparql-builder');
const q = builder.selectQuery()
.select('*')
.where('s', 'p', 'o')
.orderBy(op.desc('p'))
.orderBy(op.asc('o'));
PREFIX schema: <http://schema.org/>
SELECT * WHERE {
{
SELECT ?s WHERE {
BIND ('sparql-builder' as ?varName) .
?s schema:name ?varName .
}
}
}
const { builder, operators: op } = require('@vterzic/sparql-builder');
const q = const q = builder.selectQuery()
.prefix(op.prefix('schema', 'http://schema.org/'))
.select('*')
.nest(
builder.selectQuery()
.bind(op.toStringLiteral('sparql-builder'), 'varName')
.select('s')
.where('s', 'schema:name', 'varName'),
);
q.render();
const { builder } = require('@vterzic/sparql-builder');
PREFIX schema: <http://schema.org/>
SELECT * WHERE {
{
SELECT ?s WHERE {
?s schema:name 'example1' .
}
}
UNION
{
SELECT ?s WHERE {
?s schema:lastName 'example2' .
}
}
}
const { builder, operators: op } = require('@vterzic/sparql-builder');
const subQuery1 = builder.selectQuery()
.select('s')
.where('s', 'schema:name', op.toStringLiteral('example1'));
const subQuery2 = builder.selectQuery()
.select('s')
.where('s', 'schema:lastName', op.toStringLiteral('example2'));
const q = builder.selectQuery()
.prefix(op.prefix('schema', 'http://schema.org/'))
.select('*')
.nest(subQuery1)
.nest(subQuery2, 'UNION');
q.render();
PREFIX schema: <http://schema.org/>
SELECT * WHERE {
{
SELECT ?s WHERE {
?s schema:name 'example1' .
}
}
OPTIONAL {
SELECT ?s WHERE {
?s schema:lastName 'example2' .
}
}
}
const { builder, operators: op } = require('@vterzic/sparql-builder');
const subQuery1 = new SelectQuery()
.select('s')
.where('s', 'schema:name', op.toStringLiteral('example1'));
const subQuery2 = new SelectQuery()
.select('s')
.where('s', 'schema:lastName', op.toStringLiteral('example2'));
const q = new SelectQuery()
.prefix(op.prefix('schema', 'http://schema.org/'))
.select('*')
.nest(subQuery1)
.nest(subQuery2, 'OPTIONAL');
q.redner();