All Projects → LDflex → Query-Solid

LDflex / Query-Solid

Licence: MIT license
Simple access to data in Solid pods through LDflex expressions

Programming Languages

javascript
184084 projects - #8 most used programming language
shell
77523 projects

Projects that are alternatives of or similar to Query-Solid

userguide
User guide for SolidOS Databrowser Frontend
Stars: ✭ 393 (+514.06%)
Mutual labels:  running-code
solid-client
Javascript client library for Solid applications
Stars: ✭ 76 (+18.75%)
Mutual labels:  running-code
solid-takeout-import
Import your Google Takeout (etc) data into your solid pod so you can keep it and use it with Solid apps
Stars: ✭ 37 (-42.19%)
Mutual labels:  running-code
Node Solid Server
Solid server on top of the file-system in NodeJS
Stars: ✭ 1,637 (+2457.81%)
Mutual labels:  running-code
solid-tutorial-angular
melvincarvalho.gitbooks.io/solid-tutorials/content/index.html
Stars: ✭ 15 (-76.56%)
Mutual labels:  running-code
solid-ui
User Interface widgets and utilities for Solid
Stars: ✭ 117 (+82.81%)
Mutual labels:  running-code
solid-panes
A set of core solid-compatible apps based on solid-ui
Stars: ✭ 97 (+51.56%)
Mutual labels:  running-code
solidos
The operating system for Solid
Stars: ✭ 88 (+37.5%)
Mutual labels:  running-code
solid-auth-client
A browser library for performing authenticated requests to Solid pods
Stars: ✭ 95 (+48.44%)
Mutual labels:  running-code
mashlib
Solid-compatible data mashup library and Data Browser
Stars: ✭ 67 (+4.69%)
Mutual labels:  running-code
solid-tutorial-rdflib.js
Tutorials for using rdflib.js
Stars: ✭ 34 (-46.87%)
Mutual labels:  running-code

LDflex for Solid

Simple access to data in Solid pods through LDflex expressions

npm version Build Status Coverage Status Dependency Status

This library is a configuration of the LDflex language for the Solid ecosystem. It configures LDflex with:

  1. JSON-LD context for Solid
  2. a Solid-authenticated query engine (Comunica or rdflib.js)
  3. useful data paths for Solid

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.

Creating data paths

Once you obtain the solid.data object, start writing data paths from the following entry points.

The user entry point

The solid.data.user path can query data about the currently logged in user, such as:

  • solid.data.user.firstName yields the user's first name(s)
  • solid.data.user.email yields the user's email address(es)
  • solid.data.user.friends yields the user's friend(s)
  • solid.data.user.friends.firstName yields the user's friends' first name(s)

The any URL entry point

The solid.data[url] path can query data about any subject by URL, such as:

  • solid.data['https://ruben.verborgh.org/profile/#me'].firstName
  • solid.data['https://ruben.verborgh.org/profile/#me'].email
  • solid.data['https://ruben.verborgh.org/profile/#me'].friends
  • solid.data['https://ruben.verborgh.org/profile/#me'].friends.firstName

Specifying properties

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 to http://xmlns.com/foaf/0.1/givenName)
  • prefixed names such as foaf:givenName (which expands to http://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 a 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.name
    • solid.data.user.foaf_name
    • solid.data.user.foaf$name
    • solid.data.user['foaf:name']
    • solid.data.user['http://xmlns.com/foaf/0.1/name']
  • vCard:
    • solid.data.user.vcard_fn
    • solid.data.user.vcard$fn
    • solid.data.user['vcard:fn']
    • solid.data.user['http://www.w3.org/2006/vcard/ns#fn']
  • Schema.org:
    • solid.data.user.schema_name
    • solid.data.user.schema$name
    • solid.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).

Installation

npm install @solid/query-ldflex

Usage

Within Node.js environments

const { 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`);
}

If, instead of Comunica, you want to use the rdflib.js query engine, install @ldflex/rdflib as a dependency of your project and use

const { default: data } = require('@solid/query-ldflex/lib/exports/rdflib');

When creating browser builds, it can be easier to simply tell webpack to replace @ldflex/comunica by @ldflex/rdflib.

In the browser

<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}!`);
});

To replace Comunica by rdflib.js, opt for

<script src="solid-auth-client.bundle.js"></script>
<script src="rdflib.min.js"></script>
<script src="solid-query-ldflex.rdflib.js"></script>

Adding a custom JSON-LD context

In addition to the default properties, you might want to support your own:

console.log(solid.data.context);       // the raw default JSON-LD context
await solid.data.context.extend({      // add new JSON-LD context
  con: 'http://www.w3.org/2000/10/swap/pim/contact#',
  preferred: 'con:preferredURI',
});
console.log(await solid.data.context); // the expanded JSON-LD context

// Now we can use both existing and new properties
const ruben = solid.data['https://ruben.verborgh.org/profile/#me'];
console.log(await ruben.name);
console.log(await ruben.preferred);

Be aware though that this leads to expressions that are less portable, because they only work when the additional context is added.

Resolving string expressions

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 path solid.data.user.firstName
  • solid.data.resolve('["https://example.org/"].label') becomes the path solid.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/'

License

©2018–present Ruben Verborgh, MIT License.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].