LDkit

function createLens
import { createLens } from "https://deno.land/x/ldkit/mod.ts";

Creates an instance of Lens that lets you query and update RDF data via data schema using TypeScript native data types.

In order to create a Lens instance, you need to provide a data schema that describes the data model which serves to translate data between Linked Data and TypeScript native types (see Schema for details).

You can also pass a set of options for LDkit and a query engine that specify the data source, preferred language, etc. (see Options for details).

Examples

Example 1

import { createLens, type Options } from "ldkit";
import { dbo, rdfs, xsd } from "ldkit/namespaces";

// Create options for query engine
const options: Options = {
  sources: ["https://dbpedia.org/sparql"], // SPARQL endpoint
  language: "en", // Preferred language
};

// Create a schema
const PersonSchema = {
  "@type": dbo.Person,
  name: rdfs.label,
  abstract: dbo.abstract,
  birthDate: {
    "@id": dbo.birthDate,
    "@type": xsd.date,
  },
} as const;

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

// List some persons
const persons = await Persons.find({ take: 10 });
for (const person of persons) {
  console.log(person.name); // string
  console.log(person.birthDate); // Date
}

// Get a particular person identified by IRI
const ada = await Persons.findByIri("http://dbpedia.org/resource/Ada_Lovelace");
console.log(ada?.name); // string "Ada Lovelace"
console.log(ada?.birthDate); // Date object of 1815-12-10

Parameters

schema: T

data schema which extends Schema

optional
options: Options

optional Options - contains LDkit and query engine configuration

Returns

Lens instance that provides interface to Linked Data based on the schema