LDkit
    Preparing search index...

    Function createLens

    • 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).

      Type Parameters

      Parameters

      • schema: T

        data schema which extends Schema

      • Optionaloptions: Options

        optional Options - contains LDkit and query engine configuration

      Returns Lens<T>

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

      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