Metreeca Wire
    Preparing search index...

    Module @metreeca/wire-sparql - v0.9.0

    SPARQL repository API and data model.

    Provides a single, backend-independent API for working with SPARQL stores. Consumers query and update through the Repository interface and exchange data as the RDF and SPARQL types defined here, regardless of which connector backs the store.

    Vocabularies and formats

    • media — IANA media types for RDF serialisations and SPARQL protocol exchanges
    • rdf — RDF vocabulary namespace
    • xsd — XSD datatype namespace

    Repository

    • Repository — queries and updates an RDF store: ask/select/construct, update, transactional execute, and close

    SPARQL data model

    • SPARQL — query, update, or syntactic fragment as serialised text
    • TupleSELECT query-solution mapping
    • Pattern — triple pattern admitting variables
    • Variable — allocated ?-prefixed SPARQL variable

    RDF data model

    SPARQL type guards

    RDF type guards

    SPARQL factories

    RDF factories

    Utilities

    • skolemize — replace a sequence's blank nodes with minted IRI references

    @metreeca/wire-sparql

    npm

    SPARQL 1.1 query language framework for @metreeca/wire storage connectors.

    Provides a uniform Repository interface for issuing SPARQL queries and updates against any backend (an embedded store, a remote endpoint, or a managed server), without binding application code to a specific store or client. It also bundles the building blocks for working with the data exchanged, such as an RDF term model, DSLs for composing SPARQL queries and RDF graphs, and codecs for RDF serialisations.

    Installation

    Install this framework together with a backend connector for your target engine, plus any peer dependency the connector requires:

    npm install @metreeca/wire-sparql                # the SPARQL query API (this framework)
    npm install @metreeca/wire-sparql-<backend> # backend connector from the table below
    npm install <peer-dependency> # connector's peer dependency, where required
    Warning

    TypeScript consumers must use "moduleResolution": "nodenext"/"node16"/"bundler" in tsconfig.json. The legacy "node" resolver is not supported.

    Connector Package Peer Dependency Engine
    @metreeca/wire-sparql-http - SPARQL 1.1 Protocol endpoint
    @metreeca/wire-sparql-rdf4j - RDF4J REST API endpoint
    @metreeca/wire-sparql-oxigraph oxigraph >=0.5.0 Oxigraph in-memory WASM store

    Usage

    Note

    This section introduces essential concepts; for complete coverage, see the API reference.

    Obtain a Repository from any backend connector, then run SPARQL queries and updates through its methods:

    import { createHTTPRepository } from "@metreeca/wire-sparql-http";

    const repository = createHTTPRepository({
    query: "https://example.org/sparql",
    update: "https://example.org/sparql/statements"
    });

    await repository.update(`
    INSERT DATA { <https://example.org/widget> <https://schema.org/name> "Widget" }
    `);

    const tuples = await repository.select(`
    SELECT ?name { ?product <https://schema.org/name> ?name }
    `); // readonly Tuple[]: each maps the ?name token to a Term

    const present = await repository.ask(`ASK { ?s ?p ?o }`);

    const triples = await repository.construct(`
    CONSTRUCT { ?s ?p ?o } WHERE { ?s ?p ?o }
    `); // readonly Triple[]

    await repository.close();

    Bracket related operations with execute: the task runs on a per-call Repository and commits together, or rolls back if it throws:

    await repository.execute(async tx => {
    await tx.update(`DELETE WHERE { <https://example.org/widget> ?p ?o }`);
    await tx.update(`INSERT DATA { <https://example.org/widget> <https://schema.org/name> "Gadget" }`);
    });
    Important

    Transaction isolation is backend-dependent. A backend with native transactions brackets the task for atomic commit and rollback; one without runs the task directly against the same Repository, with no atomicity or rollback. Each connector documents the level it provides on its factory function.

    A backend connector implements the Repository interface (ask, select, construct, update, execute, and close), lifting native backend nodes into the shared term model with the reference, tagged, and typed constructors, and parsing or serialising RDF payloads with the codecs/ntriples module.

    A minimal connector has the shape:

    import type { Repository, SPARQL } from "@metreeca/wire-sparql";

    function createMyRepository(/* endpoint, client, … */): Repository {

    const repository: Repository = {

    ask: async (query: SPARQL) => { /* run ASK; return the boolean */ },

    select: async (query: SPARQL) => { /* run SELECT; map bindings to Tuples of Terms */ },

    construct: async (query: SPARQL) => { /* run CONSTRUCT; map statements to Triples */ },

    update: async (update: SPARQL) => { /* apply the UPDATE */ },

    execute: async task => task(repository), // or bracket the task in a backend transaction

    close: async () => { /* release resources, or no-op */ }

    };

    return repository;
    }
    Tip

    Each SPARQL connector in the @metreeca/wire monorepo is a complete, working implementation that you can read to learn the approach or adapt for a new engine.

    Support

    • open an issue to report a problem or to suggest a new feature
    • start a discussion to ask a how-to question or to share an idea

    License

    This project is licensed under the Apache 2.0 License – see LICENSE file for details.

    Variables

    media

    IANA media types for RDF serialisations and SPARQL protocol exchanges.

    rdf

    RDF vocabulary namespace.

    xsd

    XSD datatype namespace.

    Type Aliases

    SPARQL

    A SPARQL query, update, or syntactic fragment as serialised text.

    Tuple

    A SPARQL query-solution mapping.

    Pattern

    A SPARQL triple pattern, as a subject/predicate/object statement with optional variables.

    Variable

    An allocated SPARQL variable.

    Graph

    An RDF graph, as a flat sequence of triples.

    Triple

    An RDF triple, as a subject/predicate/object statement.

    Subject

    The subject position of an RDF Triple.

    Predicate

    The predicate position of an RDF Triple.

    Object

    The object position of an RDF Triple.

    Term

    An RDF term.

    Blank

    An RDF blank node.

    Reference

    An IRI reference.

    Tagged

    An RDF language-tagged string, as a lexical-form/language-tag pair.

    Typed

    An RDF datatype-typed literal, as a lexical-form/datatype-IRI pair.

    Interfaces

    Repository

    SPARQL repository.

    Functions

    isTuple

    Checks whether a value is a SPARQL solution Tuple.

    isPattern

    Checks whether a value is a SPARQL triple Pattern.

    isVariable

    Checks whether a value is a SPARQL Variable.

    isGraph

    Checks whether a value is an RDF Graph.

    isTriple

    Checks whether a value is an RDF Triple.

    isSubject

    Checks whether a value is the Subject position of a Triple.

    isPredicate

    Checks whether a value is the Predicate position of a Triple.

    isObject

    Checks whether a value is the Object position of a Triple.

    isTerm

    Checks whether a value is an RDF Term.

    isBlank

    Checks whether a value is an RDF blank node.

    isReference

    Checks whether a value is a Reference.

    isTagged

    Checks whether a value is an RDF language-Tagged string.

    isTyped

    Checks whether a value is an RDF datatype-Typed literal.

    pattern

    Constructs a SPARQL triple Pattern.

    tuple

    Constructs a SPARQL solution Tuple.

    variable

    Constructs a SPARQL Variable.

    graph

    Constructs an RDF Graph.

    triple

    Constructs an RDF Triple.

    blank

    Constructs an RDF blank node.

    reference

    Constructs an IRI Reference.

    tagged

    Constructs a language-Tagged Term.

    typed

    Constructs a datatype-Typed Term.

    skolemize

    Replaces blank nodes in a Triple sequence with minted IRI references.