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

@zazuko/cube-hierarchy-query

v3.0.0

Published

Facilitates querying RDF cube hierarchies

Downloads

1,054

Readme

@zazuko/cube-hierarchy-query esm

Use it to work with hierarchies defined using the RDF Cube Schema

Usage

The examples below assume dataset is an RDF/JS graph of Swiss cantons:

PREFIX schema: <http://schema.org/>
PREFIX meta: <https://cube.link/meta/>
PREFIX sh: <http://www.w3.org/ns/shacl#>

<hierarchy/Switzerland>
  a meta:Hierarchy ;
  meta:hierarchyRoot <country/CHE> ;
  meta:nextInHierarchy <hierarchy/Switzerland/cantons> .
  
<hierarchy/Switzerland/cantons>
  schema:name "Canton" ;
  sh:path schema:containsPlace ;
  meta:nextInHierarchy <hierarchy/Switzerland/districts> .
  
<hierarchy/Switzerland/districts>
  schema:name "Districts" ;
  sh:path [ sh:inversePath schema:containedInPlace ] ;
  meta:nextInHierarchy <hierarchy/Switzerland/municipalities> .
    
<hierarchy/Switzerland/municipalities>
  schema:name "Municipalities" ;
  sh:path [ sh:inversePath schema:containedInPlace ] .

Get entire hierarchy

The simplest usage is to retrieve entire hierarchy. It generates a CONSTRUCT SPARQL query which will retrieve triples of the root resources and all resources on all hierarchy levels.

  • sh:targetClass, if present is added as a restriction on each applicable level
import { getHierarchy } from '@zazuko/cube-hierarchy-query'
import $rdf from '@zazuko/env'
import StreamClient from 'sparql-http-builder'

let dataset: DatasetCore
const client = new StreamClient()

const myHierarchy = clownface({ dataset }).namedNode('my-hierarchy')
const results = await getHierarchy(myHierarchy).execute(client, $rdf)

results.forEach(print(0))

function print(indent: number) {
  return (hierarchyLevel: HierarchyNode) => {
    const { value } = hierarchyLevel.resource
    console.log(value.padStart(value.length + indent))
    hierarchyLevel.nextInHierarchy.forEach(print(indent + 2))
  }
}

By default, all properties of every hierarchy level are retrieved. That may prove very verbose and in such cases, the caller can explicitly request specific properties to be returned by passing a second argument to the getHierarchy function.

import rdf from '@zazuko/env'

getHierarchy(myHierarchy, {
  properties: [
    rdf.ns.schema.identifier,
    [rdf.ns.schema.name, { language: 'de' }]  
  ]
})

The above will fetch only schema:identifier and schema:name of all levels, additionally filtering names only in German.

Find resources

Given a hierarchy level (GraphPointer) and the URI of a resource from that hierarchy, finds children of that resource

import { children } from '@zazuko/cube-hierarchy-query/resources'
import $rdf from '@zazuko/env'
import StreamClient from 'sparql-http-builder'
import { schema } from '@tpluscode/rdf-ns-builders'

let dataset: DatasetCore
let municipality: NamedNode

const districtLevel = clownface({ dataset }).namedNode('hierarchy/Switzerland/districts')
const client = new StreamClient()

const query = children(districtLevel, municipality, {
  limit: 10,            // optional (default 1)
  offset: 10,           // optional (default 0)
  orderBy: schema.name, // optional (default undefined)
})

// will return array of graph pointers to children on the given municipality
const pointers: GraphPointer[] = await query.execute(client, $rdf)

Introspect properties

Given a hierarchy level (GraphPointer), finds properties which connect resources from that level with the next.

Return a graph of ?property rdfs:label ?label quads of found properties

import { properties } from '@zazuko/cube-hierarchy-query/introspect'
import StreamClient from 'sparql-http-builder'

const districtLevel = clownface({ dataset }).namedNode('hierarchy/Switzerland/cantons')
const client = new StreamClient()

const stream = await properties(cantonLevel).execute(client.query)

Introspect types

Given a hierarchy level (GraphPointer), finds types of resources at the given level

Return a graph of ?type rdfs:label ?label quads of found properties

import { types } from '@zazuko/cube-hierarchy-query/introspect'
import StreamClient from 'sparql-http-builder'

const districtLevel = clownface({ dataset }).namedNode('hierarchy/Switzerland/cantons')
const client = new StreamClient()

const stream = await types(cantonLevel).execute(client.query)

Examples

The examples directory contains snippets showing the usage on real cubes & hierarchies.

To run call from example NPM script and pass the example file's path as argument. For example

yarn example ./examples/children.ts

To have the executed queries printed in the console, set a DEBUG environment variable:

-yarn example ./examples/children.ts
+DEBUG=SPARQL yarn example ./examples/children.ts