Git Product home page Git Product logo

fetch-sparql-endpoint.js's Introduction

Fetch SPARQL Endpoint

Build Status Coverage Status npm version

A simple, lightweight module to send queries to SPARQL endpoints and retrieve their results in a streaming fashion.

All results are compatible with the RDFJS specification.

Currently, SPARQL queries such as SELECT, ASK, CONSTRUCT and DESCRIBE are supported. SPARQL UPDATE to insert, delete and patch data is not supported yet.

Internally, this library supports SPARQL results in SPARQL JSON, SPARQL XML, and Turtle.

Install

This package can be installed via npm.

$ npm install fetch-sparql-endpoint

This package also works out-of-the-box in browsers via tools such as webpack and browserify.

Usage

API

Create a new fetcher

import {SparqlEndpointFetcher} from "fetch-sparql-endpoint";

const myFetcher = new SparqlEndpointFetcher();

Optionally, you can pass an options object with the following optional entries:

const myFetcher = new SparqlEndpointFetcher({
  fetch: fetch,                     // A custom fetch-API-supporting function
  dataFactory: DataFactory,         // A custom RDFJS data factory
  prefixVariableQuestionMark: false // If variable names in bindings should be prefixed with '?', defaults to false
});

Fetch bindings

SPARQL SELECT queries returns a (promise to a) stream of bindings.

const bindingsStream = await fetcher.fetchBindings('https://dbpedia.org/sparql', 'SELECT * WHERE { ?s ?p ?o } LIMIT 100');
bindingsStream.on('data', (bindings) => console.log(bindings));

This will output bindings in the following form, where keys correspond to variables in the queries, and values and RDFJS terms:

{ s: namedNode('s1'), p: namedNode('p1'), o: namedNode('o1') }
{ s: namedNode('s2'), p: namedNode('p2'), o: namedNode('o2') }
{ s: namedNode('s3'), p: namedNode('p3'), o: namedNode('o3') }
...

Fetch ask

SPARQL ASK queries answer with a (promise to a) boolean value.

const answer = await fetcher.fetchAsk('https://dbpedia.org/sparql', 'ASK WHERE { ?s ?p ?o }');

This will output true or false.

Fetch triples

SPARQL CONSTRUCT and SPARQL DESCRIBE queries returns a (promise to a) stream of triples.

const tripleStream = await fetcher.fetchTriples('https://dbpedia.org/sparql', 'CONSTRUCT { ?s ?p ?o } LIMIT 100');
tripleStream.on('data', (triple) => console.log(triple));

This will output RDFJS triples as follows:

triple(namedNode('s1'), namedNode('p1'), namedNode('o1'))
triple(namedNode('s2'), namedNode('p2'), namedNode('o2'))
triple(namedNode('s3'), namedNode('p3'), namedNode('o3'))
...

Detect query type

If you want to know the query type in order to determine which of the above fetch methods to call, then you can use the getQueryType method as follows:

fetcher.getQueryType('SELECT * WHERE { ?s ?p ?o } LIMIT 100'); // Outputs 'SELECT'
fetcher.getQueryType('ASK WHERE { ?s ?p ?o }');                // Outputs 'ASK'
fetcher.getQueryType('CONSTRUCT { ?s ?p ?o } LIMIT 100');      // Outputs 'CONSTRUCT'

This method will also throw an error if the query contains a syntax error.

Command-line

A command-line tool is provided to quickly query any SPARQL endpoint:

Usage:

$ fetch-sparql-endpoint https://dbpedia.org/sparql [-q] 'SELECT * WHERE { ?s ?p ?o } LIMIT 100'
$ fetch-sparql-endpoint https://dbpedia.org/sparql -f query.sparql
$ cat query.sparql | fetch-sparql-endpoint https://dbpedia.org/sparql

License

This software is written by Ruben Taelman.

This code is released under the MIT license.

fetch-sparql-endpoint.js's People

Contributors

rubensworks avatar greenkeeper[bot] avatar renovate[bot] avatar renovate-bot avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.