Simple access to data in Solid pods through LDflex expressions
This library brings the LDflex language to Solid by:
- providing a JSON-LD context for Solid
- binding a query engine (Comunica)
- exposing useful data paths
LDflex expressions occur for example on Solid React components, where they make it easy for developers to specify what data they want to show. They can also be used as an expression language in any other Solid project or framework.
Once you obtain the solid.data object,
start writing data paths from the following entry points.
The solid.data.user path can query data about the currently logged in user,
such as:
solid.data.user.firstNameyields the user's first name(s)solid.data.user.emailyields the user's email address(es)solid.data.user.friendsyields the user's friend(s)solid.data.user.friends.firstNameyields the user's friends' first name(s)
The solid.data[url] path can query data about any subject by URL,
such as:
solid.data['https://ruben.verborgh.org/profile/#me'].firstNamesolid.data['https://ruben.verborgh.org/profile/#me'].emailsolid.data['https://ruben.verborgh.org/profile/#me'].friendssolid.data['https://ruben.verborgh.org/profile/#me'].friends.firstName
As you can see in the above examples, an LDflex path starts with an entry point and is followed by property names, which can be:
- abbreviations
such as
firstName(which expands tohttp://xmlns.com/foaf/0.1/givenName) - prefixed names
such as
foaf:givenName(which expands tohttp://xmlns.com/foaf/0.1/givenName) - full URLs
such as
http://xmlns.com/foaf/0.1/givenName
The abbreviations and prefixed names are expanded using the JSON-LD context. You can find some inspiration about what to ask for in this context.
You can access data using any vocabulary you want and, when included in the JSON-LD context, in multiple ways. For example:
- FOAF:
solid.data.user.namesolid.data.user.foaf_namesolid.data.user.foaf$namesolid.data.user['foaf:name']solid.data.user['http://xmlns.com/foaf/0.1/name']
- vCard:
solid.data.user.vcard_fnsolid.data.user.vcard$fnsolid.data.user['vcard:fn']solid.data.user['http://www.w3.org/2006/vcard/ns#fn']
- Schema.org:
solid.data.user.schema_namesolid.data.user.schema$namesolid.data.user['schema:name']solid.data.user['http://www.schema.org/name']
- Custom:
solid.data.user['http://example.org/my-ontology/name']
The traditional colon syntax for prefixes (schema:name)
can be substituted with an underscore (schema_name)
or dollar sign (schema$name).
This is because JavaScript keys with a colon
require quotes (user['schema:name'])
whereas underscores and dollar signs
can be used freely (user.schema_name, user.schema$name).
npm install @solid/query-ldflexconst { default: data } = require('@solid/query-ldflex');
const ruben = data['https://ruben.verborgh.org/profile/#me'];
showProfile(ruben);
async function showProfile(person) {
const label = await person.label;
console.log(`\nNAME: ${label}`);
console.log('\nTYPES');
for await (const type of person.type)
console.log(` - ${type}`);
console.log('\nFRIENDS');
for await (const name of person.friends.firstName)
console.log(` - ${name} is a friend`);
}<script src="solid-auth-client.bundle.js"></script>
<script src="solid-query-ldflex.bundle.js"></script>document.addEventListener('DOMContentLoaded', async () => {
const user = solid.data.user;
alert(`Welcome, ${await user.firstName}!`);
});LDflex expressions are actual JavaScript—not strings. There are times when strings are more useful though, such as when building declarative components that take LDflex expressions.
The solid.data object exports a resolve interface
that transforms a string expression into an actual LDflex path.
This string is appended to solid.data to obtain the resulting path.
For example:
solid.data.resolve('.user.firstName')becomes the pathsolid.data.user.firstNamesolid.data.resolve('["https://example.org/"].label')becomes the pathsolid.data["https://example.org/"].label
For convenience, the starting dot and quotes inside of brackets can be omitted. If the path is a single URL, quotes and brackets can be omitted. The following strings will all resolve:
'.user.firstName''user.firstName''["https://example.org/"].label''[https://example.org/].label'https://example.org/
©2018–present Ruben Verborgh, MIT License.