This library lets you use the Comunica query engine with the LDflex language.
npm install ldflex @ldflex/comunica
const { PathFactory } = require('ldflex');
const { default: ComunicaEngine } = require('@ldflex/comunica');
const { namedNode } = require('@rdfjs/data-model');
// The JSON-LD context for resolving properties
const context = {
"@context": {
"@vocab": "http://xmlns.com/foaf/0.1/",
"friends": "knows",
}
};
// The query engine and its source
const queryEngine = new ComunicaEngine('https://ruben.verborgh.org/profile/');
// The object that can create new paths
const paths = new PathFactory({ context, queryEngine });
async function showPerson(person) {
console.log(`This person is ${await person.name}`);
console.log(`${await person.givenName} is friends with:`);
for await (const name of person.friends.givenName)
console.log(`- ${name}`);
}
const ruben = paths.create({
subject: namedNode('https://ruben.verborgh.org/profile/#me'),
});
showPerson(ruben);
This example uses the comunica engine for local file queries.
const { PathFactory } = require('ldflex');
const { default: ComunicaEngine } = require('@ldflex/comunica');
const { namedNode } = require('@rdfjs/data-model');
const { newEngine: localFileEngine } = require('@comunica/actor-init-sparql-file');
// The JSON-LD context for resolving properties
const context = {
"@context": {
"@vocab": "http://xmlns.com/foaf/0.1/",
"friends": "knows",
}
};
// The query engine and its source
const queryEngine = new ComunicaEngine(
path.join(__dirname, 'ruben-verborgh.ttl'),
{ engine: localFileEngine() }
);
// The object that can create new paths
const paths = new PathFactory({ context, queryEngine });
async function showPerson(person) {
console.log(`This person is ${await person.name}`);
console.log(`${await person.givenName} is friends with:`);
for await (const name of person.friends.givenName)
console.log(`- ${name}`);
}
const ruben = paths.create({
subject: namedNode('https://ruben.verborgh.org/profile/#me'),
});
showPerson(ruben);
Add comunica context options which are passed to the Comunica Engine.
const { PathFactory } = require('ldflex');
const { default: ComunicaEngine } = require('@ldflex/comunica');
const { namedNode } = require('@rdfjs/data-model');
// The JSON-LD context for resolving properties
const context = {
"@context": {
"@vocab": "http://xmlns.com/foaf/0.1/",
"friends": "knows",
}
};
// The query engine and its source
const queryEngine = new ComunicaEngine(
'https://ruben.verborgh.org/profile/',
{ options: {/* add options here */} },
);
// The object that can create new paths
const paths = new PathFactory({ context, queryEngine });
async function showPerson(person) {
console.log(`This person is ${await person.name}`);
console.log(`${await person.givenName} is friends with:`);
for await (const name of person.friends.givenName)
console.log(`- ${name}`);
}
const ruben = paths.create({
subject: namedNode('https://ruben.verborgh.org/profile/#me'),
});
showPerson(ruben);
By default the source given is also used as the destination for updates (if multiple sources are given, then the first one is chosen).
Optionally you can specify your own destination for updates as follows
// The query engine and its source
const queryEngine = new ComunicaEngine(
'https://ruben.verborgh.org/profile/',
{ destination: 'https://example.org/destination' },
);
©2018–present Ruben Verborgh, Joachim Van Herwegen, Jesse Wright. MIT License.