Skip to main content
Module

x/ldkit/library/lens/mod.ts>Lens#query

LDkit - Linked Data query toolkit for TypeScript developers
Latest
method Lens.prototype.query
import { Lens } from "https://deno.land/x/ldkit@2.0.0/library/lens/mod.ts";

Find entities with a custom SPARQL query.

The query must be a CONSTRUCT query, and the root nodes must be of type ldkit:Resource. So that the decoder can decode the results, the query must also return all properties according to the data schema.

Examples

Example 1

import { createLens } from "ldkit";
import { ldkit, schema } from "ldkit/namespaces";
import { CONSTRUCT } from "ldkit/sparql";

// Create a schema
const PersonSchema = {
  "@type": schema.Person,
  name: schema.name,
} as const;

// Create a resource using the data schema above
const Persons = createLens(PersonSchema);

// Query to find all persons named "Doe"
const query = CONSTRUCT`?s a <${ldkit.Resource}>; <${schema.name}> ?name`
  .WHERE`?s <${schema.name}> ?name; <${schema.familyName}> "Doe"`.build();

// Find all persons that match the custom query
const doePersons = await Persons.query(query);

Parameters

sparqlConstructQuery: string

CONSTRUCT SPARQL query

Returns

Promise<Unite<SchemaInterface<T>>[]>

Found entities