npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

changeset-sparql

v2.0.1

Published

applies a changeset via SPARQL Update

Downloads

8

Readme

changeset-sparql

Applies a changeset via SPARQL Update

npm install changeset-sparql

Usage:

First we make a changeset:

const EX = 'http://example.info/'
const changeset = {
    // an array of triples/quads to add
     add: [{s: EX+'123', p: EX+'count', o_value: 0, o_type:'literal', o_datatype: EX+'integer', g: EX+'graphs/graph-name'}]
    // triples/quads to remove (changeset will fail if they don't exist)
   , remove: [{s: EX+'987', p: EX+'message',  o_value: "Hello", o_type:'literal', o_lang: 'en-gb'}]
    // the URIs of the latest changesets for each subject we want to change
   , previous: ["urn:hash:sha1:jkhjkhkj2kjh3", "urn:hash:sha1:mnwejkljh7898c"],

}

Now we define two functions, one to do SPARQL queries, and one to do SPARQL updates.

Each function should take two arguments: a query/update and a "Node-style" ((err, data)=>) callback

const request = require('request')

const endpoint = 'http://localhost:3030/TestData'

const query = (q, callback) => request({
    url: endpoint+'/sparql',
    body: q,
    method: 'POST',
    headers: {
        'Accept': 'application/sparql-results+json',
        'Content-type': 'application/sparql-query'
    }
}, (err, data)=> err? callback(err) : callback(null, JSON.parse(data.body)))

const update = (q, callback) => request({
    url: endpoint+'/update',
    body: q,
    method: 'POST',
    headers: {
        'Accept': 'application/sparql-results+json',
        'Content-type': 'application/sparql-update'
    }
}, callback)

Now we pass those functions to changsetSparql, and get back a function that accepts a changeset, and a dictionary of callbacks:

const changsetSparql = require('changeset-sparql')(query, update)
changsetSparql(changeset, {
    error: console.error,
    rejected: console.error,
    ok: console.log
})
  • error Gets called with an error if there are problems running the updates or queries (ie: if query or update call the callback with an error). You may want to retry if this happens.
  • rejected gets called with an error message if the changeset was rejected. This happens if:
    • the subject and predicate of a create triple/quad already exists in the dataset
    • any delete triples/quads don't exist in the dataset. You should not retry, but you may want to alter the changeset and resubmit.
  • ok Gets called with the URI of the changeset if:
    • The update was successfully applied
    • The changeset was already applied

Changeset Object Specification

  • add: optional, must be an Array of quads to be added to the dataset
  • remove: optional, must be an Array of quads to be removed from the dataset
  • previous: optional, must be an Array of URIs of latest changesets of each subject

Triple/Quad Specification

(format of the objects in the add and remove Arrays)

  • s: required, URI (subject)
  • p: required, URI (predicate/property)
  • o_value: required (object value)
  • o_type: required, must be uri OR literal
  • o_lang: optional, must be valid language tag
  • o_datatype: optional, URI
  • g: optional, URI (graph)