Querying over RDF/JS sources


On this page

    One of the different types of sources that is supported by Comunica is the RDF/JS Source interface. This allows you to pass objects as source to Comunica as long as they implement this interface.

    An RDF/JS Source exposes the match method that allows quad pattern queries to be executed, and matching quads to be returned as a stream.

    Learn more about RDF/JS in this RDF/JS guide.

    Several implementations of this Source interface exist. In the example below, we make use of the Store from N3.js that offers one possible implementation when you want to query over it with Comunica within a JavaScript application:

    const store = new N3.Store();
    store.addQuad(
      namedNode('http://ex.org/Pluto'),
      namedNode('http://ex.org/type'),
      namedNode('http://ex.org/Dog')
    );
    store.addQuad(
      namedNode('http://ex.org/Mickey'),
      namedNode('http://ex.org/type'),
      namedNode('http://ex.org/Mouse')
    );
    
    const bindingsStream = await myEngine.queryBindings(`SELECT * WHERE { ?s ?p ?o }`, {
      sources: [store],
    });
    
    Instead of the default Comunica SPARQL package (@comunica/query-sparql), the Comunica SPARQL RDF/JS (@comunica/query-sparql-rdfjs) can also be used as a more lightweight alternative that only allows querying over RDF/JS sources.
    If the RDF/JS `Source` also implements the RDF/JS Store interface, then it is also supports update queries to add, change or delete quads in the store.

    Optional: query optimization

    The RDFJS Source interface by default only exposed the match method. In order to allow Comunica to produce more efficient query plans, you can optionally expose a countQuads method that has the same signature as match, but returns a number or Promise<number> that represents (an estimate of) the number of quads that would match the given quad pattern. Certain Source implementations may be able to provide an efficient implementation of this method, which would lead to better query performance.

    If Comunica does not detect a countQuads method, it will fallback to a sub-optimal counting mechanism where match will be called again to manually count the number of matches.