Detalhes do pacote

@tpluscode/sparql-builder

tpluscode10kMIT3.0.1

Simple JS library to build SPARQL queries

readme (leia-me)

@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.

changelog (log de mudanças)

Changelog

3.0.1

Patch Changes

  • a4736b6: UNION expression typing did not accept a subquery

3.0.0

Major Changes

  • ae69cd7: Removed built-in logging. Use an optional parameter to execute. Exceptions will be reported as a custom Error subclass

2.0.4

Patch Changes

  • 463ea01: Update anylogger to 1.1-beta

2.0.3

Patch Changes

  • 43e8ce9: Export WithQuery to avoid error like "The inferred type of 'X' cannot be named without a reference to '/node_modules/@tpluscode/sparql-builder/lib/WithBuilder.js'. This is likely not portable. A type annotation is necessary."

2.0.2

Patch Changes

  • c48bd28: Export the *Executable types

2.0.1

Patch Changes

  • 5e451cd: Correct imports to work with moduleResolution = NodeNext

2.0.0

Major Changes

  • b34931c: Updated sparql-http-client to v3

    The .execute method now takes an instance of client and not the query object. Also, since the new client does not return a promise from methods which return streams, await is no longer necessary

1.1.0

Minor Changes

  • 127f72f: No longer depends on debug. Use anylogger-* adapters to receive logs

Patch Changes

  • 5bdc6b0: Relax dependency on @tpluscode/rdf-ns-builders

1.0.1

Patch Changes

  • 1ad0905: Update @tpluscode/rdf-string - improves inference of return types

1.0.0

Major Changes

  • 5b1a043: Package is now ESM-only

0.4.1

Patch Changes

  • 9812437: ESM exported types which failed in runtime

0.4.0

Minor Changes

  • beaf9e1: When interpolating SELECT inside another template, it will automatically be wrapped in a graph pattern

0.3.31

Patch Changes

  • c7cea63: Main module incorrectly exported types as JS

0.3.30

Patch Changes

  • d00f737: Export full builder types of query forms
  • 0f5f895: Added DISTINCT and AND which allow modifying the query after it has been initialised

0.3.29

Patch Changes

  • 66cb644: Allow string as parameter of UNION

0.3.28

Patch Changes

  • e04f163: SPARQL UNION expression builder

0.3.27

Patch Changes

  • ec05a22: Added direct dependency on @types/sparql-http-client

0.3.26

Patch Changes

  • 92e4fcf: Add prefixes parameter to build so ad-hoc prefixes can be applied to a query Re-export prefixes where it's possible to add prefixes globally (re #81)

0.3.25

Patch Changes

  • 9f681a2: Type declaration prevented DELETE.DATA where the interpolated value was quad array

0.3.24

