@tpluscode/sparql-builder
Version:
Simple JS library to build SPARQL queries
61 lines (60 loc) • 1.64 kB
JavaScript
import { sparql } from '@tpluscode/rdf-string';
import { select } from './execute.js';
import WHERE from './partials/WHERE.js';
import LIMIT from './partials/LIMIT.js';
import ORDER from './partials/ORDER.js';
import FROM from './partials/FROM.js';
import GROUP from './partials/GROUP.js';
import HAVING from './partials/HAVING.js';
import Builder from './index.js';
const SelectBuilder = (strings, ...values) => ({
...Builder('SELECT'),
...select,
...WHERE({
required: true,
}),
...LIMIT(),
...ORDER(),
...GROUP(),
...HAVING(),
...FROM(),
DISTINCT() {
return {
...this,
distinct: true,
};
},
AND(strings, ...values) {
return {
...this,
variables: sparql `${this.variables}${sparql(strings, ...values)}`,
};
},
distinct: false,
reduced: false,
variables: sparql(strings, ...values),
_getTemplateResult() {
const modifier = this.distinct ? 'DISTINCT ' : this.reduced ? 'REDUCED ' : '';
return sparql `SELECT ${modifier}${this.variables}
${this.fromClause()}
${this.whereClause()}
${this.orderClause()}
${this.groupByClause()}
${this.havingClause()}
${this.limitOffsetClause()}`;
},
});
SelectBuilder.DISTINCT = (strings, ...values) => ({
...SelectBuilder(strings, ...values),
distinct: true,
});
SelectBuilder.REDUCED = (strings, ...values) => ({
...SelectBuilder(strings, ...values),
reduced: true,
});
Object.defineProperty(SelectBuilder, 'ALL', {
get() {
return SelectBuilder `*`;
},
});
export const SELECT = SelectBuilder;