https://github.com/tpluscode/sparql-builder
Build SPARQL with string templates
https://github.com/tpluscode/sparql-builder
Last synced: 10 months ago
JSON representation
Build SPARQL with string templates
- Host: GitHub
- URL: https://github.com/tpluscode/sparql-builder
- Owner: tpluscode
- Created: 2020-02-19T14:50:12.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2025-03-10T22:33:22.000Z (11 months ago)
- Last Synced: 2025-03-27T14:52:20.886Z (10 months ago)
- Language: TypeScript
- Homepage: https://tpluscode.github.io/sparql-builder/
- Size: 1.98 MB
- Stars: 14
- Watchers: 1
- Forks: 2
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
Awesome Lists containing this project
README
> # @tpluscode/sparql-builder  [](https://codecov.io/gh/tpluscode/sparql-builder) [](https://badge.fury.io/js/%40tpluscode%2Fsparql-builder)
Simple library to create SPARQL queries with tagged ES Template Strings
## How is it different from simply string concatenation/plain templates?
1. Focuses on graph patterns. No need to remember exact syntax
1. Still looks like SPARQL, having a familiar structure and little glue code
1. Has the IDE provide syntactic hints of valid SPARQL keywords
1. Ensures correct formatting of terms (URI nodes, literals variables) via [@tpluscode/rdf-string](https://github.com/tpluscode/rdf-string)
1. Automatically shortens URIs with [`@zazuko/prefixes`](http://npm.im/@zazuko/prefixes)
## Installation
```
npm i -S @tpluscode/sparql-builder
```
To be able to execute queries against a remote endpoint install a peer
dependency:
```
npm i -S sparql-http-client
```
## Usage
### Build a SPARQL query string
```js
import rdf from '@zazuko/env'
import { SELECT } from '@tpluscode/sparql-builder'
const ex = rdf.namespace('http://example.com/')
const { foaf } = rdf.ns
/*
PREFIX foaf:
SELECT ?person ?name
FROM
WHERE
{
?person a foaf:Person ;
foaf:name ?name .
}
*/
const person = rdf.variable('person')
const query =
SELECT`${person} ?name`
.FROM(ex.People)
.WHERE`
${person} a ${foaf.Person} ;
${foaf.name} ?name .
`.build()
```
### Executing a query
Using [`sparql-http-client`](https://github.com/zazuko/sparql-http-client)
```js
import rdf from '@zazuko/env'
import SparqlHttp from 'sparql-http-client'
import { ASK } from '@tpluscode/sparql-builder'
const { dbo } = rdf.ns
const dbr = rdf.namespace('http://dbpedia.org/resource/')
const client = new SparqlHttp({
factory: rdf,
endpointUrl: 'http://dbpedia.org/sparql',
})
const scoobyDoo = dbr('Scooby-Doo')
/*
PREFIX dbo:
ASK {
a dbo:Person .
}
*/
ASK`${scoobyDoo} a ${dbo.Person}`
.execute(client)
.then(isScoobyAPerson => {
// Fun fact: DBpedia seems to claim that Scooby-Doo is indeed a Person...
return isScoobyAPerson
})
```
## Running examples
The example in [`docs/examples`](docs/examples) can be executed locally. To do so, first replace the package import to point to the repository root.
```diff
-const { /* */ } = require('@tpluscode/sparql-builder')
+const { /* */ } = require('../..')
```
Then simply `npm run example`.