Skip to main content
Module

x/ldkit/examples/basic/inverse.ts

LDkit - Linked Data query toolkit for TypeScript developers
Latest
File
import { createLens, type Options } from "ldkit";import { dbo, rdfs, xsd } from "ldkit/namespaces";
// Create a context for query engineconst options: Options = { sources: ["https://dbpedia.org/sparql"], // SPARQL endpoint language: "en", // Preferred language};
// Crete a simple movie schemaconst MovieSchema = { "@type": dbo.Film, name: rdfs.label,} as const;
// Create a movie director schemaconst DirectorSchema = { "@type": dbo.Person, name: rdfs.label, birthDate: { "@id": dbo.birthDate, "@type": xsd.date, }, isDirectorOf: { "@id": dbo.director, "@inverse": true, // This is cool! "@array": true, "@schema": MovieSchema, },} as const;
// Create a resource using the data schema and context aboveconst Persons = createLens(DirectorSchema, options);
// Get a particular person identified by IRIconst tarantino = await Persons.findByIri( "http://dbpedia.org/resource/Quentin_Tarantino",);
if (tarantino === null) { throw new Error("Tarantino not found?!");}
// List all movies directed by Tarantinoconsole.log("List of movies directed by Tarantino:");for (const movie of tarantino.isDirectorOf) { console.log(movie.name);}