-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.ts
62 lines (52 loc) · 1.51 KB
/
main.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
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 context 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
// Get total count of all persons
const count = await Persons.count();
console.log(count); // number
// The following code will not work on DBPedia as it needs write permissions
// Insert a new person
/*
Persons.insert({
$id: "http://dbpedia.org/resource/Alan_Turing",
name: "Alan Turing",
birthDate: new Date("1912-06-23"),
});
*/
// Modify a person's name
/*
Persons.update({
$id: "http://dbpedia.org/resource/Alan_Turing",
name: "Not Alan Turing",
});
*/
// Delete a person
/*
Persons.delete("http://dbpedia.org/resource/Alan_Turing");
*/