Patch Changes

  • 3f1495e: Adding prologue before the prefixes (closes #75)

0.3.23

Patch Changes

  • 329c89e: Shorthand CONSTRUCT syntax would interpolate templates incorrectly, producing invalid SPARQL

0.3.22

Patch Changes

  • 551f6a1: Updated @tpluscode/rdf-ns-builders to v2

0.3.21

Patch Changes

  • 3231508: Correct syntax for FROM in shorthand CONSTRUCT

0.3.20

Patch Changes

  • 6200b56: Add shorthand form CONSTRUCT WHERE

0.3.19

Patch Changes

  • a4dea43: DESCRIBE query function should support ORDER BY clause

0.3.18

Patch Changes

  • 0e045af: Wrong URLs in package meta

0.3.17

Patch Changes

  • ede2fb5: Export types representing queries which can be executed and built
  • 837447c: Support for GROUP BY and HAVING clauses in SELECT (closes #57)

0.3.16

Patch Changes

  • 3e72cd3: Export a function to create IN function
  • 4afbddb: Builder function for VALUES (closes #4)

0.3.15

Patch Changes

  • 795ff8a: Export SparqlTemplateResult as type

0.3.14

Patch Changes

  • d9a173b: Re-export SPARQL template from @tpluscode/rdf-string
  • 51c985a: Update @tpluscode/rdf-ns-builders and rdf-js types

0.3.13

Patch Changes

0.3.12

Patch Changes

  • 928b9d9: Update @tpluscode/rdf-ns-builders

All notable changes to this project will be documented in this file. See standard-version for commit guidelines.

0.3.11 (2021-02-18)

Features

0.3.10 (2020-11-10)

Features

  • add FROM to ASK and DESCRIBE builders (63ccaea)

0.3.9 (2020-08-08)

Features

  • multiple FROM statements (5fa4f39)

0.3.8 (2020-08-08)

Features

  • add FROM (NAMED) to Construct queries (4a3d662)
  • add FROM NAMED clause builder (2451561)

0.3.7 (2020-08-02)

Features

  • select: added SELECT * shorthand (ae9fdf4)

Bug Fixes

  • select: added more intuitive API for multiple ORDER BY (02a31d1)
  • select: ordering descending was not implemented (4537ca8)

0.3.6 (2020-07-01)

0.3.5 (2020-06-08)

0.3.4 (2020-06-07)

0.3.3 (2020-05-21)

0.3.2 (2020-04-08)

Bug Fixes

  • skip INSERT in DELETE when empty (5f0a734)

0.3.1 (2020-04-01)

Bug Fixes

  • parameters of execute were not correctly inferred (87ed666)

0.3.0 (2020-04-01)

⚠ BREAKING CHANGES

  • changes the signature of execute method

Features

  • update to sparql-http-client-2 (9953317)

0.2.2 (2020-03-02)

Bug Fixes

  • execute: incomplete export prevented the BASE from being applied (7f301a5)
  • execute: the build was not actually called when executing (437a749)

0.2.1 (2020-03-02)

Bug Fixes

  • it's impossible to use BASE with execute (2c43d2b)

0.2.0 (2020-03-02)

⚠ BREAKING CHANGES

  • will potentially break if SparqlQuery<> was imported and not only inferred

Bug Fixes

  • refactor execute to return the correct Response type (928cdbe)

0.1.2 (2020-02-28)

Bug Fixes

  • it was impossible to interpolate another (sub-)query (a060113)

0.1.1 (2020-02-25)

Bug Fixes

  • the ORDER BY clause must come before LIMIT/OFFSET (80f77c6)

0.1.0 (2020-02-24)

⚠ BREAKING CHANGES

  • CONSTRUCT/DESCRIBE now return Response and not a Stream

Features

  • select: support for ORDER BY (6841709)

  • change the graph query API (ccfe4d6)

0.0.10 (2020-02-24)

Bug Fixes

  • base was not applied to nested templates (9ca4bea)

0.0.9 (2020-02-24)

0.0.8 (2020-02-24)

Features

  • support LIMIT/OFFSET in SPARQL Queries (a52cc02)
  • select: support REDUCED and DISTINCT (e3987e8)

0.0.7 (2020-02-23)

Bug Fixes

  • missing question marks when interpolating variables (94e590d)

0.0.6 (2020-02-23)

0.0.5 (2020-02-23)

0.0.4 (2020-02-23)

0.0.3 (2020-02-23)

0.0.2 (2020-02-23)

Features

  • delete: chain multiple INSERT and DELETE calls (d5659dc)
  • delete: chaining multiple DATA calls (c1d3c4c)
  • delete: support DELETE..WHERE updates (6d8dc1f)
  • insert: chain multiple INSERT calls (cc79fed)
  • insert: chaining multiple DATA calls (60d2b50)
  • insert: support INSERT..WHERE updates (e8d254b)
  • where: chaining multiple WHERE calls (ca88d77)

0.0.1 (2020-02-22)

Features

  • moved all existing code from data-cube-curation (71c1212)