3.0.1 • Published 1 month ago

@tpluscode/sparql-builder v3.0.1

Weekly downloads
1,014
License
MIT
Repository
github
Last release
1 month ago

@tpluscode/sparql-builder Test codecov npm version

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
  2. Still looks like SPARQL, having a familiar structure and little glue code
  3. Has the IDE provide syntactic hints of valid SPARQL keywords
  4. Ensures correct formatting of terms (URI nodes, literals variables) via @tpluscode/rdf-string
  5. Automatically shortens URIs with @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

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

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 can be executed locally. To do so, first replace the package import to point to the repository root.

-const { /* */ } = require('@tpluscode/sparql-builder')
+const { /* */ } = require('../..')

Then simply npm run example.

3.0.1

1 month ago

3.0.0

3 months ago

2.0.4

4 months ago

2.0.3

8 months ago

2.0.2

9 months ago

2.0.1

9 months ago

2.0.0

9 months ago

0.3.31

1 year ago

1.1.0

2 years ago

1.0.1

2 years ago

0.3.30

2 years ago

1.0.0

2 years ago

0.4.1

2 years ago

0.4.0

2 years ago

0.3.29

2 years ago

0.3.28

2 years ago

0.3.27

2 years ago

0.3.26

2 years ago

0.3.25

2 years ago

0.3.24

2 years ago

0.3.23

2 years ago

0.3.22

2 years ago

0.3.20

3 years ago

0.3.21

3 years ago

0.3.19

3 years ago

0.3.17

3 years ago

0.3.18

3 years ago

0.3.16

3 years ago

0.3.15

3 years ago

0.3.14

4 years ago

0.3.13

4 years ago

0.3.12

4 years ago

0.3.11

4 years ago

0.3.10

4 years ago

0.3.9

4 years ago

0.3.8

4 years ago

0.3.7

4 years ago

0.3.6

4 years ago

0.3.5

5 years ago

0.3.4

5 years ago

0.3.3

5 years ago

0.3.2

5 years ago

0.3.0

5 years ago

0.3.1

5 years ago

0.2.2

5 years ago

0.2.1

5 years ago

0.2.0

5 years ago

0.1.2

5 years ago

0.1.1

5 years ago

0.0.10

5 years ago

0.1.0

5 years ago

0.0.9

5 years ago

0.0.8

5 years ago

0.0.7

5 years ago

0.0.6

5 years ago

0.0.5

5 years ago

0.0.4

5 years ago

0.0.3

5 years ago

0.0.2

5 years ago

0.0.1

5 years ago