LDflex / Query-Solid

Simple access to data in Solid pods through LDflex expressions
https://solid.github.io/query-ldflex/
MIT License
66 stars 15 forks source link

Fetch an RDF list and return it as a Javascript array #53

Closed james-martin-jd closed 2 years ago

james-martin-jd commented 4 years ago

It would be a very nice feature-add to be able to fetch an RDF collection such as:

<#_:_book_shex_BookDemo__parts_7> a ui:Classifier;
    ui:values ("Science Fiction" "Fantasy" "Romance" "Western" "Non-fiction" "Thriller" "Horror" "Douglas Adams").

... and return the values as a JS array. (This is real data I've used for demos and testing the SDK, using the Form Model syntax and vocabs).

Right now we have to run the results of values through a custom function that parses the collection recursively into an array by removing the rdf$first item, appending it to an array, then sending rdf$rest recursively into itself. The code snippet we use is here:

async function loopList(doc: any) {
  let parts: any = []

  const field = await doc.rdf$first
  if (field) {
    const nextField = await loopList(doc.rdf$rest)
    parts = [...parts, field.value, ...nextField]
  }

  return parts
}

This currently works and returns a collection as an array, but something like this seems extremely useful for managing other datatypes than just Literals.

rubensworks commented 4 years ago

For reference, there's an abstraction for this in rdf-object: https://github.com/rubensworks/rdf-object.js#conveniently-access-rdf-lists We could easily port this code to LDflex: https://github.com/rubensworks/rdf-object.js/blob/master/lib/RdfListMaterializer.ts

RubenVerborgh commented 4 years ago

Just want to note that we'll need to be careful with blank nodes (#34).

jeswr commented 2 years ago

Resolved upstream in the main LDflex package https://github.com/LDflex/LDflex/pull/122