LDflex is a domain-specific language for querying Linked Data on the Web as if you were browsing a local JavaScript graph.
You can write things like person.friends.firstName
to get a list of your friends.
Thanks to the power of JSON-LD contexts
and JavaScript's Proxy,
these properties are not hard-coded in LDflex,
but can be chosen at runtime.
They feel as if you're traversing a local object,
while you're actually querying the Web—without
pulling in all data first.
Tim Berners-Lee came up with the idea for such a fluid JavaScript interface to Linked Data, in a discussion on how to make Linked Data easier for developers.
npm install ldflexIn order to execute queries, you will also need a query engine:
npm install ldflex-comunicaWhen you have obtained a starting subject, you can navigate through its properties using standard JavaScript dot property syntax.
In order to query for the result,
use await if you want a single value,
or for await to iterate over all values.
const{ PathFactory }=require('ldflex');const{default: ComunicaEngine}=require('ldflex-comunica');const{ namedNode }=require('@rdfjs/data-model');// The JSON-LD context for resolving propertiesconstcontext={"@context": {"@vocab": "http://xmlns.com/foaf/0.1/","friends": "knows","label": "http://www.w3.org/2000/01/rdf-schema#label",}};// The query engine and its sourceconstqueryEngine=newComunicaEngine('https://ruben.verborgh.org/profile/');// The object that can create new pathsconstpath=newPathFactory({ context, queryEngine });construben=path.create({subject: namedNode('https://ruben.verborgh.org/profile/#me')});showPerson(ruben);asyncfunctionshowPerson(person){console.log(`This person is ${awaitperson.name}`);console.log(`${awaitperson.givenName} is interested in:`);forawait(constnameofperson.interest.label)console.log(`- ${name}`);console.log(`${awaitperson.givenName} is friends with:`);forawait(constnameofperson.friends.givenName)console.log(`- ${name}`);}(asyncperson=>{console.log(awaitperson.friends.givenName.pathExpression);})(ruben);(asyncdocument=>{forawait(constsubjectofdocument.subjects)console.log(`${subject}`);})(ruben);(asyncsubject=>{forawait(constpropertyofsubject.properties)console.log(`${property}`);})(ruben);(asyncperson=>{console.log(awaitperson.friends.givenName.sparql);})(ruben);©2018–present Ruben Verborgh, Ruben Taelman. MIT License.