# @tpluscode/sparql-builder

> Simple JS library to build SPARQL queries

Latest version **3.0.1** (published 2024-11-15) · MIT license · 0 weekly downloads

## Install

```sh
npm install @tpluscode/sparql-builder
pnpm add @tpluscode/sparql-builder
yarn add @tpluscode/sparql-builder
bun add @tpluscode/sparql-builder
```

## Health

**Score 40/100 (D)** — status: maintenance-mode.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: stale; low maintenance score.

## Facts

| | |
|---|---|
| Version | 3.0.1 |
| Published | 2024-11-15 |
| First published | 2020-02-22 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 5 |
| Unpacked size | 64.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 14 |
| Author | Tomasz Pluskiewicz |
| Maintainers | tpluscode |

## Links

- npm: https://www.npmjs.com/package/@tpluscode/sparql-builder
- Repository: https://github.com/tpluscode/sparql-builder
- Homepage: https://github.com/tpluscode/sparql-builder#readme
- Issues: https://github.com/tpluscode/sparql-builder/issues
- npm.io page: https://npm.io/package/@tpluscode/sparql-builder

## Dependencies (5)

- [@rdfjs/types](https://npm.io/package/@rdfjs/types.md) *
- [@rdfjs/term-set](https://npm.io/package/@rdfjs/term-set.md) ^2
- [@rdfjs/data-model](https://npm.io/package/@rdfjs/data-model.md) ^2
- [@tpluscode/rdf-string](https://npm.io/package/@tpluscode/rdf-string.md) ^1.3.0
- [@types/sparql-http-client](https://npm.io/package/@types/sparql-http-client.md) ^3.0.0

## Recent versions

- 3.0.1 (latest) — 2024-11-15
- 3.0.0 — 2024-10-01
- 2.0.4 — 2024-08-23
- 2.0.3 — 2024-05-06
- 2.0.2 — 2024-03-28
- 2.0.1 — 2024-03-28
- 2.0.0 — 2024-03-21
- 0.3.31 — 2023-07-18
- 1.1.0 — 2023-05-06
- 1.0.1 — 2023-03-26
- 0.4.1 — 2023-03-23
- 1.0.0 — 2023-03-19
- 0.4.0 — 2023-03-16
- 0.3.30 — 2023-03-14
- 0.3.29 — 2023-02-25
- … 45 more at https://npm.io/package/@tpluscode/sparql-builder/versions

## README

> # @tpluscode/sparql-builder ![Test](https://github.com/tpluscode/sparql-builder/workflows/Test/badge.svg) [![codecov](https://codecov.io/gh/tpluscode/sparql-builder/branch/master/graph/badge.svg)](https://codecov.io/gh/tpluscode/sparql-builder) [![npm version](https://badge.fury.io/js/%40tpluscode%2Fsparql-builder.svg)](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: <http://xmlns.com/foaf/0.1/>

    SELECT ?person ?name
    FROM <http://example.com/People>
    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: <http://dbpedia.org/ontology/>

    ASK {
        <http://dbpedia.org/resource/Scooby-Doo> 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`.

---
_Source: https://npm.io/package/@tpluscode/sparql-builder · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
