graphy

Query linked-data graphs by abstracting away traditional JSON-LD interaction
Install
$ npm install --save graphy
Usage
Take the following graph:
@prefix ns: <vocab://ns/> .
@prefix color: <vocab://color/> .
@prefix plant: <vocab://plant/> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
ns:Banana
a ns:Fruit ;
ns:shape "curved"^^ns:Liberty ;
ns:tastes "good"^^xsd:string ;
ns:data 25 ;
ns:class ns:Berry ;
ns:appears color:Yellow ;
plant:blossoms ns:YearRound ;
ns:alias ns:Cavendish ;
ns:alias ns:Naner ;
ns:alias ns:Bananarama ;
ns:stages (
ns:FindSpace
plant:Seed
plant:Grow
plant:Harvest
) ;
ns:considered [
a plant:Clone
]
Here, example.json
is a JSON-LD file generated from the graph above:
var graphy = require('graphy');
var json_ld = require('./example.json');
var q_graph = graphy(json_ld);
q_graph.network('ns:', function(k_banana) {
k_banana.$id;
k_banana['@id'];
k_banana.$type;
k_banana['@type'];
k_banana.shape();
k_banana.tastes();
k_banana.shape.$type;
k_banana.tastes.$type;
k_banana.shape['@type'];
k_banana.tastes['@type'];
k_banana.tastes['@full'];
k_banana.tastes.$('xsd:').$type;
k_banana.class();
k_banana.class.$id;
k_banana.class['@id'];
k_banana.class['@type'];
k_banana.appears();
k_banana.appears.$id;
k_banana.appears['@id'];
k_banana.appears['@type'];
k_banana.appears.$('color:').$id;
k_banana.$('plant:').blossoms();
k_banana.$('plant:').blossoms.$id;
k_banana.$('plant:').blossoms.$('ns:').$id;
k_banana.$terse();
k_banana.appears.$terse();
k_banana.tastes.$terse();
k_banana.tastes.$terse.value();
k_banana.tastes.$terse.datatype();
k_banana.$is();
k_banana.$is.node;
k_banana.appears.$is();
k_banana.data.$is();
k_banana.stages.$is();
k_banana.considered.$is();
k_banana.$is.node;
k_banana.appears.$is.iri;
k_banana.data.$is.literal;
k_banana.stages.$is.iri;
k_banana.stages.$is.literal;
k_banana.stages.$is.collection;
k_banana.considered.$is.blanknode;
k_banana.alias;
let a_items = k_banana('alias', function(k_alias) {
return k_alias();
});
a_items;
k_banana.stages().map(function(k_stage) {
return k_stage.$id || k_stage.$('plant:').$id;
});
k_banana.stages(function(k_stage) {
return k_stage.$id || k_stage.$('plant:').$id;
});
k_banana.stages(0).$id;
});
Iterating
for..in
for..of
RDF Collections
Calling a collection node as a function with no arguments will return the underlying array.
...
The returned array is the underlying array; mutating the returned object will also affect the underlying array
You can also iterate a collection node using for..of
for(let k_stage of k_banana.stages) {
}
In order to be consistent with the graph, rdf collection properties are emulated on collection objects. So instead of accessing a collection's elements via Array's properties/methods, you can also use the rdf:first
and rdf:rest
properties:
let w_list = k_banana.stages.$('rdf:');
w_list.first.$('ns:').$id;
w_list = w_list.rest;
w_list.first.$('plant:').$id;
w_list = w_list.rest;
w_list.first.$('plant:').$id;
w_list = w_list.rest;
w_list.first.$('plant:').$id;
w_list = w_list.rest;
w_list.$id;
let a_stages = [];
let w_list = k_banana.stages.$('rdf:');
while(w_list.$id !== 'nil') {
a_stage.push(w_list.first.$('plant:').$id || w_list.first.$('ns:').$id);
w_list = w_list.rest;
}
a_stages;
node.$(terse_namespace: string)
Returns a node that points to the same LD node but changes its namespace to the expanded version of the IRI given by terse_namespace
. By chaining this call, you can change the namespace on the same line to access properties or iris by their suffix.
node.$terse()
Returns a string representation of the node in terse form. The string is both SPARQL and TTL compatible; it is prefixed by the longest matching URI available in the original JSON-LD context, unless the resulting suffix would contain invalid characters for a prefixed IRI in either SPARQL or TTL.
License
ISC © Blake Regalia